flutter_in_action_2nd
flutter_in_action_2nd copied to clipboard
7.2.2 缺少一种case
注意,如果将上面示例中ShareDataWidget.of()方法实现改成调用getElementForInheritedWidgetOfExactType(),运行示例后,点击"Increment"按钮,会发现__TestWidgetState的didChangeDependencies()方法确实不会再被调用,但是其build()仍然会被调用
少写一种case:当_TestWidget被const修饰时,点击按钮其build方法并不会调用
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const _TestWidget(),
TextButton(
onPressed: () {
setState(() {
++count;
});
},
child: const Text('increment'),
),
],
),
我这里使用了空安全特性,dart会推荐 使用const 修饰构造器方法,在初始化的时候也会推荐使用const,不然会有会有波浪号提示(强迫症看不惯😂)