How to Pass Inherited Widgets to Entire Material App

后端 未结 1 1933
北荒
北荒 2021-01-21 01:10

So i have an inherited widget that looks like :

class InheritedStateWidget extends StatefulWidget {
  final Widget child;

  InheritedStateWidget({
    @required         


        
相关标签:
1条回答
  • 2021-01-21 01:40

    I restructured your top few classes slightly:

    import 'package:flutter/material.dart';
    
    void main() => runApp(InheritedStateWidget());
    
    class InheritedStateWidget extends StatefulWidget {
      @override
      InheritedStateWidgetState createState() => InheritedStateWidgetState();
    }
    
    class InheritedStateWidgetState extends State<InheritedStateWidget> {
      String _userName;
    
      // Getter methods
      String get tasks => _userName;
    
      void changeUserName(String name) {
        setState(() {
          _userName = name;
        });
      }
    
      @override
      Widget build(BuildContext context) {
        return new MyInheritedWidget(
          data: this,
          child: MyApp(),
        );
      }
    }
    
    class MyInheritedWidget extends InheritedWidget {
      final InheritedStateWidgetState data;
    
      MyInheritedWidget({Key key, this.data, Widget child})
          : super(key: key, child: child);
    
      static MyInheritedWidget of(BuildContext context) =>
          context.inheritFromWidgetOfExactType(MyInheritedWidget);
    
      @override
      bool updateShouldNotify(MyInheritedWidget old) => true;
    }
    
    class MyApp extends StatelessWidget {
      // in MyApp and below you can refer to MyInheritedWidget.of(context).data
      @override
      Widget build(BuildContext context) {
        return new MaterialApp(
          title: 'App One',
          home: new MyHomePage(),
        );
      }
    }
    

    I prefer to strengthen the interface between the State and the InheritedWidget like this, but just a suggestion...

    void main() => runApp(new Controller());
    
    class Controller extends StatefulWidget {
      @override
      State<StatefulWidget> createState() => ControllerState();
    }
    
    typedef void VoidIntFunction(int i);
    
    class ControllerState extends State<Controller> {
      String someString = '';
    
      void someFunction(int v) {
        print('function called with $v');
      }
    
      @override
      Widget build(BuildContext context) {
        return StateContainer(
          someString: someString,
          someFunction: someFunction,
          child: new ExampleApp(),
        );
      }
    }
    
    class StateContainer extends InheritedWidget {
      final String someString;
      final VoidIntFunction someFunction;
    
      const StateContainer({
        this.someString,
        this.someFunction,
        Widget child,
      }) : super(child: child);
    
      static StateContainer of(BuildContext context) =>
          context.inheritFromWidgetOfExactType(StateContainer);
    
      @override
      bool updateShouldNotify(StateContainer oldWidget) => true;
    }
    

    Any child now using the InheritedWidget just has read access to the state (someString) and has to modify it by calling methods (someFunction). There's a bit more boilerplate involved.

    0 讨论(0)
提交回复
热议问题