So i have a list with images and i added the images with ${index+1} and added ontap but every image i tap on sends me to the same page and i want every image to send me to it’s own page that im gonna make for each one.
body: SafeArea(
child: Padding(
padding: EdgeInsets.symmetric(vertical: 30),
child: Column(children: [
Row(
children: [
Expanded(child: Container(
height: 200,
child: Center(
child: ListView.builder(
itemCount: 8,
scrollDirection: Axis.horizontal,
shrinkWrap: true,
itemBuilder: (BuildContext context, int index){
return GestureDetector(
onTap:(){
Navigator.push(context, MaterialPageRoute(
builder: (context)=>PostScreen(),
));
},
child: Center(
child: Container(
width: 160,
padding: EdgeInsets.all(20),
margin: EdgeInsets.only(left: 15),
decoration: BoxDecoration(
color: Colors.black,
borderRadius: BorderRadius.circular(15),
image: DecorationImage(
//city1.jpg
image: AssetImage("images/city${index+1}.jpg"),
fit: BoxFit.cover,
opacity: 0.7,
),
),
child: Column(
children: [
Container(
alignment: Alignment.topRight,
child: Icon(
Icons.bookmark_border_outlined,
color: Colors.white,
size: 30,
),
),
Spacer(),
Container(
alignment: Alignment.bottomLeft,
child: Text(
"city Name",
style: TextStyle(
color: Colors.white,
fontSize: 15,
fontWeight: FontWeight.w600,
)
)
),
],
),
),
),
);
} ),
),),
),
],
I tried to put the images separately without the index and it just broke the app
2
Answers
Right now you’ve the following code :
So when any image is clicked, it will redirect to PostScreen.
Now, if you want to make it redirect to screens as per image selection, you can use the index here.
I will show it using pushNamed() method.
You should make a list of screen names, each one per image and use the same index as image to navigate to that screen inside your GestureDetector.
For example, create a list as follows inside your widget :
List screens = [‘image_1_screen’, ‘image_2_screen’, ‘image_3_screen’];
Now, use it like :
So, it will redirect to relevant screen as per the image selected.
In short, make a list of the screens and use the same index as an image to navigate to that particular screen.
First of all, create a list of your image files. before your build
Now, make these changes in listView.builder:
Then In your PostScreen class you need to create a final String variable name assetPath and make it as required in its contructor. and in your PostScreen build return Image.asset(path). The code should look like this.
now, inside gesture detector you need to pass "assetPath":
So, lets just say. you click on the 3rd image its index will be 2. then it will pass the path of 2nd image from the list to your PostScreen().