【发布时间】:2020-06-02 14:39:06
【问题描述】:
我需要对setState() 函数进行一些说明。
我将问题分为 3 个,但都是关于 setState()。
- 当调用
setState时,应用程序或 上下文(?)被重新渲染/更新? -
setState(() {/* MY CODE */});中的代码对/* myCode;*/ setState(() {});的重要性是什么? - 如果我要更新
setState函数中的一些变量 喜欢:setState(() { myText='dfg';})和这个变量 更新一个小部件,Text(myText),setState怎么会知道更新 文本小部件?
例如下面的经典代码:
return Scaffold(
appBar: AppBar(
title: Text('Sample Code'),
),
body: Center(
child: Text('You have pressed the button $_count times.'),
),
bottomNavigationBar: BottomAppBar(
shape: const CircularNotchedRectangle(),
child: Container(
height: 50.0,
),
),
floatingActionButton: FloatingActionButton(
onPressed: () => setState(() {
_count++;
}),
tooltip: 'Increment Counter',
child: Icon(Icons.add),
),
floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
);
}
}
在bottomNavigationBar小部件中调用了setState()(所以这个小部件被标记为脏),那么为什么我们可以在与child...body相关的Text小部件中看到更新
【问题讨论】:
标签: flutter