我无法获取我的有状态小部件的状态以在带有列表轮滚动视图的提升按钮上显示更新的月份.useDelegate()


import 'package:flutter/material.dart';
import 'year_month_picker/states_month.dart';
import 'year_month_picker/widgets/wheeltile.dart';
class MyMonthPickerr extends StatefulWidget {
const MyMonthPickerr({Key? key}) : super(key: key);
@override
_MyMonthPickerrState createState() => _MyMonthPickerrState();
}
class _MyMonthPickerrState extends State<MyMonthPickerr> {
List<States_Month> states_month = [];
String currentState = [
'January',
'February',
'March',
'April',
"May",
"June",
"July",
'August',
"September",
"October",
'November',
'December'
][((DateTime.now().month)).toInt()];
@override
void initState() {
super.initState();
states_month = allStates();
}
@override
Widget build(BuildContext context) {
return Center(
child: ElevatedButton(
child: Text(
''), //Here is where I want to get the month when the user gets off the modal bottom sheet
onPressed: (() => showModalBottomSheet(
context: context,
builder: (BuildContext context) {
return Container(
color: Colors.blueGrey.shade800,
child: ListWheelScrollView.useDelegate(
itemExtent: 50,
controller: FixedExtentScrollController(
initialItem: ((DateTime.now().month).toInt() - 1),
),
perspective: 0.001,
diameterRatio: 1.6,
physics: FixedExtentScrollPhysics(),
squeeze: 1.0,
overAndUnderCenterOpacity: 0.2,
useMagnifier: true,
magnification: 1.3,
onSelectedItemChanged: (index) {
setState(() {
currentState = states_month[index].names!;
});
},
childDelegate: ListWheelChildBuilderDelegate(
childCount: states_month.length,
builder: (context, index) {
return WheelTile(
currentState == states_month[index].names
? Colors.white
: Colors.white60,
states_month[index].names!);
},
),
),
);
})),
),
);
}
}

你可以把这个小部件放在背景颜色为Colors.blueGrey.shade900的脚手架的主体中,看看我想做什么。

我需要显示我的应用程序项目的月份,我已经学习了几个星期了,所以如果有比我更有经验的人能帮助我,我将不胜感激!

您可以简化像这样的小部件

class MyMonthPickerr extends StatefulWidget {
const MyMonthPickerr({Key? key}) : super(key: key);
@override
_MyMonthPickerrState createState() => _MyMonthPickerrState();
}
class _MyMonthPickerrState extends State<MyMonthPickerr> {
final months = [
'January',
'February',
'March',
'April',
"May",
"June",
"July",
'August',
"September",
"October",
'November',
'December'
];
int selected = DateTime.now().month;
@override
Widget build(BuildContext context) {
return Center(
child: ElevatedButton(
child: Text(
'${months[selected]}'), //Here is where I want to get the month when the user gets off the modal bottom sheet
onPressed: () async {
await showModalBottomSheet(
context: context,
builder: (BuildContext context) {
return Container(
color: Colors.blueGrey.shade800,
child: ListWheelScrollView.useDelegate(
itemExtent: 50,
controller: FixedExtentScrollController(
initialItem: ((DateTime.now().month).toInt() - 1),
),
perspective: 0.001,
diameterRatio: 1.6,
physics: FixedExtentScrollPhysics(),
squeeze: 1.0,
overAndUnderCenterOpacity: 0.2,
useMagnifier: true,
magnification: 1.3,
onSelectedItemChanged: (index) {
selected = index;
setState(() {}); // onchange update the ui
},
childDelegate: ListWheelChildBuilderDelegate(
childCount: months.length,
builder: (context, index) {
return Container(
child: Text("${months[index]}"),
);
},
),
),
);
});
// setState(() {}); //or on closing the dialog
},
),
);
}
}

相关内容

  • 没有找到相关文章

最新更新