如何解决时间选择器无法正常工作的问题
当我选择应该在字段中自动输入的时间时,时间选择器不工作的问题
代码:https://codeshare.io/WddQvl
onTap: () {
showTimePicker(context: context,initialTime: TimeOfDay.Now()
).then((dynamic value) {
timeController.text = value.format(context);
print(value.format(context).toString());
});
},
图片 1:
图片 2:
解决方法
希望你错过了SetState()
。并且您忘记在 defaultFormField
onPressed: () {
showTimePicker(context: context,initialTime: TimeOfDay.now())
.then((dynamic value) {
setState(() {
_controller.text = value.format(context);
});
print(value.format(context).toString());
});
import 'package:flutter/material.dart';
Widget defaultFormField({
required TextEditingController controller,required TextInputType type,Function? validate,required dynamic label,required IconData prefix,IconData? suffix,bool isPassword = false,Function? suffixPressed,VoidCallback? onTap,Color colorField = Colors.black54,}) =>
TextFormField(
controller: controller,keyboardType: type,obscureText: isPassword,onTap: onTap,validator: (value) {
return validate!(value);
},decoration: InputDecoration(
hintText: label,border: OutlineInputBorder(),focusedBorder:
OutlineInputBorder(borderSide: BorderSide(color: colorField)),prefixIcon: Icon(
prefix,color: colorField,),suffixIcon: IconButton(
onPressed: () {
suffixPressed!();
},icon: Icon(suffix),);
class HomeLayout extends StatefulWidget {
@override
_HomeLayoutState createState() => _HomeLayoutState();
}
class _HomeLayoutState extends State<HomeLayout> {
int currentIndex = 0;
List<Widget> screens = [
NewTasksScreen(),// DoneTasksScreen(),// ArchivedTasksScreen(),];
List<String> titles = ['New Tasks','Done Tasks','Archived Tasks'];
// late Database database;
var scaffoldKey = GlobalKey<ScaffoldState>();
bool isBottomSheetShown = false;
IconData febIcon = Icons.edit;
var titleController = TextEditingController();
final timeController = TextEditingController();
var backgroundFloatBottom = Colors.deepPurpleAccent;
String toolTip = 'Add';
@override
void initState() {
// TODO : DATABASE SQFLITE
super.initState();
// createDatabase();
}
@override
void dispose() {
timeController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
key: scaffoldKey,appBar: AppBar(
title: Text(titles[currentIndex]),backgroundColor: Colors.deepPurpleAccent,elevation: 5,shadowColor: Colors.black,body: screens[currentIndex],floatingActionButton: FloatingActionButton(
backgroundColor: backgroundFloatBottom,tooltip: toolTip,onPressed: () {
if (isBottomSheetShown) {
Navigator.pop(context);
isBottomSheetShown = false;
setState(() {
febIcon = Icons.edit;
toolTip = 'Add';
});
} else {
scaffoldKey.currentState!.showBottomSheet((context) => Container(
color: Colors.grey[50],padding: EdgeInsets.all(20),child: Column(
mainAxisSize: MainAxisSize.min,children: [
defaultFormField(
controller: titleController,type: TextInputType.text,label: 'Task Title',prefix: Icons.title,colorField: Colors.deepPurpleAccent,validate: (String value) {
if (value.isEmpty) {
return 'Title Must Not Be Empty';
}
return null;
}),SizedBox(height: 15),defaultFormField(
controller: timeController,type: TextInputType.datetime,label: 'Task Time',prefix: Icons.watch_later_outlined,onTap: () async {
await showTimePicker(
context: context,initialTime: TimeOfDay.now())
.then((dynamic value) {
setState(() {
timeController.text = value.format(context);
});
print(value.format(context).toString());
});
},validate: (String value) {
if (value.isEmpty) {
return 'Time Must Not Be Empty';
}
return null;
})
],));
isBottomSheetShown = true;
setState(() {
febIcon = Icons.close;
backgroundFloatBottom = Colors.deepPurpleAccent;
toolTip = 'Close';
});
}
},child: Icon(febIcon),hoverColor: Colors.deepPurple,bottomNavigationBar: BottomNavigationBar(
type: BottomNavigationBarType.fixed,selectedItemColor: Colors.deepPurpleAccent,currentIndex: currentIndex,onTap: (index) {
setState(() {
currentIndex = index;
});
},items: [
BottomNavigationBarItem(
icon: Icon(Icons.menu),label: 'Tasks',BottomNavigationBarItem(
icon: Icon(Icons.done_all),label: 'Done',BottomNavigationBarItem(
icon: Icon(Icons.archive_outlined),label: 'Archived',],);
}
}
class NewTasksScreen extends StatefulWidget {
NewTasksScreen({Key? key}) : super(key: key);
@override
_NewTasksScreenState createState() => _NewTasksScreenState();
}
class _NewTasksScreenState extends State<NewTasksScreen> {
@override
Widget build(BuildContext context) {
return Container(
child: Text("NewTasksScreen"),);
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。