0

我有一个带有操作按钮的应用栏的外部脚手架,单击该按钮时我想将内部 Stateful 小部件的状态保存到 perm 存储 - 能够从外部应用栏调用内部小部件方法的最佳方法是什么并且还可以在被调用的方法中执行 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(....)
  }
}

4

2 回答 2

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;
      }
    }
于 2020-02-28T11:05:43.910 回答
0

这就是我最终得到的结果,它允许我从应用栏按钮保存设置并显示一个快餐栏。在另一个带有选项卡视图的组件中,我提升了状态并将 State 小部件传递给 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),
    );
  }
}

于 2020-02-29T11:46:51.733 回答