Flutter:自定义 Appbar 的导航路线

问题描述 投票:0回答:1

添加导航路由时报错(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 来简化代码管理

flutter appbar
1个回答
0
投票

您应该在该函数中添加

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))
    ],
  );
}
© www.soinside.com 2019 - 2024. All rights reserved.