我有一个主类和TimePickerScreen类,我试图从TimePickerScreen类获取值到主类以填充这些值,我用 GestureDetector 包装From和To Text 以在bottomSheet中调用TimePickerScreen类,然后选择时间并点击保存按钮值应该填充选择时间,但我不知道如何获取这些值,下面我粘贴了我的代码和屏幕截图,谁能帮助我,提前致谢。 主班
import 'package:flutter/material.dart';
import 'package:single_selection_horizontal/timedatepicker_screen.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: "custom time picker horizontal",
home: SelectTimeDate(),
);
}
}
class SelectTimeDate extends StatefulWidget {
@override
_SelectTimeDateState createState() => _SelectTimeDateState();
}
class _SelectTimeDateState extends State<SelectTimeDate> {
String pfromTime ='';
String ptoTime ='';
//String fromDate='';
//String toDate='';
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('custom time picker horizontal'),
),
body: Center(
child: Container(
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.center,
children: [
GestureDetector(
onTap: pickerBottomSheet,
child: Container(
child: Column(
children: [
Text(" From ",style: TextStyle(fontSize: 20,color: Colors.black),),
Text(
pfromTime==''
? "Select Time"
: "$pfromTime",style: TextStyle(fontSize: 15,color: Colors.grey),),
],
),
),
),
SizedBox(height: 10,),
GestureDetector(
onTap: pickerBottomSheet,
child: Container(
child: Column(
children: [
Text(" To ",style: TextStyle(fontSize: 20,color: Colors.black),),
Text(
ptoTime==''
? "Select Time"
: "$ptoTime",style: TextStyle(fontSize: 15,color: Colors.grey),),
],
),
),
),
],
),
),
),
);
}
pickerBottomSheet(){
showModalBottomSheet(
context: context,
isScrollControlled: true,
isDismissible: true,
builder: (BuildContext context){
return TimePickerScreen();
}
);
}
}
时间选择器屏幕
import 'package:flutter/material.dart';
class TimePickerScreen extends StatefulWidget {
@override
_TimePickerScreenState createState() => _TimePickerScreenState();
}
class _TimePickerScreenState extends State<TimePickerScreen> {
String selectedFromTime = " ";
List<String> fromTimeList = ["0:00", "0:30", "1:00", "1:30", "2:00", "2:30", "3:00", "3:30", "4:00", "4:30", "5:00", "5:30", "6:00", "6:30"];
List<bool> fromTimeListSelect =[false,false,false,false,false,false,false,false,false,false,false,false,false,false,];
String selectedToTime =" ";
List<String> toTimeList = ["0:00", "0:30", "1:00", "1:30", "2:00", "2:30", "3:00", "3:30", "4:00", "4:30", "5:00", "5:30", "6:00", "6:30"];
List<bool> toTimeListSelect =[false,false,false,false,false,false,false,false,false,false,false,false,false,false,];
@override
Widget build(BuildContext context) {
return Container(
height: MediaQuery.of(context).size.height * 0.80,
decoration: BoxDecoration(
borderRadius: BorderRadius.only(
topLeft: Radius.circular(10),
topRight: Radius.circular(10),
)
),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Container(
padding: EdgeInsets.fromLTRB(50, 50, 50, 50),
child: Text("Time Picker",style: TextStyle(fontSize: 40,color: Colors.purple),)),
Divider(),
Text("Select From Time",style: TextStyle(fontSize: 20,color: Colors.purple),),
fromTime(),
Divider(),
Text("Select To Time",style: TextStyle(fontSize: 20,color: Colors.purple),),
toTime(),
FlatButton(onPressed: (){
},
color: Colors.purple,
padding: EdgeInsets.fromLTRB(15, 15, 15, 15),
child: Text("Save",style: TextStyle(fontSize: 20,color: Colors.white),)),
SizedBox(height: 100,),
Text('From : $selectedFromTime To : $selectedToTime'),
],
),
);
}
Widget fromTime(){
return Expanded(
child: ListView.builder(
shrinkWrap: true,
scrollDirection: Axis.horizontal,
itemCount: fromTimeList.length,
itemBuilder: (BuildContext context,int index){
return GestureDetector(
child: Container(
padding: EdgeInsets.all(6),
child: Center(
child: Text(fromTimeList[index],
style: TextStyle(color: fromTimeListSelect[index] ? Colors.white : Colors.black ,fontSize: 16),)),
decoration: BoxDecoration(
shape: BoxShape.circle,
color: fromTimeListSelect[index] ? Colors.purple : Colors.white,
),
),
onTap: (){
setState(() {
for(int i=0; i< fromTimeListSelect.length; i++){
fromTimeListSelect[i] = false;
}
fromTimeListSelect[index] = !fromTimeListSelect[index];
fromTimeListSelect[index] == true ? selectedFromTime =fromTimeList[index] : selectedFromTime= ' ';
print(fromTimeListSelect[index]);
print(fromTimeList[index]);
});
},
);
},),
);
}
Widget toTime(){
return Expanded(
child: ListView.builder(
shrinkWrap: true,
scrollDirection: Axis.horizontal,
itemCount: fromTimeList.length,
itemBuilder: (BuildContext context,int index){
return GestureDetector(
child: Container(
padding: EdgeInsets.all(6),
child: Center(
child: Text(toTimeList[index],
style: TextStyle(color: toTimeListSelect[index] ? Colors.white : Colors.black ,fontSize: 16),)),
decoration: BoxDecoration(
shape: BoxShape.circle,
color: toTimeListSelect[index] ? Colors.purple : Colors.white,
),
),
onTap: (){
setState(() {
for(int i=0; i< toTimeListSelect.length;i++) {
toTimeListSelect[i] = false;
}
toTimeListSelect[index] = !toTimeListSelect[index];
toTimeListSelect[index] == true ? selectedToTime =toTimeList[index] : selectedToTime= ' ';
print(toTimeListSelect[index]);
print(toTimeList[index]);
});
},
);
},),
);
}
}