I was following a few tutorials and followed exactly to the tee, to create a bottom sheet, but it just doesn’t open.
The only message i get is ‘I/flutter ( 6593): Another exception was thrown: No MaterialLocalizations found’ in the debug console
Here’s the code:
import 'package:flutter/material.dart';
void main() => runApp(const MyApp());
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
Widget buildButtonSheet(BuildContext context) {
return Container(
color: Colors.white,
height: 300,
child: Center(
child: Text('Sheet Opened'),
),
);
}
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Bottom Sheet Example',
home: Scaffold(
backgroundColor: Colors.black,
appBar: AppBar(
title: const Text('Bottom Sheet Example'),
),
body:
Center(
child: ElevatedButton(
child: const Text('Bottom Sheet'),
onPressed: () {
showModalBottomSheet(
context: context,
builder: buildButtonSheet);
},
),
),
),
);
}
}
I tried one of the solutions posted before (showModalBottomSheet() not opening a bottom modal in flutter)but didn’t work.
I tried out using different emulators as ChatGPT suggested also, but didn’t work.
2
Answers
Your
context
, doesn’t yet have aMaterialLocalizations
widget in the widget tree, check this answer. So this means that you need ‘new’ context, and you have two possible solutions. You either move your body into new widget, or you can wrap body withBuilder
widget.Solution with Builder widget:
Solution with NewWidget:
create a function such as showPersonalData(BuildContext context) outside of the build:
Now connect the function on the elevetatedButton’s onPressed:
hope this will work for you.