Jacek,不确定您尝试了什么,但实现您想要的一种方法是使用您的对话框选择作为Navigator.push 的结果。这是一个简单的例子,
class MyApp extends StatefulWidget {
@override
MyAppState createState() => MyAppState();
}
class MyAppState extends State<MyApp> {
String text = 'Original text';
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(title: Text('Test'),),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Padding(
padding: const EdgeInsets.only(
left: 10.0, right: 10.0),
child: Column(children: <Widget>[
Text(text, style: TextStyle(fontSize: 18.0, fontWeight: FontWeight.bold)),
]))
])),
floatingActionButton: Builder( builder: (context) => FloatingActionButton(
child: Icon(Icons.refresh),
onPressed: () async {
update(context);
},
)),
),
);
}
update(BuildContext context) async {
final result = await Navigator.push(
context,
MaterialPageRoute(builder: (context) => Dialog()),
);
setState(() {
text= result;
});
}
}
class Dialog extends StatelessWidget {
String dropdownValue = 'Updated item 1';
@override
Widget build(BuildContext context) {
return AlertDialog(
title: new Text('Test dialog'),
content: DropdownButton<String>(
value: dropdownValue,
icon: Icon(Icons.arrow_downward),
iconSize: 24,
elevation: 16,
style: TextStyle(color: Colors.deepPurple),
underline: Container(
height: 2,
color: Colors.deepPurpleAccent,
),
onChanged: (String newValue) {
Navigator.pop(context, newValue);
},
items: <String>[
'Updated item 1',
'Updated item 2',
'Updated item 3',
'Updated item 4'
].map<DropdownMenuItem<String>>((String value) {
return DropdownMenuItem<String>(
value: value,
child: Text(value),
);
}).toList(),
),
actions: <Widget>[
// usually buttons at the bottom of the dialog
new FlatButton(
child: new Text('Cancel'),
onPressed: () {
Navigator.pop(context, true);
},
)
],
);
}
}
希望这一切顺利。