【问题标题】:is it Possible to Implement a Dismissible widget for a button inside a SliverList in flutter是否可以在颤动中为 SliverList 内的按钮实现 Dismissible 小部件
【发布时间】:2018-08-13 04:07:42
【问题描述】:

假设我已经建立了一个看起来像这样的 sliverlist。

return new Container(
              child: new CustomScrollView(
            scrollDirection: Axis.vertical,
            shrinkWrap: false,

            slivers: <Widget>[
              new SliverPadding(
                padding: const EdgeInsets.symmetric(vertical: 2.0),
                sliver: new SliverList(
                  delegate: new SliverChildBuilderDelegate(
                      (BuildContext context, int index) {
                    ModelClass class= _List[index];

                    return new Dismissible(
                      key: new ObjectKey(_List[index]),
                      child: ModelCard(class),
                      onDismissed: (DismissDirection direction) {
                        setState(() {
                          _List.removeAt(index);

                          direction == DismissDirection.endToStart;
                        });
                      },
                      background: new Container(
                          color: const Color.fromRGBO(183, 28, 28, 0.8),
                          child: new Center(
                            child: new Text(
                              "Item Removed",
                              style: new TextStyle(color: Colors.white),
                            ),
                          )),

                    );

                    // return new ModelCard(class);
                  }, childCount: _List.length),
                ),
              ),
            ],
          ));

现在我有一个名为 ModelCard 的无状态小部件来填充这样的列表

   new Container(
                    padding: EdgeInsets.fromLTRB(80.0, 10.0, 0.0, 0.0),
                    child: new Text(
                      "${class.listDescription}",
                      style: new TextStyle(),
                    ),
                  ),

现在我想要一个图标按钮来关闭一个项目,所以我将它添加到卡片中

new Container(
                    padding: EdgeInsets.fromLTRB(350.0, 20.0, 0.0, 0.0),
                    child: new IconButton(
                        icon: new Icon(Icons.delete), onPressed: () {}),
                  ),

您将如何在图标按钮内实现可关闭小部件,当在颤动中按下时,该小部件会关闭列表中的项目?

【问题讨论】:

  • 按下按钮时是否需要移除项目?
  • 哦,是的,我忘了说,我的错
  • 您能描述一下您当前的实现遇到的问题吗?
  • 我可以在文档中给出的常用示例和某些站点中的一些示例中使用可关闭小部件,但我似乎找不到有关使用按钮或图标按钮关闭项目的示例在列表视图中,而不是使用 Dismissible,您只需滑动即可将其删除,

标签: android dart flutter


【解决方案1】:

好的,已经有一个包可以满足你的需要。

https://pub.dartlang.org/packages/flutter_slidable

带有方向滑动的可滑动列表项的 Flutter 实现 可以取消的操作。

用法:

  new Slidable(
    delegate: new SlidableScrollDelegate(),
    actionExtentRatio: 0.25,
    child: new Container(
      color: Colors.white,
      child: new ListTile(
        leading: new CircleAvatar(
          backgroundColor: Colors.indigoAccent,
          child: new Text('$3'),
          foregroundColor: Colors.white,
        ),
        title: new Text('Tile n°$3'),
        subtitle: new Text('SlidableDrawerDelegate'),
      ),
    ),
    actions: <Widget>[
      new IconSlideAction(
        caption: 'Archive',
        color: Colors.blue,
        icon: Icons.archive,
        onTap: () => _showSnackBar('Archive'),
      ),
      new IconSlideAction(
        caption: 'Share',
        color: Colors.indigo,
        icon: Icons.share,
        onTap: () => _showSnackBar('Share'),
      ),
    ],

    );

【讨论】:

  • 为什么不使用 Dismissible?
  • 我认为 dissmisible 在小部件消失之前没有选择操作的选项。
猜你喜欢
  • 1970-01-01
  • 2020-02-16
  • 2021-11-28
  • 2021-06-10
  • 2018-05-10
  • 2020-05-23
  • 2022-06-23
  • 2021-07-18
  • 1970-01-01
相关资源
最近更新 更多