简体   繁体   中英

How to use a dismissible widget inside a CustomScrollView in Flutter?

I am trying to create a list of dismissible cards inside a customscrollview. The cards are getting rendered, but when i swipe the cards to dismiss them , they don't get removed from the list. Below is the code. Please help.

  CustomScrollView customScroll = new CustomScrollView(
    slivers: <Widget>[
      new SliverAppBar(
        backgroundColor: Colors.black,
        automaticallyImplyLeading: false,
        expandedHeight: 90.0,
        title: new Text("Test"),
      ),
      new SliverFixedExtentList(
        itemExtent: 128.0,
        delegate: new SliverChildBuilderDelegate(
              (BuildContext context, int index) {
              return new Dismissible(key: new ObjectKey(objects[index]),
              child: widget.widgetAdapter(objects[index]),
              onDismissed: (DismissDirection direction) {
                setState(() {
                  this.objects.removeAt(index);
                  this.reIndex();
                });
                direction == DismissDirection.endToStart ? print(
                    "favourite") : print("remove");
              },
              background: new Container(
                  color: const Color.fromRGBO(183, 28, 28, 0.8),
                  child: const ListTile(
                      leading: const Icon(
                          Icons.delete, color: Colors.white, size: 36.0)
                  )
              ),
              secondaryBackground: new Container(
                  color: const Color.fromRGBO(0, 96, 100, 0.8),
                  child: const ListTile(
                      trailing: const Icon(
                          Icons.favorite, color: Colors.white, size: 36.0)
                  )
              ),
            );
            },
           childCount: objects.length,
        ),
      ),
    ]
);

your attempt is basically correct - I have simplified list creation and replaced it in your sample code below - what you are looking for is in the dmiss function @ line 35;

import 'package:flutter/material.dart';

class TestDismissCSV extends StatefulWidget {
  @override
  _TestDismissCSVState createState() => new _TestDismissCSVState();
}

class _TestDismissCSVState extends State<TestDismissCSV> {
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      title: "Dismiss in Cust Scroll V",
      theme: new ThemeData(brightness: Brightness.dark),
      home: new Scaffold(
        body: dmiss(context),
      ),
    );
  }

  List<TheListClass> _theList;

  Widget dmiss(context) {
    return new CustomScrollView(slivers: <Widget>[
      new SliverAppBar(
        backgroundColor: Colors.black,
        automaticallyImplyLeading: false,
        expandedHeight: 90.0,
        title: new Text("Test"),
      ),
      new SliverFixedExtentList(
        itemExtent: 128.0,
        delegate: new SliverChildBuilderDelegate(
          (BuildContext context, int index) {
            return new Dismissible(
              key: new ObjectKey(_theList[index]),
              child: new Material(child: new Text(_theList[index].title)),
              onDismissed: (DismissDirection direction) {
                setState(() {
                  this._theList.removeAt(index);
                  //this.reIndex();
                });
                direction == DismissDirection.endToStart
                    ? print("favourite")
                    : print("remove");
              },
              background: new Container(
                  color: const Color.fromRGBO(183, 28, 28, 0.8),
                  child: const ListTile(
                      leading: const Icon(Icons.delete,
                          color: Colors.white, size: 36.0))),
              secondaryBackground: new Container(
                  color: const Color.fromRGBO(0, 96, 100, 0.8),
                  child: const ListTile(
                      trailing: const Icon(Icons.favorite,
                          color: Colors.white, size: 36.0))),
            );
          },
          childCount: _theList.length,
        ),
      ),
    ]);
  }

  @override
  void initState() {
    super.initState();
    _theList = new List<TheListClass>();

    for (var i = 0; i < 100; i++) {
      _theList.add(new TheListClass('List Item ' + i.toString()));
    }
  }

  @override
  void dispose() {
    super.dispose();
  }
}

class TheListClass {
  String title;

  TheListClass(this.title);
}

List Item dismissed

Happy coding!

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