在Flutter内部机制中,默认使用自动管理导航机制,该机制在Flutter与原生混和开发情况下,Flutter页面不一定作为项目的首页面,所以出现需要在首个Flutter页面使用导航返回的需求。
解决方案:
Flutter的AppBar中定义有属性:
final bool automaticallyImplyLeading;
该属性默认为YES,即默认为自动管理导航栏,该情况下其会在非第一个Flutter页面创建导航返回按钮,我们在AppBar中将其设置为false:
automaticallyImplyLeading: false,
并且手动添加导航返回按钮:
appBar: AppBar(
title: Text('MixStack'),
centerTitle: true,
automaticallyImplyLeading: false,
leading: Builder(
builder: (context) {
return IconButton(icon: Icon(Icons.arrow_back_ios), onPressed: () => router.pop(context));
},
),
),
完成