在appbar上,我想使用图标返回到上一页。但是它一直说,参数类型'context‘不能分配给参数类型'BuildContext’。
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);
       }
    ),
    )
        
  );
}发布于 2022-04-09 08:17:01
因为在您的类中没有context声明。
将BuildContext context添加到所需的参数中。
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时,请记住将上下文添加为:
app_bar_parking(context, "MyTitle");发布于 2022-04-09 08:14:51
在函数中传递context。
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()内部使用
@override
 Widget build(BuildContext context) {
   return Scaffold(
     appBar: app_bar_parking('',context),
// rest of the code
   );
}发布于 2022-04-09 20:50:41
在函数参数中添加BuildContext context。基本上,你必须使用相同的上下文。
app_bar_parking ({String title = '',BuildContext context}) {}https://stackoverflow.com/questions/71806303
复制相似问题