这是因为dialog的context错误了,在main.dart中的main函数中需要新建一个MaterialApp对象,该对象的context才是dialog正确的context
void main() {
runApp(MaterialApp(home: MyApp()));
}
void main() {
runApp(MaterialApp(home: MyApp()));
}
class MyApp extends StatefulWidget {
....
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('List'),
),
body: RaisedButton(
child: Text('Show Material Dialog'),
onPressed: _showMaterialDialog,
),
);
}
_showMaterialDialog() {
showDialog(
context: context,
builder: (_) => new AlertDialog(
title: new Text("Material Dialog"),