Flutter - Collapsing ExpansionTile after choosing an item

后端 未结 6 1401
傲寒
傲寒 2020-11-27 20:04

I\'m trying to get ExpansionTile to collapse after I choose an item, but it does not close the list that was opened.

I tried to use the onExpansio

6条回答
  •  粉色の甜心
    2020-11-27 21:07

    Create a clone from ExpansionTile class and replace build method code by the following:

    @override
    Widget build(BuildContext context) {
      final bool closed = !_isExpanded && _controller.isDismissed;
      return AnimatedBuilder(
        animation: _controller.view,
        builder: _buildChildren,
        child: closed ? null : GestureDetector(
          child: Column(children: widget.children),
          onTap: _handleTap,
        ),
      );
    }
    

    and then ExpansionTile will collapse after click on each item.

    Note: if one of children has onTap call back, this solution doesn't work. in this case you must provide onChildTap handler to pass index of tapped child in use case.(contact me for complete code)

提交回复
热议问题