Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/flutter/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Flutter 了解颤振力学_Flutter - Fatal编程技术网

Flutter 了解颤振力学

Flutter 了解颤振力学,flutter,Flutter,在阅读了文档和状态生命周期之后,我仍然不确定didChangeDependencies是如何工作的 据我所知,它将在initState和InheritedWidget中的任何更改后触发,但这些更改是什么?我认为了解什么样的更改触发了didChangeDependencies,这一点很重要,这样我们就可以了解何时以及如何正确使用它 当flatter调用并返回true时,先前在build()中请求继承小部件的小部件会被调用的didChangeDependencies通知 updateShouldNo

在阅读了文档和
状态
生命周期之后,我仍然不确定
didChangeDependencies
是如何工作的

据我所知,它将在
initState
InheritedWidget
中的任何更改后触发,但这些更改是什么?我认为了解什么样的更改触发了
didChangeDependencies
,这一点很重要,这样我们就可以了解何时以及如何正确使用它

当flatter调用并返回
true
时,先前在
build()
中请求继承小部件的小部件会被调用的
didChangeDependencies
通知


updateShouldNotify
如果其状态自上次调用后发生更改,则应返回
true

didChangeDependencies()
当此[state]对象的依赖项更改时调用。 所以,确切地说,它是如何被调用的?根据上面的定义,它看起来像是在状态改变后被调用的,但是我们如何知道状态改变了呢? 例子: 下面的示例使用提供者状态管理机制从父窗口小部件更新子窗口小部件。提供程序有一个名为updateShouldNotify的属性,该属性决定是否更改状态。若返回true,则在ChildWidget类中只调用didChangeDependencies。 updateShouldNotify在内部默认情况下返回true,因为它知道状态已更改

为什么我们需要更新通知?

这是必要的,因为如果有人想在特定条件下更新状态。如果UI要求只显示偶数值,那么我们可以添加如下条件

updateShouldNotify: (oldValue, newValue) => newValue % 2 == 0,
代码片段:

class ParentWidget extends StatefulWidget {
  ParentWidget({Key key, this.title}) : super(key: key);
  final String title;
  @override
  _ParentWidgetState createState() => _ParentWidgetState();
}
class _ParentWidgetState extends State<ParentWidget> {
  int _counter = 0;
  void _incrementCounter() {
    setState(() {
      _counter++;
    });
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Life Cycle'),
      ),
      body: Provider.value(
        value: _counter,
        updateShouldNotify: (oldValue, newValue) => true,
        child: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              Text(
                'Press Fab button to increase counter:',
              ),
              ChildWidget()
            ],
          ),
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: Icon(Icons.add),
      ),
    );
  }
}
class ChildWidget extends StatefulWidget {
  @override
  _ChildWidgetState createState() => _ChildWidgetState();
}
class _ChildWidgetState extends State<ChildWidget> {
  int _counter = 0;
  @override
  void initState() {
    print('initState(), counter = $_counter');
    super.initState();
  }
  @override
  void didChangeDependencies() {
    _counter = Provider.of<int>(context);
    print('didChangeDependencies(), counter = $_counter');
    super.didChangeDependencies();
  }
  @override
  Widget build(BuildContext context) {
    print('build(), counter = $_counter');
    return Text(
      '$_counter',
    );
  }
}
I/flutter ( 3779): didChangeDependencies(), counter = 1
I/flutter ( 3779): build(), counter = 1
有关更多信息:

class ParentWidget extends StatefulWidget {
  ParentWidget({Key key, this.title}) : super(key: key);
  final String title;
  @override
  _ParentWidgetState createState() => _ParentWidgetState();
}
class _ParentWidgetState extends State<ParentWidget> {
  int _counter = 0;
  void _incrementCounter() {
    setState(() {
      _counter++;
    });
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Life Cycle'),
      ),
      body: Provider.value(
        value: _counter,
        updateShouldNotify: (oldValue, newValue) => true,
        child: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              Text(
                'Press Fab button to increase counter:',
              ),
              ChildWidget()
            ],
          ),
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: Icon(Icons.add),
      ),
    );
  }
}
class ChildWidget extends StatefulWidget {
  @override
  _ChildWidgetState createState() => _ChildWidgetState();
}
class _ChildWidgetState extends State<ChildWidget> {
  int _counter = 0;
  @override
  void initState() {
    print('initState(), counter = $_counter');
    super.initState();
  }
  @override
  void didChangeDependencies() {
    _counter = Provider.of<int>(context);
    print('didChangeDependencies(), counter = $_counter');
    super.didChangeDependencies();
  }
  @override
  Widget build(BuildContext context) {
    print('build(), counter = $_counter');
    return Text(
      '$_counter',
    );
  }
}
I/flutter ( 3779): didChangeDependencies(), counter = 1
I/flutter ( 3779): build(), counter = 1