如何从一个 class 屏幕获取值到另一个 class 屏幕并在 flutter 中填充这些值?
How to get values from one class screen to another class and populate those values in flutter?
我有一个主 class 和 TimePickerScreen class,我正在尝试从 TimePickerScreen[=29= 获取值] class 到 Main class 来填充这些值,我包装了 From 和 To 文本与 GestureDetector 调用 TimePickerScreen class 在 bottomSheet 内和 selecting 时间和点击保存按钮值之后应该填充 select Time 但我不知道如何获取这些值,下面我粘贴了我的代码和屏幕截图,任何人都可以帮助我,在此先感谢.
主要Class
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();
}
);
}
}
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]);
});
},
);
},),
);
}
}
您可以定义一个 class 来从底部对结果建模 sheet :
class TimeSelectResult{
final String pFromTime;
final String pToTime;
TimeSelectResult(this.pFromTime, this.pToTime);
}
然后在底部的代码sheet中,当用户选择2次from和to时,你调用Navigator.of(context).pop(timeSelectResult)
,其中timeSelectResult
是你的[=14]的一个变量=] 填充了用户选择的结果。
所以你现在这样称呼底部 sheet:
pickerBottomSheet() async {
final TimeSelectResult? result = await showModalBottomSheet(
context: context,
isScrollControlled: true,
isDismissible: true,
builder: (BuildContext context) {
return TimePickerScreen();
});
if (result != null) {
setState(() {
pfromTime = result.pFromTime;
ptoTime = result.pToTime;
});
}
}
在 class 级别添加一个回调方法 TimePickerScreen
,例如
final Function callback;
并像
一样在Save button
上使用它
onPressed: () {
widget.callback(selectedFromTime, selectedToTime); //here
Navigator.of(context).pop();
},
并像
一样使用TimePickerScreen
return TimePickerScreen(
callback: (String from, String to) {
print("From $from TO $to");
setState(() {
pfromTime = from;
ptoTime = to;
});
},
);
making nullable String will be better choice in this, becasue its depend on user.
完整代码
import 'package:flutter/material.dart';
void main() => runApp(const MyApp());
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@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: const 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(
callback: (String from, String to) {
print("From $from TO $to");
},
);
});
}
}
class TimePickerScreen extends StatefulWidget {
final Function callback;
const TimePickerScreen({Key? key, required this.callback}) : super(key: key);
@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: const 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: () {
widget.callback(selectedFromTime, selectedToTime); //here
Navigator.of(context).pop();
},
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]);
});
},
);
},
),
);
}
}
我有一个主 class 和 TimePickerScreen class,我正在尝试从 TimePickerScreen[=29= 获取值] class 到 Main class 来填充这些值,我包装了 From 和 To 文本与 GestureDetector 调用 TimePickerScreen class 在 bottomSheet 内和 selecting 时间和点击保存按钮值之后应该填充 select Time 但我不知道如何获取这些值,下面我粘贴了我的代码和屏幕截图,任何人都可以帮助我,在此先感谢.
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();
}
);
}
}
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]);
});
},
);
},),
);
}
}
您可以定义一个 class 来从底部对结果建模 sheet :
class TimeSelectResult{
final String pFromTime;
final String pToTime;
TimeSelectResult(this.pFromTime, this.pToTime);
}
然后在底部的代码sheet中,当用户选择2次from和to时,你调用Navigator.of(context).pop(timeSelectResult)
,其中timeSelectResult
是你的[=14]的一个变量=] 填充了用户选择的结果。
所以你现在这样称呼底部 sheet:
pickerBottomSheet() async {
final TimeSelectResult? result = await showModalBottomSheet(
context: context,
isScrollControlled: true,
isDismissible: true,
builder: (BuildContext context) {
return TimePickerScreen();
});
if (result != null) {
setState(() {
pfromTime = result.pFromTime;
ptoTime = result.pToTime;
});
}
}
在 class 级别添加一个回调方法 TimePickerScreen
,例如
final Function callback;
并像
一样在Save button
上使用它
onPressed: () {
widget.callback(selectedFromTime, selectedToTime); //here
Navigator.of(context).pop();
},
并像
一样使用TimePickerScreen
return TimePickerScreen(
callback: (String from, String to) {
print("From $from TO $to");
setState(() {
pfromTime = from;
ptoTime = to;
});
},
);
making nullable String will be better choice in this, becasue its depend on user.
完整代码
import 'package:flutter/material.dart';
void main() => runApp(const MyApp());
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@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: const 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(
callback: (String from, String to) {
print("From $from TO $to");
},
);
});
}
}
class TimePickerScreen extends StatefulWidget {
final Function callback;
const TimePickerScreen({Key? key, required this.callback}) : super(key: key);
@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: const 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: () {
widget.callback(selectedFromTime, selectedToTime); //here
Navigator.of(context).pop();
},
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]);
});
},
);
},
),
);
}
}