是否可以在 flutter 中为 SliverList 内的按钮实现可关闭小部件

use*_*349 4 android dart flutter

假设我已经构建了一个如下所示的条子列表。

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),
                ),
              ),
            ],
          ));
Run Code Online (Sandbox Code Playgroud)

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

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

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

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

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

die*_*per 5

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

\n\n

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

\n\n
\n

可滑动列表项的 Flutter 实现,具有可关闭的定向滑动\n 操作。

\n
\n\n

用法:

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

  • 我认为当您在小部件消失之前滑动小部件时,dissmissible 没有选择操作的选项。 (2认同)