我想给这个小部件中的局部变量分配一个Date值,并显示在文本框上



这是我的代码,我想通过使用showDatepicker获取日期来将k值设置为字符串我无法得到k值并显示提示文本。它与之前的值相同,没有变化。它与"值"相同,我使用了then方法并设置了值,但没有工作

Widget fillBoxDate(String text,BuildContext context) {
String k ='';
return  Column(
Container(
child: GestureDetector(
onTap: (){
showDatePicker(
context:context ,
initialDate: DateTime.now(),
firstDate: DateTime(2001),
lastDate: DateTime(2200),
).then((value) => {
k = value.toIso8601String().split('T').first
});
},
child: Container(
// height:40.h,
height: MediaQuery.of(context).size.height*0.050,
child: DropdownButton(
icon: Container(
alignment: Alignment.centerLeft,
// color: Colors.blueGrey,
padding: EdgeInsets.zero,
margin: EdgeInsets.only(left: MediaQuery.of(context).size.width*0.25),
child: IconButton(
icon:Icon(Icons.arrow_drop_down,size:25.w,),
color: Colors.black87,
padding: EdgeInsets.all(0),
constraints: BoxConstraints(),
onPressed: (){
print("Hello");
showDatePicker(
context:context ,
initialDate: DateTime.now(),
firstDate: DateTime(2001),
lastDate: DateTime(2200),
).then((pickedDate) => print(pickedDate));
},
)),
hint:Container(
// color: Colors.red,
child: Text(k,style: TextStyle(fontSize: 14.sp,fontWeight: FontWeight.bold,color:Colors.black87,fontFamily:'OpenSans'),)),
),
),
),
),
);
}

这样做的原因是,这不是一个有状态的小部件,最后我更新为一个有状态的小部件,并使k作为局部变量和setState当更新发生

class Fdate extends StatefulWidget {
@override
_FdateState createState() => _FdateState();
}
class _FdateState extends State<Fdate> {
String k ='Select Date';
DateTime selectedDate = DateTime.now();
void setDate(String date){
setState(() {
k = date;
});
}
@override
Widget build(BuildContext context) {
return  Column(
children: [
Container(
padding: EdgeInsets.zero,
margin: EdgeInsets.all(0),
// color: Colors.black87,
child:
Align(
alignment: Alignment.centerLeft,
child: Text("Select Date",style: TextStyle(fontSize: 14.sp,fontWeight: FontWeight.w400,fontFamily:'OpenSans',),),
)
),
Container(
// color: Colors.blue,
margin: EdgeInsets.only(top:0),
// color: Colors.blue,
child: Align(
alignment: Alignment.centerLeft,
child: GestureDetector(
onTap: () async {
final DateTime picked = await showDatePicker(
context:context ,
initialDate: DateTime.now(),
firstDate: DateTime(2001),
lastDate: DateTime(2200),
// initialEntryMode: DatePickerEntryMode.input,
);
setDate(picked.toIso8601String().split('T').first);

},
child: Container(
// height:40.h,
height: MediaQuery.of(context).size.height*0.050,
child: DropdownButton(
icon: Container(
alignment: Alignment.centerLeft,
// color: Colors.blueGrey,
padding: EdgeInsets.zero,
margin: EdgeInsets.only(left: MediaQuery.of(context).size.width*0.25),
child: IconButton(
icon:Icon(Icons.arrow_drop_down,size:25.w,),
color: Colors.black87,
padding: EdgeInsets.all(0),
constraints: BoxConstraints(),
onPressed: (){
// print("Hello");
// showDatePicker(
//   context:context ,
//   initialDate: DateTime.now(),
//   firstDate: DateTime(2001),
//   lastDate: DateTime(2200),
// ).then((pickedDate) => print(pickedDate));
},
)),
hint:Container(
// color: Colors.red,
child: Text(k,style: TextStyle(fontSize: 14.sp,fontWeight: FontWeight.bold,color:Colors.black87,fontFamily:'OpenSans'),)),
),
),
),
),
)
],
);
}
}

最新更新