xrr2016 / flutter-tree

Flutter tree widget
https://pub.dev/packages/flutter_tree
MIT License
67 stars 41 forks source link

add this change to support null safety #7

Open Marrawi1994 opened 2 years ago

Marrawi1994 commented 2 years ago

Hi Guys ... For anyone need to use this library with null safety support .. follow these steps :

First : copy files from root directory -> lib -> src and paste in your project

Secound : Go to tree_view.dart

change code in line 18 to this

 const TreeView({
    Key? key,  // add this line
    @required this.data,
    this.titleKey = 'title',
    this.leadingKey = 'leading',
    this.expanedKey = 'expaned',
    this.childrenKey = 'children',
    this.offsetLeft = 24.0,
    this.titleOnTap,
    this.leadingOnTap,
    this.trailingOnTap,
  }) : super(key: key);  // replace with this line

third : Go to tree_node.dart

change code in line 17 to this :

 const TreeNode({ 
    Key? key, // add this line
    this.level = 0,
    this.expaned = false, 
    this.offsetLeft = 24.0,
    this.children = const [],
    this.title = const Text('Title'),
    this.leading = const IconButton(
      icon: Icon(Icons.star_border),
      iconSize: 16,
      onPressed: null,
    ),
    this.trailing = const IconButton(
      icon: Icon(Icons.expand_more),
      iconSize: 16,
      onPressed: null,
    ),
    this.titleOnTap,
    this.leadingOnTap,
    this.trailingOnTap,
  }); : super(key: key); // replace with this line

in line 50 add this:

  @override // add this line
  initState() {
    _isExpaned = widget.expaned;
    _rotationController = AnimationController(
      duration: const Duration(milliseconds: 300),
      vsync: this,
    );
    super.initState();
  }

in line 90 and line 102 :

const SizedBox(width: 6.0), // add const
Expanded(
  child: GestureDetector(
    onTap: () {
      if (widget.titleOnTap != null &&
          widget.titleOnTap is Function) {
        widget.titleOnTap();
      }
    },
    child: widget.title ?? Container(),
  ),
),
const SizedBox(width: 6.0), // add const
Visibility(
  visible: children.isNotEmpty, // replace with isNotEmpty

in line 136 :

Visibility(
  visible: children.isNotEmpty && _isExpaned, // replace with isNotEmpty
  child: Padding(
    padding: EdgeInsets.only(left: level + 1 * offsetLeft),
    child: Column(
      children: widget.children,
      crossAxisAlignment: CrossAxisAlignment.start,
    ),
  ),
),

That's all šŸ‘ šŸ‘ šŸ‘

xrr2016 commented 2 years ago

It's null safety now!