【问题标题】:Flutter : How to call a screen onclick on image of reusable appbarFlutter:如何在可重用应用栏的图像上点击屏幕
【发布时间】:2021-06-21 10:46:07
【问题描述】:

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



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()));

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

如果有人知道如何解决它,请提供帮助。

【问题讨论】:

  • 你试过通知父类吗?您可以将函数传递给 appbar 类,然后在点击时,它将调用父类上的函数。此功能导航到其他页面。您还可以查看无上下文导航文章medium.com/flutter-community/… 如果其中任何一项有帮助,请通知我
  • 可以用您的答案编辑我的代码吗?我不知道该怎么做

标签: image flutter appbar


【解决方案1】:

您可以在构建函数中访问context,这样就可以了, 在您的自定义 appbar 中创建一个函数变量,并像这样将其提供给 InkWell,

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

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

现在你在哪里使用 appbar 像这样传递那个函数,

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-04-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-06
    • 2020-12-12
    • 2019-03-02
    相关资源
    最近更新 更多