Damian Hamill
Damian Hamill

Reputation: 103

save state from outer widget

I have an outer scaffold with an app bar with an action button that when clicked I want to save the state of an inner Stateful widget to perm storage - what is the best way to be able to call the inner widget method from the outer app bar and also be able to do Scaffold.of(context) in the called method ?

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(....)
  }
}

Upvotes: 0

Views: 92

Answers (2)

Damian Hamill
Damian Hamill

Reputation: 103

This is what I ended up with which allows me to save the settings from the app bar button and show a snackbar. In another component with tabbed view I lifted up the state and passed the State widget into TabBarView children widgets so they can update the state.

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),
    );
  }
}

Upvotes: 0

Chirag Rathod
Chirag Rathod

Reputation: 167

You have to make outer class stateful and an inner class stateless or stateful that outer class setState() method reflect in an inner class. below I provide your example with some modification.

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;
      }
    }

Upvotes: 1

Related Questions