-2

I want to add a function when the showModalBottomSheet is close or when the barrier tapped the function run(routing to the main page) but how?

I want to run this when the barrier tapped/modal closed

Navigator.pushNamedAndRemoveUntil(context, '/mainMenuPage', (route) => false);

Modal Code

showModalBottomSheet(
    shape: RoundedRectangleBorder(
      borderRadius: BorderRadius.vertical(
        top: Radius.circular(25.0),
      ),
    ),
    backgroundColor: Colors.white,
    context: context,
    builder: (context) => Widget(...),
   );
  }
3

2 Answers 2

1

Try below code hope its help to you.

Your Widget with bottomSheet:

Center(
      child: ElevatedButton(
        child: const Text('showModalBottomSheet'),
        onPressed: () {
          showModalBottomSheet<void>(
            context: context,
            builder: (BuildContext context) {
              return Container(
                height: 200,
                color: Colors.amber,
                child: Center(
                  child: Column(
                    mainAxisAlignment: MainAxisAlignment.center,
                    mainAxisSize: MainAxisSize.min,
                    children: <Widget>[
                      const Text('Modal BottomSheet'),
                    ],
                  ), //MyWidget()
                ),
              );
            },
          ).then(
            (value) => Navigator.push(
              context,
              MaterialPageRoute(
                builder: (context) => MyWidget(),
              ),
            ),
          );
        },
      ),
    ),

Other class:

class MyWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Container(
      color: Colors.white,
      child: Center(
        child: Text(
          'Hello, World!',
          style: Theme.of(context).textTheme.headline4,
        ),
      ),
    );
  }
}

.then() -> This means that the next instructions will be executed. But it allows you to execute the code when the asynchronous method completes.

0
0
showModalBottomSheet(
    context: context,
    builder: (context){
      return Text("example");
    }
).then((value) => {
  Navigator.of(context).push(MaterialPageRoute(builder: (context) => const Page2()))
});

You can try like this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.