Flutter-InheritedWidget

2019-02-27  本文已影响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}')),
    );
  }
}

注意

上一篇下一篇

猜你喜欢

热点阅读