简体   繁体   English

如何将索引从一个小部件传递到 flutter 中的另一个小部件

[英]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 ,如果您提供完整的代码,最好说明如何操作,如果GridViewPageView内,则可以直接访问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.

相关问题 将索引从 Carousel Widget 传递到另一个 Widget Flutter - Pass Index from Carousel Widget to Another Widget Flutter 如何将 void 函数从同一个小部件传递给另一个小部件,然后将相同的 void 函数从另一个小部件返回到同一个小部件中颤振 - how to pass the void function from same widget to another widget and then return the same void function from another widget to same widget in flutter 如何从 Flutter 中的另一个小部件滚动小部件 - How to scroll a widget from another widget in Flutter 如何将自定义小部件传递给 flutter 中的另一个自定义小部件? - How can I pass a custom widget into another custom widget in flutter? Flutter 如何将异步函数传递给另一个小部件? - Flutter how to pass async function to another widget? 我如何将 QuerySnapshot 数据从一个小部件传递到 flutter 中的另一个小部件? - How i can pass QuerySnapshot data from one widget to another widget in flutter? Flutter - 如何在搜索时将搜索文本从一个小部件传递到另一个 class 小部件? - Flutter - How to pass search text from one widget to its another class widget while searching? 我如何将数据从一个小部件传递到 flutter 中的另一个小部件? - How i can pass Data from one widget to another widget in flutter? 如何将 flutter textediting controller 值从一个无状态小部件传递到另一个无状态小部件 - How can I pass flutter textediting controller value from one stateless widget to another stateless widget 如何将数据从子状态小部件传递到 Flutter 中的父小部件 - How to pass data from a child Stateful widget to Parent Widget in Flutter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM