zoukankan      html  css  js  c++  java
  • Flutter: ValueListenableBuilder 内容与ValueListenable保持"同步"的窗口小部件

    使用这个修改状态可以不用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,
          ),
        );
      }
    }
    
  • 相关阅读:
    [python第七课]字符串和常用数据结构
    深浅拷贝与循环引用问题
    CSS居中总结
    CSS布局总结
    跨域
    函数节流与防抖
    浏览器渲染原理及渲染阻塞
    进程与线程
    前端之网络攻击
    前端之缓存
  • 原文地址:https://www.cnblogs.com/ajanuw/p/10924548.html
Copyright © 2011-2022 走看看