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!);
},
),
),
);
})),
),
);
}
}
您可以將此小部件放在帶有 backgroundcolor 的 Scaffold 主體中:Colors.blueGrey.shade900 以查看我想要做什么。
我需要顯示我的應用專案的月份,我已經學習了幾個星期的顫振,所以如果有人比我做更多的實驗可以幫助我,那將非常感激!
uj5u.com熱心網友回復:
您可以簡化小部件,例如
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
},
),
);
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/512083.html
