Flutter 列表长按排序错乱问题

2021-02-19  本文已影响0人  北纬357
Item的最外层Containe height必须设置,否则长按排序页面会有错乱问题,Containe也必须自定义不同的key: ValueKey('custom key $index'),
代码如下:
  @override
  Widget build(BuildContext context) {
    themeModule = context.watch<ThemeModule>();
    groupModule = context.watch<CrmGroupModule>();
    return Container(
      color: themeModule.themeData.backgroundColor,
      child: ReorderableListView(
        header: _buildItemHeader(context), //设置头
        children: _buildItemDatas(context),//构建item
        onReorder: (int oldIndex, int newIndex) {//长按排序回调
          _dragSortGroup(context, oldIndex, newIndex);
        },
      ),
    );
  }

  Widget _buildItemHeader(BuildContext context) {
    return GestureDetector(
        onTap: () {
          _onClickAddGroup(context);
        }, //themeModule.themeData.scaffoldBackgroundColor
        child: Container(
          color: themeModule.themeData.scaffoldBackgroundColor,
          child: Row(
            children: [
              Container(
                height: 55,
                padding: EdgeInsets.only(left: 20, top: 5),
                child: Icon(
                  Icons.add_circle_outline,
                  color: Colors.green,
                ),
              ),
              Expanded(
                child: Container(
                  child: Padding(
                      padding: EdgeInsets.only(left: 5, top: 5),
                      child: Align(
                        alignment: Alignment.centerLeft,
                        child: Text(
                          '添加分组',
                          overflow: TextOverflow.ellipsis,
                          maxLines: 1,
                          style: TextStyle(fontSize: 18),
                          textAlign: TextAlign.center,
                        ),
                      )
                  ),
                ),
              ),
            ],
          ),
        )
    );
  }
  List<Widget> _buildItemDatas(BuildContext context) {
    List<Widget> items = [];
    for (var index = 0; index < groupModule.editGroupLists.length; index++) {
      items.add(_buildItem(context, index));
    }
    return items;
  }

  Widget _buildItem(BuildContext context, int index) {
    return Container(
        key: ValueKey('custom key $index'),
        color: Colors.white,
        height: 50, //高必须设置,否则长按排序页面会有错乱问题
        child: _buildItemColumn(context, index)
    );
  }
上一篇 下一篇

猜你喜欢

热点阅读