Show menu

main.dart

  void main() => runApp(DemoApp());

  class DemoApp extends StatelessWidget {
    const DemoApp();

    @override
    Widget build(BuildContext context) {
      return MaterialApp(
        home: Scaffold(
          appBar: AppBar(
            title: const Text("Swipe to delete"),
          ),
          body: const Center(
            child: SwipeItems(),
          )
        ),
      );
    }
  }
  

swipelist_provider.dart

  class SourceList with ChangeNotifier {
    final _myList = List<String>.generate(10, (i) {
      return "Number $i";
    });

    List<String> get values => UnmodifiableListView(_myList);

    void removeItem(int index) {
      _myList.removeAt(index);
      notifyListeners();
    }
  }
  

swipe_items.dart

  class SwipeItems extends StatelessWidget {
    const SwipeItems();

    @override
    Widget build(BuildContext context) {
      return Consumer<SourceList>(
        builder: (context, list, _) {
          return ListView.builder(
            itemCount: list.values.length,
            itemBuilder: (context, index) {
              var item = list.values[index];

              return Dismissible(
                key: Key(item),
                background: Container(
                  color: Colors.redAccent,
                ),
                onDismissed: (direction) {
                  list.removeItem(index);
                },
                direction: DismissDirection.startToEnd,
                confirmDismiss: (direction) => _getConfirm(context, direction),
                child: ListTile(
                  leading: const Icon(Icons.trending_flat),
                  title: Text(item),
                ),
              );
            },
          );
        },
      );
    }

    Future<bool> _getConfirm(BuildContext context, DismissDirection direction) {
      return showDialog<bool>(
        context: context,
        barrierDismissible: false, // user must tap button!
        builder: (BuildContext context) {
          return AlertDialog(
            title: const Text("Confirm"),
            content: const Text("Do you really want to delete this item?"),
            actions: <Widget>[
              RaisedButton(
                child: Text("Nope"),
                color: Colors.blueAccent,
                onPressed: () {
                  Navigator.of(context)?.pop(false);
                },
              ),
              FlatButton(
                child: Text("Yes"),
                onPressed: () {
                  Navigator.of(context)?.pop(true);
                },
              ),
            ],
          );
        },
      );
    }

  }
  

swipelist_provider.dart

  class DismissBackground extends StatelessWidget {
    const DismissBackground();

    @override
    Widget build(BuildContext context) {
      return Container(
        color: Colors.redAccent,
      );
    }
  }
  

This website and the book are not official Google products. No affiliations are involved. Built with Java 14 and Vert.X

"Flutter and the related logo are trademarks of Google LLC. We are not endorsed by or affiliated with Google LLC"