在写dialog的时候,发现了一个问题,就是 dialog不能直接写在martierapp()这个home属性下面,要把dialog的代码,直接踢出去写。
这个是我发现了,是martierapp的conetxt的问题。
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new MaterialApp(
title: 'Flutter Demo',
theme: new ThemeData(
primarySwatch: Colors.blue,
),
home: new MyHomePage(),----这个地方,不能直接写dialog的代码,把所有的内容都要提取出去写。
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key}) : super(key: key);
_MyHomePageState createState() => _MyHomePageState();
}
-----------------------------------dialog部分的代码
class _MyHomePageState extends State
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('dialog 数据的显示'),),
body: Container(
child: Column(
children:
new RaisedButton(
onPressed: () {
showMyDialog(context);
},
child: new Text("显示SimpleDialog,Material风格")),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: () {
showMyDialog(context);
},
tooltip: 'increment',
child: Icon(Icons.add),
),
);
}
void showMySimpleDialog(BuildContext context) {
showDialog(
context: context,
builder: (context) {
return new SimpleDialog(
title: new Text("SimpleDialog"),
children:
new SimpleDialogOption(
child: new Text("SimpleDialogOption One"),
onPressed: () {
Navigator.of(context).pop("SimpleDialogOption One");
},
),
new SimpleDialogOption(
child: new Text("SimpleDialogOption Two"),
onPressed: () {
Navigator.of(context).pop("SimpleDialogOption Two");
},
),
new SimpleDialogOption(
child: new Text("SimpleDialogOption Three"),
onPressed: () {
Navigator.of(context).pop("SimpleDialogOption Three");
},
),
],
);
});
}
void showMyDialog(BuildContext context) {
showDialog
context: context,
builder: (BuildContext context) {
return AlertDialog(
content: const Text('message'),
actions:
FlatButton(
child: const Text('ok'),
onPressed: () {
Navigator.of(context).pop(true);
},
)
],
);
});
}
}