我们需要将onTap监听器添加到appBar . 不是 actions 或 leading 按钮 . 我们需要它适用于所有appBar . 我试图使用 InkWell ,但它有视觉效果,我们不需要它 . 我尝试使用 GestureDetector ,但仅当用户点击此 GestureDetector 中的文本时才有效 .
actions
leading
InkWell
GestureDetector
有几种方法可以做到这一点 . 我认为创建自定义应用栏是一种好方法
示例:
return Scaffold( appBar: CustomAppBar( appBar: AppBar(title: Text("hello"),), onTap: () { print("test"); }, ), body: Container(), );
要创建自定义应用栏,您需要实现 PreferredsizeWidget
PreferredsizeWidget
class CustomAppBar extends StatelessWidget implements PreferredSizeWidget { final VoidCallback onTap; final AppBar appBar; const CustomAppBar({Key key, this.onTap,this.appBar}) : super(key: key); @override Widget build(BuildContext context) { return GestureDetector(onTap: onTap,child: appBar); } // TODO: implement preferredSize @override Size get preferredSize => new Size.fromHeight(kToolbarHeight); }
1 回答
有几种方法可以做到这一点 . 我认为创建自定义应用栏是一种好方法
要创建自定义应用栏,您需要实现
PreferredsizeWidget