gpt-3.5-turbo-0301· openai
Suzhen/CodeChat
Flutter how to show modalbottomsheet from one context on another context?
- User
Flutter how to show modalbottomsheet from one context on another context?
- Assistant
To show a
ModalBottomSheetfrom one context on another context, you will need to pass the second context to the first context and use it as thecontextparameter in theshowModalBottomSheetmethod.Here's an example:
void _showModalBottomSheet(BuildContext context) { showModalBottomSheet( context: context, builder: (BuildContext context) { return Container( height: 200, child: Center( child: Text('This is a modal bottom sheet.'), ), ); }, ); } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text('My App'), ), body: Center( child: ElevatedButton( onPressed: () { _showModalBottomSheet(context); }, child: Text('Show Modal Bottom Sheet'), ), ), ); }In this example, the
_showModalBottomSheet()method is called when theElevatedButtonis pressed. The method takes the currentcontextand passes it to theshowModalBottomSheetmethod. The parameter in the method returns a with some text in the center, which represents the contents of the modal bottom sheet.