[英]How to pass the index from a widget to another widget in flutter
Hi i have Pageview with weekIndex
which show me the days of the week and I need to pass weekIndex
to another widget this is my code for the Pageview:嗨,我有带有
weekIndex
的 Pageview,它显示了一周中的几天,我需要将weekIndex
传递给另一个小部件,这是我的 Pageview 代码:
Expanded(
child: PageView.builder(
controller: _pageController,
itemBuilder: (context, weekIndex) {
return Column(
children: [
Expanded(
child: ListView.builder(
scrollDirection: Axis.horizontal,
itemCount: 7,
itemBuilder:
(BuildContext context, int index) {
DateTime now = DateTime.now();
int milliseconds = now
.millisecondsSinceEpoch -
(now.weekday - 1) *
24 *
60 *
60 *
1000 +
weekIndex * 7 * 24 * 60 * 60 * 1000 +
index * 24 * 60 * 60 * 1000;
and I need to pass weekIndex to this widget:我需要将 weekIndex 传递给这个小部件:
GridView.builder(
scrollDirection: Axis.vertical,
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 7,
),
itemCount: 20,
itemBuilder:
(BuildContext context, int index) {
DateTime now = DateTime.now();
int milliseconds = now
.millisecondsSinceEpoch -
(now.weekday - 1) *
24 *
60 *
60 *
1000 +
weekIndex * 7 * 24 * 60 * 60 * 1000 +// here I need to pass the weekIndex
index * 24 * 60 * 60 * 1000;
DateTime dayDateTime =
DateTime.fromMillisecondsSinceEpoch(
milliseconds);
int monthIndex = dayDateTime.month - 1;
take another variable int temp = 0
inside the PageView.builder
in the itemBuilder
set temp = weekIndex
, If you give the full code it would be better to say how to do it, if the GridView
is inside the PageView
then you can access weekIndex
directly.在
itemBuilder
中的PageView.builder
中取另一个变量int temp = 0
set temp = weekIndex
,如果您提供完整的代码,最好说明如何操作,如果GridView
在PageView
内,则可以直接访问weekIndex
. If it is outside its outside then declaring another variable will do the trick.如果它在其外部,那么声明另一个变量就可以了。
you can use inherited widget to update variables without setState您可以使用继承的小部件来更新没有 setState 的变量
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.