首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >从外部小部件保存状态

从外部小部件保存状态
EN

Stack Overflow用户
提问于 2020-02-28 18:33:48
回答 2查看 36关注 0票数 0

我有一个外部脚手架和一个带有操作按钮的应用程序栏,当单击它时,我希望将内部有状态小部件的状态保存到perm存储中-什么是能够从外部应用程序栏调用内部小部件方法并能够在调用的方法中执行Scaffold.of(上下文)的最佳方法?

代码语言:javascript
运行
复制
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(....)
  }
}
EN

回答 2

Stack Overflow用户

发布于 2020-02-28 19:05:44

您必须使外部类是有状态的,而内部类是无状态的,或者是外部类setState()方法在内部类中反映的有状态的。下面我将提供一些修改后的示例。

代码语言:javascript
运行
复制
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
EN

Stack Overflow用户

发布于 2020-02-29 19:46:52

这就是我最终得到的,它允许我保存应用程序栏按钮中的设置,并显示一个快餐栏。在另一个带有选项卡式视图的组件中,我提升了状态,并将state小部件传递到TabBarView子小部件中,以便它们可以更新状态。

代码语言:javascript
运行
复制
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),
    );
  }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60449897

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档