Flutter 获取ListWheelScrollView中子级的值
我试图获取子文本小部件的值,或者即使有一种方法可以从填充文本小部件的列表中获取值。当您滚动并且滚轮停止时,我想提取selectedItem的值 由于使用无限滚动,我无法将selectedItem的值映射到列表Flutter 获取ListWheelScrollView中子级的值,flutter,dart,Flutter,Dart,我试图获取子文本小部件的值,或者即使有一种方法可以从填充文本小部件的列表中获取值。当您滚动并且滚轮停止时,我想提取selectedItem的值 由于使用无限滚动,我无法将selectedItem的值映射到列表 import 'package:flutter/material.dart'; void main() => runApp(MyApp()); class MyApp extends StatefulWidget { @override _MyAppState creat
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
FixedExtentScrollController fixedExtentScrollController =
new FixedExtentScrollController();
// I would like to get one of these values
List<String> numbers = [
'one',
'two',
'three',
'four',
'five',
'six',
'seven',
'eight',
'nine'
];
@override
Widget build(BuildContext context) {
return MaterialApp(
color: Colors.white,
home: Center(
child: Container(
height: 800.0,
width: 250.0,
child: ListWheelScrollView.useDelegate(
itemExtent: 100,
controller: fixedExtentScrollController,
physics: FixedExtentScrollPhysics(),
onSelectedItemChanged: (_) {
print(fixedExtentScrollController.selectedItem.);
},
childDelegate: ListWheelChildLoopingListDelegate(
children: <Widget>[
...numbers.map((String number) {
return Container(
height: 100.0,
color: Colors.green,
child: Center(
child: Text(
number, //Here is the child value I would like to get
style: TextStyle(
fontSize: 24.0,
color: Colors.black,
fontWeight: FontWeight.normal,
fontStyle: FontStyle.normal,
decoration: TextDecoration.none),
),
),
);
})
],
),
),
),
),
);
}
}
导入“包装:颤振/材料.省道”;
void main()=>runApp(MyApp());
类MyApp扩展了StatefulWidget{
@凌驾
_MyAppState createState()=>\u MyAppState();
}
类MyAppState扩展了状态{
FixedExtentScroll控制器FixedExtentScroll控制器=
新的FixedExtentScrollController();
//我想得到其中一个值
列表编号=[
"一",,
“两个”,
"三",,
"四",,
"五",,
“六”,
“七”,
"八",,
“九”
];
@凌驾
小部件构建(构建上下文){
返回材料PP(
颜色:颜色,白色,
首页:中(
子:容器(
高度:800.0,
宽度:250.0,
子项:ListWheelScrollView.useDelegate(
项目范围:100,
控制器:fixedExtentScrollController,
物理:FixedExtentScrollPhysics(),
onSelectedItemChanged:(){
打印(fixedExtentScrollController.selectedItem.);
},
childDelegate:ListWheelChildLoopingListDelegate(
儿童:[
…number.map((字符串编号){
返回容器(
高度:100.0,
颜色:颜色。绿色,
儿童:中心(
儿童:文本(
number,//这是我要获取的子值
样式:TextStyle(
字体大小:24.0,
颜色:颜色,黑色,
fontWeight:fontWeight.normal,
fontStyle:fontStyle.normal,
装饰:文本装饰。无),
),
),
);
})
],
),
),
),
),
);
}
}
检查,您可以使用onSelectedItemChanged
onSelectedItemChanged→ 价值改变
在中心项更改时调用的可选侦听器上
您正在查找的值可以通过将代码更改为:
onSelectedItemChanged: (i) {
print(numbers[i]);
},
我希望这有帮助 谢谢你,弗朗西斯科,问题是我有无限卷轴,所以无法映射值,无限卷轴同时给出无限的负值和正值。因此,如果我继续向下滚动,该值将继续上升,并且它将比我的列表更大。如果我删除无限卷轴,我可以做你的建议,但我没有实现我想要的用户体验。没关系,我看到你在那里做了什么,你刚刚解决了我的问题,我没有传递“我”,你是一个传奇你是受欢迎的。很抱歉,我不清楚是否通过考试,我会记住这一点。