添加导航路由时报错(error: Undefined name 'context')创建自定义AppBar 下面是自定义小部件
Widget customAppBarTest(String title, bool backButton, bool home, bool about) {
return AppBar(
centerTitle: true,
title: Center(
child: Text(title),
),
automaticallyImplyLeading: false,
leading: GestureDetector(
onTap: () {
print('Go previous screen');
Navigator.pop(context);
//Navigator.of(context).pop();
},
child: const Icon(Icons.arrow_back)),
actions: [
GestureDetector(
onTap: () {
print('Go home');
/* Navigator.push(
context,
MaterialPageRoute(
builder: (context) => const HomeMenu(),
),
);*/
},
child: const Icon(Icons.home))
],
);
}
我不使用默认的 AppBar 没有问题,但需要使用 customAppBarTest 来简化代码管理
您应该在该函数中添加
BuildContext context
参数
Widget customAppBarTest(BuildContext context, String title, bool backButton, bool home, bool about) {
return AppBar(
centerTitle: true,
title: Center(
child: Text(title),
),
automaticallyImplyLeading: false,
leading: GestureDetector(
onTap: () {
print('Go previous screen');
Navigator.pop(context);
//Navigator.of(context).pop();
},
child: const Icon(Icons.arrow_back)),
actions: [
GestureDetector(
onTap: () {
print('Go home');
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => const HomeMenu(),
),
);
},
child: const Icon(Icons.home))
],
);
}