首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何返回到上一页

如何返回到上一页
EN

Stack Overflow用户
提问于 2022-04-09 08:09:36
回答 3查看 141关注 0票数 1

在appbar上,我想使用图标返回到上一页。但是它一直说,参数类型'context‘不能分配给参数类型'BuildContext’。

代码语言:javascript
运行
复制
AppBar app_bar_parking ({String title = ''}) { 

  return AppBar( 
    backgroundColor: Colors.white, 
    centerTitle: true,
    title: Text('Parking & Pay'), 
    elevation: 0,
    titleTextStyle: TextStyle(color: Colors.black, fontWeight: FontWeight.bold, fontSize: 20),
    leading: GestureDetector(  
      child: IconButton(  
      icon: Icon(Icons.arrow_back_ios_new_outlined, 
      size: 20, 
      color: Colors.lightBlue,),
      onPressed: () { 
        Navigator.pop(context);
       }
    ),
    )
        
  );
}
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2022-04-09 08:17:01

因为在您的类中没有context声明。

BuildContext context添加到所需的参数中。

代码语言:javascript
运行
复制
AppBar app_bar_parking(BuildContext context, {String title = ''}) {
  return AppBar(
      backgroundColor: Colors.white,
      centerTitle: true,
      title: Text('Parking & Pay'),
      elevation: 0,
      titleTextStyle:
          TextStyle(color: Colors.black, fontWeight: FontWeight.bold, fontSize: 20),
      leading: GestureDetector(
        child: IconButton(
            icon: Icon(
              Icons.arrow_back_ios_new_outlined,
              size: 20,
              color: Colors.lightBlue,
            ),
            onPressed: () {
              Navigator.pop(context);
            }),
      ));
}

当您使用app_bar_parking时,请记住将上下文添加为:

代码语言:javascript
运行
复制
app_bar_parking(context, "MyTitle");
票数 0
EN

Stack Overflow用户

发布于 2022-04-09 08:14:51

在函数中传递context

代码语言:javascript
运行
复制
AppBar app_bar_parking ({String title = '',BuildContext context}) { 

  return AppBar( 
    backgroundColor: Colors.white, 
    centerTitle: true,
    title: Text('Parking & Pay'), 
    elevation: 0,
    titleTextStyle: TextStyle(color: Colors.black, fontWeight: FontWeight.bold, fontSize: 20),
    leading: GestureDetector(  
      child: IconButton(  
      icon: Icon(Icons.arrow_back_ios_new_outlined, 
      size: 20, 
      color: Colors.lightBlue,),
      onPressed: () { 
        Navigator.pop(context);
       }
    ),
    )
        
  );
}

现在在build()内部使用

代码语言:javascript
运行
复制
@override
 Widget build(BuildContext context) {
   return Scaffold(
     appBar: app_bar_parking('',context),
// rest of the code
   );
}
票数 0
EN

Stack Overflow用户

发布于 2022-04-09 20:50:41

在函数参数中添加BuildContext context。基本上,你必须使用相同的上下文。

代码语言:javascript
运行
复制
app_bar_parking ({String title = '',BuildContext context}) {}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71806303

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档