Flutter:如何在可重用应用栏的图像上调用屏幕 onclick

Flutter : How to call a screen onclick on image of reusable appbar

我创建了一个可重复使用的 appBar class 并在我想使用的任何地方调用它 这是代码



class MyAppBar extends AppBar {
  
 MyAppBar({Key key,Widget title})
   :super(key:key ,title:title,actions: <Widget>[
     InkWell(
        onTap: () {
          Profile();
            
        },
        child: Padding(
            padding: const EdgeInsets.all(14),
            child: ClipOval(
            
                child: Image.network(
                    'https://googleflutter.com/sample_image.jpg',
                ),
            ),
        ),
    ),
  
   ]
   );}


当点击图片时,它不会重定向到个人资料屏幕。 我也用过这个

Navigator.push(context, MaterialPageRoute(builder: (context)=>Profile()));

但它在上下文中给我错误,因为它没有定义。

如果有人知道如何修复它,请帮助。

你可以在构建函数中访问 context 所以这样做, 在您的自定义应用栏中创建一个函数变量,然后像这样将其提供给 InkWell,

class MyAppBar extends AppBar {
final Function onPressed;
...

InkWell(
onTap: onPressed,
child:...
   )
 }

现在,在您使用应用栏的地方,像这样传递该功能,

Scaffold(
appBar : MyAppBar(
onPressed: (){
Navigator.push(context, MaterialPageRoute(builder: (context)=>Profile()));
   },
title: ...
 ),
);