码迷,mamicode.com
首页 > 其他好文 > 详细

Flutter: ValueListenableBuilder 内容与ValueListenable保持"同步"的窗口小部件

时间:2019-05-26 00:35:21      阅读:544      评论:0      收藏:0      [点我收藏+]

标签:lis   value   htm   demo   isa   state   bar   app   窗口   

使用这个修改状态可以不用setState()。

class _MyHomeState extends State<MyHome> {

  final ValueNotifier<int> _counter = ValueNotifier<int>(0);
  final Widget goodJob = const Text('Good job!');

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Flutter Demo'),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            ValueListenableBuilder(
              builder: (BuildContext context, int value, Widget child) {
                return Row(
                  mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                  children: <Widget>[
                    child,
                    Text('$value'),
                    child,
                  ],
                );
              },
              valueListenable: _counter,
              child: goodJob,
            )
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        child: Icon(Icons.plus_one),
        onPressed: () => _counter.value += 1,
      ),
    );
  }
}

Flutter: ValueListenableBuilder 内容与ValueListenable保持"同步"的窗口小部件

标签:lis   value   htm   demo   isa   state   bar   app   窗口   

原文地址:https://www.cnblogs.com/ajanuw/p/10924548.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!