【问题标题】:save state from outer widget从外部小部件保存状态
【发布时间】:2020-02-28 10:33:47
【问题描述】:

我有一个外部脚手架,带有一个带有操作按钮的应用栏外部应用栏,也可以在被调用的方法中做 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(....)
  }
}

【问题讨论】:

    标签: flutter flutter-state


    【解决方案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;
          }
        }
    

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-06-10
        • 2021-06-11
        • 1970-01-01
        • 2017-03-19
        • 2018-09-01
        • 2020-10-19
        相关资源
        最近更新 更多