generate_tree 2.2.3 copy "generate_tree: ^2.2.3" to clipboard
generate_tree: ^2.2.3 copied to clipboard

Create beautiful tree structure in flutter

example/lib/main.dart

import 'package:flutter/material.dart';
import 'package:generate_tree/generate_tree.dart';
import 'package:generate_tree/treeNode.dart';

void main() {
  WidgetsFlutterBinding.ensureInitialized();
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      color: Colors.lightBlue,
      theme: ThemeData(dividerColor: Colors.transparent),
      home: Scaffold(
        body: SafeArea(
          child: MyHomePage(),
        ),
      ),
    );
  }
}

class MyHomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final List data = [
      {
        "checked": true,
        "children": [
          {
            "checked": true,
            "show": false,
            "children": [],
            "id": 11,
            "pid": 1,
            "commonID": 1,
            "title": "Child title 11"
          }
        ],
        "id": 1,
        "pid": 0,
        "commonID": 1,
        "show": false,
        "title": "Parent title 1"
      },
      {
        "checked": true,
        "show": false,
        "children": [],
        "id": 2,
        "commonID": 2,
        "pid": 0,
        "title": "Parent title 2"
      },
      {
        "checked": true,
        "children": [
          {
            "checked": true,
            "children": [],
            "id": 31,
            "commonID": 3,
            "pid": 3,
            "show": false,
            "title": "Parent title 3.1"
          },
          {
            "checked": true,
            "children": [
              {
                "checked": true,
                "children": [],
                "id": 321,
                "commonID": 3,
                "pid": 32,
                "show": false,
                "title": "Parent title 3.2.1"
              },
              {
                "checked": true,
                "children": [],
                "id": 322,
                "commonID": 3,
                "pid": 32,
                "show": false,
                "title": "Parent title 3.2.2"
              }
            ],
            "id": 32,
            "commonID": 3,
            "pid": 3,
            "show": false,
            "title": "Parent title 3.2"
          }
        ],
        "id": 3,
        "commonID": 3,
        "pid": 0,
        "show": false,
        "title": "Parent title 3"
      }
    ];

    final List<TreeNode> treeNodes =
        data.map((item) => TreeNode.fromJson(item)).toList();

    return Scaffold(
      appBar: AppBar(
        title: Text('Generate Tree'),
      ),
      body: GenerateTree(
        data: treeNodes,
        selectOneToAll: true,
        textColor: Colors.black,
        onChecked: (node, checked, commonID) {
          print('isChecked : $checked');
          print('common Node ID : ${commonID}');
          print(
              'children node data : ${node.children.map((e) => '${e.title}')}');
        },
        checkBoxColor: Colors.green,
        childrenPadding: EdgeInsets.only(left: 30, top: 0, right: 0, bottom: 0),
      ),
    );
  }
}
8
likes
120
points
152
downloads

Publisher

verified publishernexasync.in

Weekly Downloads

Create beautiful tree structure in flutter

Repository (GitHub)
View/report issues

Documentation

API reference

License

BSD-3-Clause (license)

Dependencies

collection, flutter

More

Packages that depend on generate_tree