我有一个带有操作按钮的应用程序栏的外部支架,单击该按钮时,我想将内部有状态小部件的状态保存到烫发存储中-最好的方法是能够从内部调用内部小部件方法外部应用程序栏,还可以在调用的方法中执行Scaffold.of(context)吗?
class AISSettings extends StatelessWidget {
static const String route = 'settings/ais';
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('AIS settings'),
actions: <Widget>[
IconButton(icon: Icon(Icons.check), onPressed: () => {/* call _SettingsState.saveStuff() */} ),
],
),
bottomNavigationBar: Navbar(),
body: AISSettingsForm(),
);
}
}
class AISSettingsForm extends StatefulWidget {
@override
_SettingsState createState() => _SettingsState();
}
class _SettingsState extends State<AISSettingsForm> {
final _formKey = GlobalKey<_SettingsState>();
Map<String, dynamic> _options;
saveStuff() {
// Scaffold.of(context).showSnackBar(....)
}
}
答案 0 :(得分:1)
您必须使外部类成为有状态的,并使内部类成为无状态的,或者使外部类的setState()方法反映在内部类中。 下面,我对您的示例进行了一些修改。
class AISSettings extends StatefulWidget {
static const String route = 'settings/ais';
final _formKey = GlobalKey<_SettingsState>();
Map<String, dynamic> _options;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('AIS settings'),
actions: <Widget>[
IconButton(icon: Icon(Icons.check), onPressed: () => {/* call _SettingsState.saveStuff() */} ),
],
),
//bottomNavigationBar: Navbar(),
body: AISSettingsForm(),
);
}
@override
State<StatefulWidget> createState() {
// TODO: implement createState
return null;
}
}
class AISSettingsForm extends StatefulWidget {
@override
_SettingsState createState() => _SettingsState();
}
class _SettingsState extends State<AISSettingsForm> {
saveStuff() {
// Scaffold.of(context).showSnackBar(....)
}
@override
Widget build(BuildContext context) {
// TODO: implement build
return null;
}
}
答案 1 :(得分:0)
这就是我最终得到的结果,它使我可以从应用程序栏按钮保存设置并显示小吃栏。在另一个带有选项卡式视图的组件中,我抬起了状态并将状态小部件传递到TabBarView子级小部件中,以便它们可以更新状态。
class AISSettings extends StatefulWidget {
static const String route = 'settings/ais';
@override
_SettingsState createState() => _SettingsState();
}
class _SettingsState extends State<AISSettings> {
final _formKey = GlobalKey<_SettingsState>();
Map<String, dynamic> _options;
saveStuff(context) {
// do stuff to save the settings to perm storage....
Scaffold.of(context).showSnackBar(....)
}
setValue(key, value) {
setState(() => _options[key] = value);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('AIS settings'),
actions: <Widget>[
Builder(
builder: (context) => IconButton(
icon: Icon(Icons.check),
onPressed: () => {saveStuff(context)}),
)
],
),
bottomNavigationBar: Navbar(),
body: ListView(
children: [
StatelessWidget1(this, _options),
StatelessWidget2(this, _options),
StatelessWidget3(this, _options),
])
);
}
}
class StatelessWidget1 extends StatelessWidget {
final Map<String, dynamic> settings;
final _SettingsState parent;
StatelessWidget1(this.parent, this.settings);
@override
Widget build(BuildContext context) {
return Slider(
key: Key('variation'),
min: -20.0,
max: 20.0,
label: '${settings['variation']}\u00B0',
divisions: 40,
value: 0.0 + settings['variation']
onChanged: (val) => parent.setValue('variation', val),
);
}
}