简体   繁体   中英

is it Possible to Implement a Dismissible widget for a button inside a SliverList in flutter

so lets say I have built a sliverlist that looks like this.

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),
                ),
              ),
            ],
          ));

and now i have a stateless widget called ModelCard to populate the list like this one

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

now I want to have an Icon button to dismiss an item so i added it inside the card

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

How would you implement the dismissible widget inside an icon button that dismiss an Item in a list when pressed in flutter?

Ok , there is already a package which do what you need.

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

A Flutter implementation of slidable list item with directional slide actions that can be dismissed.

Usage:

  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'),
      ),
    ],

    );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM