简体   繁体   English

flutter:如何在ListWheelChildLoopingListDelegate 中获取所选项目的索引?

[英]flutter: how to get the index of the selected item in ListWheelChildLoopingListDelegate?

I have put a list of numbers(1-23) and a '-' in a ListWheelScrollView() to display time-selecting feature,(just in the following picture).我在ListWheelScrollView()放置了一个数字列表(1-23)和一个'-'来显示time-selecting功能,(如下图所示)。 I want add some feature in the item that is selected currently such as bold font, so my problem is how to get the selected item index ?我想在当前选择的项目中添加一些功能,例如粗体,所以我的问题是how to get the selected item index My code is below.我的代码如下。

new ListWheelScrollView.useDelegate(
                            itemExtent: 48,
                            physics: FixedExtentScrollPhysics(),
                            childDelegate: ListWheelChildLoopingListDelegate(
                              children: List<Widget>.generate(24, (index) {
                                if (index < 0 || index > 24) return null;
                                else if (index == 0) return Container(
                                  child: Text('-', style: CalendarTextStyle.timeStyle),
                                  alignment: Alignment.center,
                                );
                                else return Container(
                                  child: Text('${index}', style: CalendarTextStyle.timeStyle),
                                  alignment: Alignment.center,);
                              })
                            ),
                          ),

在此处输入图片说明

在此处输入图片说明

I have found a solution.我找到了解决办法。 use onSelectedItemChanged property in ListWheelScrollView.useDelegate , which can listen the item index when scolling.使用ListWheelScrollView.useDelegate onSelectedItemChanged属性,可以在ListWheelScrollView.useDelegate时监听项索引。

ListWheelScrollView.useDelegate(
                        onSelectedItemChanged: (index) {setState(() {seletedItem = index;});}, // I initialized selectedItem = 0 in `initState()`
                        itemExtent: 48,
                        squeeze: 1,
                        controller: fsc,
                        physics: FixedExtentScrollPhysics(),
                        childDelegate: ListWheelChildLoopingListDelegate(
                          children: List<Widget>.generate(24, (index) {
                            if (index < 0 || index > 24) return null;
                            else if (index == 0) {
                              return Container(
                                child: Text('-', style:
                              seletedItem == index ?
                              CalendarTextStyle.selectedTimeStyle : CalendarTextStyle.timeStyle),
                              alignment: Alignment.center,);}
                            else {
                              return Container(
                                child: Text('$index', style:
                                seletedItem == index ?
                                CalendarTextStyle.selectedTimeStyle : CalendarTextStyle.timeStyle),
                                alignment: Alignment.center,);}
                          })
                        ),
                      ),

then we can got the effort like this: we can change the style of the selected item.那么我们可以得到这样的努力:我们可以改变所选项目的样式。

在此处输入图片说明

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM