Home > other >  Close Multiple Bottom Sheet Modals in Flutter
Close Multiple Bottom Sheet Modals in Flutter

Time:08-28

I have two bottom sheet modals on a page. In the second modal, I do an action with cubit. On the main page, I listen to the event emitted from cubit, I want to close those two modals from the main page. I have tried these ways:

context.popRoute();
return context.read<BillCubit>().fetch();

or

context
     ..popRoute()
     ..popRoute();
return context.read<BillCubit>().fetch();

Both of those codes only close the second modal (last modal).

How to deal with it?

CodePudding user response:

Just use Navigator.of(context).popUntil(ModalRoute.withName('/')); where '/' is the page route before the modals. This is going to pop all modals until it reaches the specified route.

Check out below a minimal reproducible example (also the live demo on DartPad)

import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const MyHomePage(),
      debugShowCheckedModeBanner: false,
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({Key? key}) : super(key: key);

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  void _showModalBottomSheet({int count = 1}) {
    showModalBottomSheet(
        context: context,
        builder: (context) {
          return Container(
            color: Colors.amber,
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                Text('ModalBottomSheet $count'),
                const SizedBox(height: 8),
                ElevatedButton(
                  onPressed: () => _showModalBottomSheet(count: count   1),
                  child: const Text(' 1'),
                ),
                const SizedBox(height: 8),
                ElevatedButton(
                  onPressed: () {
                    Navigator.of(context).pop();
                  },
                  child: const Text('Close this'),
                ),
                const SizedBox(height: 8),
                ElevatedButton(
                  onPressed: () {
                    // Pop until the initial page... in that case the root page
                    Navigator.of(context).popUntil(ModalRoute.withName('/'));
                  },
                  child: const Text('Close all'),
                ),
              ],
            ),
          );
        });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: const [
            Text(
              'Click   to show ModalBottomSheet',
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _showModalBottomSheet,
        tooltip: 'Show ModalBottomSheet',
        child: const Icon(Icons.add),
      ),
    );
  }
}
  • Related