繁体   English   中英

我想为这个小部件中的局部变量分配一个日期值并显示在文本框中

[英]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.

 
粤ICP备18138465号  © 2020-2023 STACKOOM.COM