Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
404 views
in Technique[技术] by (71.8m points)

flutter - I want to assign a Date value to local variable in this Widget and show on the text box

That is my code I want to set k value as a string by getting the Date using showDatepicker I cannot get the k value and put on the hint text. It same as the before value, it is not change. It is same as the '' value, I made then method and set the value but not worked

 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'),)),

              ),
            ),
          ),
       ),
   );
}
question from:https://stackoverflow.com/questions/65890809/i-want-to-assign-a-date-value-to-local-variable-in-this-widget-and-show-on-the-t

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The reason of this was that is not a statefull widget ,at lenght I update as a statefull widget and make k as local variable and setState when update happend

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'),)),

                ),
              ),
            ),
          ),
        )
      ],
    );
  }
}



与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...