Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ability added to use custom icons along with default icons in Node Le… #451

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -59,18 +59,20 @@ class _ControllerUsageState extends State<ControllerUsage> {
Widget buildTree() {
return TreeView(
treeController: _controller,
primaryIcon: Icon(Icons.check_box_outline_blank),
secondaryIcon: Icon(Icons.check_box),
nodes: [
TreeNode(content: Text("node 1")),
TreeNode(
content: Icon(Icons.audiotrack),
content: Text("Icons.audiotrack"),
children: [
TreeNode(content: Text("node 21")),
TreeNode(
content: Text("node 22"),
key: _key,
children: [
TreeNode(
content: Icon(Icons.sentiment_very_satisfied),
content: Text("Icons.sentiment_very_satisfied"),
),
],
),
Expand Down
12 changes: 10 additions & 2 deletions packages/flutter_simple_treeview/lib/src/builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,14 @@ import 'primitives/tree_controller.dart';
import 'primitives/tree_node.dart';

/// Builds set of [nodes] respecting [state], [indent] and [iconSize].
Widget buildNodes(Iterable<TreeNode> nodes, double? indent,
TreeController state, double? iconSize) {
Widget buildNodes(
Iterable<TreeNode> nodes,
double? indent,
TreeController state,
double? iconSize,
Widget? primaryIcon,
Widget? secondaryIcon,
) {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Expand All @@ -22,6 +28,8 @@ Widget buildNodes(Iterable<TreeNode> nodes, double? indent,
indent: indent,
state: state,
iconSize: iconSize,
primaryIcon: primaryIcon,
secondaryIcon: secondaryIcon,
)
],
);
Expand Down
53 changes: 32 additions & 21 deletions packages/flutter_simple_treeview/lib/src/node_widget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,26 @@ class NodeWidget extends StatefulWidget {
final double? indent;
final double? iconSize;
final TreeController state;
final Widget? primaryIcon;
final Widget? secondaryIcon;

const NodeWidget(
{Key? key,
required this.treeNode,
this.indent,
required this.state,
this.iconSize})
: super(key: key);
const NodeWidget({
Key? key,
required this.treeNode,
this.indent,
required this.state,
this.iconSize,
this.primaryIcon,
this.secondaryIcon,
}) : super(key: key);

@override
_NodeWidgetState createState() => _NodeWidgetState();
}

class _NodeWidgetState extends State<NodeWidget> {
bool get _isLeaf {
return widget.treeNode.children == null ||
widget.treeNode.children!.isEmpty;
return widget.treeNode.children == null || widget.treeNode.children!.isEmpty;
}

bool get _isExpanded {
Expand All @@ -44,32 +47,40 @@ class _NodeWidgetState extends State<NodeWidget> {
var icon = _isLeaf
? null
: _isExpanded
? Icons.expand_more
: Icons.chevron_right;
? widget.secondaryIcon ?? Icon(Icons.expand_more)
: widget.primaryIcon ?? Icon(Icons.chevron_right);

var onIconPressed = _isLeaf
? null
: () => setState(
() => widget.state.toggleNodeExpanded(widget.treeNode.key!));
var onIconPressed = _isLeaf ? null : () => setState(() => widget.state.toggleNodeExpanded(widget.treeNode.key!));

return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
children: [
IconButton(
iconSize: widget.iconSize ?? 24.0,
icon: Icon(icon),
onPressed: onIconPressed,
Material(
child: InkWell(
borderRadius: BorderRadius.circular(2.00),
child: SizedBox.square(
dimension: widget.iconSize ?? 24.0,
child: icon,
),
onTap: onIconPressed,
),
),
widget.treeNode.content,
],
),
if (_isExpanded && !_isLeaf)
Padding(
padding: EdgeInsets.only(left: widget.indent!),
child: buildNodes(widget.treeNode.children!, widget.indent,
widget.state, widget.iconSize),
child: buildNodes(
widget.treeNode.children!,
widget.indent,
widget.state,
widget.iconSize,
widget.primaryIcon,
widget.secondaryIcon,
),
)
],
);
Expand Down
30 changes: 22 additions & 8 deletions packages/flutter_simple_treeview/lib/src/tree_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,21 @@ class TreeView extends StatefulWidget {
/// Tree controller to manage the tree state.
final TreeController? treeController;

TreeView(
{Key? key,
required List<TreeNode> nodes,
this.indent = 40,
this.iconSize,
this.treeController})
: nodes = copyTreeNodes(nodes),
/// This widget will be takes place of default icon when Node will not be in expanded state
final Widget? primaryIcon;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rename to iconExpanded


/// This widget will be takes place of default icon when Node will be in expanded state
final Widget? secondaryIcon;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rename to iconCollapsed

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here and in another one, make widget non-nullable and assign default value.


TreeView({
Key? key,
required List<TreeNode> nodes,
this.indent = 40,
this.iconSize,
this.treeController,
this.primaryIcon,
this.secondaryIcon,
}) : nodes = copyTreeNodes(nodes),
super(key: key);

@override
Expand All @@ -50,6 +58,12 @@ class _TreeViewState extends State<TreeView> {
@override
Widget build(BuildContext context) {
return buildNodes(
widget.nodes, widget.indent, _controller!, widget.iconSize);
widget.nodes,
widget.indent,
_controller!,
widget.iconSize,
widget.primaryIcon,
widget.secondaryIcon,
);
}
}