I am working on a project which uses PageView builder. I want to open screens from the onClick index, but it always starts from index 0. How can I display them with current index with swipe up left and right both direction?
Here is my code-
GridView.builder(
itemCount: snapshot.data!.data.length,
itemBuilder: (BuildContext context, int index) {
return GestureDetector(
onTap: () {
showDialog(
context: context,
builder: (
BuildContext context) {
return PageView.builder(
controller: _pageController,
itemCount: snapshot.data!.data.length,
scrollDirection: Axis.horizontal,
reverse: false,
//onPageChanged: (page) => setState(() => _activeImageIndex = page),
itemBuilder: (BuildContext context, int index) {
return Center(
child: Padding(
padding: EdgeInsets.symmetric(horizontal: 4.0),
child: Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(10),
color: Colors.white,
),
height: 300.h,
width: 1.sw,
child: Image.network(snapshot.data!.data[index].thumb, fit: BoxFit.cover,),
),
),
);
},
);
});
},
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
Container(
height: 150.h,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(10.r),
image: DecorationImage(
image: NetworkImage(
snapshot.data!.data[index].thumb.toString()
),
fit: BoxFit.cover
),
),
),
Container(
padding: EdgeInsets.symmetric(vertical: 3.h, horizontal: 2.w),
child: Align(alignment: Alignment.center,child: Text(snapshot.data!.data[index].prodName,
style: TextStyle(color: kblue, fontSize: 15.sp, fontWeight: FontWeight.w400),),)
)
]),
);
},
);
2
Answers
I think in the dialog, you should wrap
StatefulBuilder
to show changes usingsetState
. Here you can see example.Create a new Stateful Page
CustomPageView
with two variables intindex
and ListimageList
and make them required inside contructor:and in
showDialog
simply import and returnCustomPageView
. also, pass the required parameters.hopefully it will work