美文网首页
Flutter-InheritedWidget

Flutter-InheritedWidget

作者: 念_夕 | 来源:发表于2019-02-27 14:23 被阅读1次

    InheritedWidget简介

    InheritedWidget 是带有通知属性的widget,framework有对其做特殊处理,官方描述如下:
    Base class for widgets that efficiently propagate information down the tree.
    简单的说,我们可以利用inheritedWidget来实现响应式的UI界面构建。

    示例

    一:创建模型对象,管理数据

    class ALRootModel {
      ALRootModel({this.count});
      final int count;
    }
    

    二:创建InheritedWidget 类,分发消息

    class ALRootInheritWideget extends InheritedWidget {
      ALRootInheritWideget({Key key, this.child, this.model})
          : super(key: key, child: child);
    
      final Widget child;
      final ALRootModel model;
    
      //便利方法
      static ALRootInheritWideget of(BuildContext context) {
        return (context.inheritFromWidgetOfExactType(ALRootInheritWideget)
            as ALRootInheritWideget);
      }
    
      //消息更新,触发消息分发
      @override
      bool updateShouldNotify(ALRootInheritWideget oldWidget) {
        return this.model != oldWidget.model;
      }
    }
    

    三:使用InheritedWidget

    class _InheritWidegetTestPageState extends State<InheritWidegetTestPage> {
      ALRootModel _model;
      var _count = 1;
    
      @override
      void initState() {
        super.initState();
        _model = ALRootModel(count: _count);
      }
    
      void _didHitBtn() {
        setState(() {
          _count++;
          // 触发消息更新
          _model = ALRootModel(count: _count);
        });
      }
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
            appBar: AppBar(
              title: Text('ALRootInheritWideget'),
            ),
            body: ALRootInheritWideget(
              model: _model,
              child: Container(
                padding: EdgeInsets.all(32.0),
                child: Center(
                  child: Column(
                    children: <Widget>[
                      Text('我是计数器$_count'),
                      SizedBox(height: 30,),
                      FlatButton(
                        child: Text('hit me'),
                        color: Colors.greenAccent,
                        onPressed:_didHitBtn
                      ),
                      SizedBox(height: 30,),
                      ClassA()
                    ],
                  ),
                ),
              ),
            ));
      }
    }
    

    四:接收消息,并完成UI更新

    class ClassA extends StatefulWidget {
      _ClassAState createState() => _ClassAState();
    }
    
    class _ClassAState extends State<ClassA> {
      @override
      Widget build(BuildContext context) {
    
        ALRootModel model = ALRootInheritWideget.of(this.context).model;
    
        return Container(
          child: Container(
              color: Colors.greenAccent,
              alignment: Alignment.center,
              constraints: BoxConstraints(
                  maxWidth: double.infinity,
                  minWidth: 200.0,
                  maxHeight: 300,
                  minHeight: 200.0),
              child: Text('我也是计数器${model.count}')),
        );
      }
    }
    

    注意

    • InheritedWidget只能管理子widget消息分发,因此ALRootInheritWideget.of(this.context).model;只能在子widget中才能获取到实例对象。
    • InheritedWidget作为子widget的dependency,可以自子widget对应的生命周期方法中做自定义处理。

    相关文章

      网友评论

          本文标题:Flutter-InheritedWidget

          本文链接:https://www.haomeiwen.com/subject/tuxfuqtx.html