[英]I want to assign a Date value to local variable in this Widget and show on the text box
那是我的代碼我想通過使用 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'),)),
),
),
),
),
)
],
);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.