我使用以下代码将焦点移到任何页面上。当我切换页面时,将再次执行didChangeDependencies和当前页面的构建。为什么?如何阻止它执行?
FocusScope.of(context).requestFocus(FocusNode());
Widget build(BuildContext context) {
print('build');
return Scaffold(
appBar: AppBar(),
body: GestureDetector(
child: Center(
child: Column(
children: <Widget>[
Container(
width: 100,
height: 100,
color: Colors.red,
child: Text('remove focus'),
),
GestureDetector(
child: Text('next page'),
onTap: () {
Navigator.push(context, MaterialPageRoute(builder: (_) {
return Apage();
}));
},
)
],
),
),
onTap: () {
FocusScope.of(context).requestFocus(FocusNode());
},
),
);
}
我有两个页面,A和B。我单击以触发页面A上的转移焦点。这时,dChangeChangeDependencies和页面A的构建将立即触发。然后跳转到页面B,然后返回到页面A,这时将再次执行didChangeDependencies和页面A的构建。如果在跳转之前未执行聚焦操作,则不会触发didChangeDependencies和构建。
我使用的Flutter版本是1.17.3