【发布时间】:2021-04-25 22:40:36
【问题描述】:
我一直在尝试找到一种方法来在“Widget CardUI”中放置导航,因为我希望它在用户单击时将其重定向到另一个页面。但是,当我尝试放置通常的 Navigator.push(context, MaterialPageRoute(builder: (context)=>PageName()));" 时,它告诉我上下文未定义。
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
leading: IconButton(
icon: Icon(Icons.arrow_back, color: Colors.white),
onPressed: ()
{Navigator.pop(context);
Navigator.push(context, MaterialPageRoute(builder: (context)=>homepage()));}),
title: Text("Creator's Club"),
backgroundColor: Color(0xff2657ce),
elevation: 0,),
body: Container(
padding: EdgeInsets.all(20),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text('Visual Arts', style: TextStyle(
color: Color(0xff2657ce),
fontSize: 27,
),),
Text('Choose which course you want to study.', style: TextStyle(
color: Colors.black.withOpacity(0.6),
fontSize: 20
),),
SizedBox(height: 10),
Expanded(
child: SingleChildScrollView(
child: Column(
children: <Widget>[
coursesList.length == 0 ? Center(child: Text("Loading...", style: TextStyle(fontSize: 15),)): ListView.builder(
scrollDirection: Axis.vertical,
shrinkWrap: true,
physics: const NeverScrollableScrollPhysics(),
itemCount: coursesList.length,
itemBuilder: (_, index) {
return CardUI(coursesList[index].courseName, coursesList[index].teacher, coursesList[index].category);
}
)
]
),
),
),
]
)
)
);
}
}
Widget CardUI (String courseName, String teacher, String category){
return Card(
elevation: 1,
margin: EdgeInsets.all(5),
color: Color(0xffd3defa),
child: Container(
color: Colors.white,
margin: EdgeInsets.all(1),
padding: EdgeInsets.all(10),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Row(
children: <Widget>[
Container(
width: 50,
height: 50,
decoration: BoxDecoration(
color: Color(0xffd3defa),
borderRadius: BorderRadius.all(Radius.circular(17)),
),
child: IconButton(
icon: Icon(
Icons.star_border_rounded,
color: Color(0xff2657ce),
),
),
),
SizedBox(width: 15,),
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Container(
child: InkWell(
onTap: () {},
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(courseName,
style: TextStyle(fontSize: 18)),
SizedBox(height: 5),
Text(teacher, style: TextStyle(
fontSize: 15, color: Colors.grey)),
SizedBox(height: 5),
],
),
)
)
]
)
]
)
],
)
),
);
}
此外,有没有办法在 Inkwell onTap 中设置 if else 条件?在我的代码中,从实时数据库中显示了一堆课程,我只想点击其中一个。在我当前的代码中,Inkwell 适用于所有正在显示的卡片,但我只希望它在一张上。除了使用 Inkwell,还有其他选择吗?
【问题讨论】:
-
要么使用 StateFulWidget 要么将上下文从构建函数传递到卡片函数,它具有 onTap
-
@ॐRakeshKumar 嘿,成功了。谢谢!顺便说一句,你知道我是否可以点击一张课程名称为“ABCD”的卡片吗?
-
你可以在 onTap 上做这样的事情: (){ if(courseName=='ABCD'){ //按你的要求做 } else { } },
标签: firebase flutter firebase-realtime-database