[英]How to navigate with GestureDetector and SliverList in Flutter
How can I navigate using my list?如何使用我的列表进行导航?
My list我的列表
class myHome extends StatelessWidget {
List<paralies> beaches = [
paralies(title: "Afandou Beach", photos: "rv_afandou_a.jpg", intent: "b_afandou"),
paralies(title: "Agathi Beach", photos: "b_afandou_b.jpg", intent: "b_agathi")
];
paralies.dart瘫痪者.dart
class paralies {
String title; // Name
String photos; // Photos
String intent; // Pages
paralies({ this.title,this.photos,this.intent});
}
My onTap我的 onTap
SliverList(
delegate: SliverChildBuilderDelegate(
(context, index) => GestureDetector(onTap:(){
Navigator.of(context).push(MaterialPageRoute(builder: (BuildContext context){return beaches[index].intent;})
); //Material
}, //onTap
Full Code:完整代码:
class myHome extends StatelessWidget {
List<paralies> beaches = [
paralies(title: "Afandou Beach", photos: "rv_afandou_a.jpg", intent: "b_afandou"),
paralies(title: "Agathi Beach", photos: "b_afandou_b.jpg", intent: "b_agathi")
];
@override
Widget build(BuildContext context) {
return SafeArea(
child: Material(
child: CustomScrollView(
slivers: [
SliverPersistentHeader(
delegate: MySliverAppBar(expandedHeight: 200),
pinned: true,
),
SliverList(
delegate: SliverChildBuilderDelegate(
(context, index) => GestureDetector(onTap:(){
Navigator.of(context).push(MaterialPageRoute(builder: (BuildContext context){return beaches[index].intent;})
); //Material
}, //onTap
child: Card(
child: Padding(
padding: const EdgeInsets.fromLTRB(15.0,0,15.0,0),
child: Row(
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
Image.asset("assets/beaches/${beaches[index].photos}", width: 100.0,height:
100.0,),
SizedBox(width: 70.0),
Text(beaches[index].title,
style: TextStyle(
color: Colors.blue,
fontWeight: FontWeight.w800,
fontSize: 20,
), //TextStyle
), //Text
], //<Widget>[]
), //Row
), //Padding
), //Card
), //GestureDetector
childCount: beaches.length,
), //SliverChildBuilderDelegate
), //SliverList
], //Slivers
), //Custom ScrollView
), //Material
); //Safe Area
} //build
} //MyHome
SliverList( delegate: SliverChildBuilderDelegate( (context, index) => GestureDetector(onTap:(){ Navigator.of(context).push(MaterialPageRoute(builder: (BuildContext context){return beaches[index].intent;}) ); //Material }, /
From what I understand you are attempting to navigate using the specific index of the list, what you should do is something like this:据我了解,您正在尝试使用列表的特定索引进行导航,您应该做的是这样的:
Widget _intentToPage(String intent){
if(intent == "b_afandou"){
return B_AFANDOU_PAGE();
}
else if(intent == ...){
...
}
return Text("Page not found");
}
SliverList(
delegate: SliverChildBuilderDelegate(
(context, index) => GestureDetector(onTap:(){
Navigator.push(context, MaterialPageRoute(builder: (context) => _intentToPage(beaches[index].intent));
},
With B_AFANDOU_PAGE()
being the ctor of the specific page for that intent. B_AFANDOU_PAGE()
是该意图的特定页面的B_AFANDOU_PAGE()
。
Note: Code is not tested but I believe this approach should give you a basic idea to solve this.注意:代码未经测试,但我相信这种方法应该为您提供解决此问题的基本思路。
I just used an if我只是用了一个 if
SliverList(
delegate: SliverChildBuilderDelegate(
(context, index) => GestureDetector(
child: Card(
child: Padding(
padding: const EdgeInsets.fromLTRB(15.0,0,15.0,0),
child: Row(
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
Image.asset("assets/beaches/${beaches[index].photos}", width: 100.0,height:
100.0,),
SizedBox(width: 70.0),
Text(beaches[index].title,
style: TextStyle(
color: Colors.blue,
fontWeight: FontWeight.w800,
fontSize: 20,
), //TextStyle
), //Text
],
)
),
),
onTap:() {if (index==0)
Navigator.of(context).push(MaterialPageRoute(builder: (BuildContext context){return b_afandou();})
);
if (index==1)
Navigator.of(context).push(MaterialPageRoute(builder: (BuildContext context){return b_agathi();})
);
}
),
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.