vendredi 6 août 2021

How can i refresh 'if condition' on widget tree in flutter?

class CustomDateAndTime extends StatefulWidget {
  const CustomDateAndTime({Key? key}) : super(key: key);

  @override
  _CustomDateAndTimeState createState() => _CustomDateAndTimeState();
}

class _CustomDateAndTimeState extends State<CustomDateAndTime> {
  bool checkDate = false;
  TextEditingController controller = TextEditingController();
  String dateText = '';
  late DateTime date;
  var formatter = new DateFormat('d MMM, yyyy');

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 4),
      child: SizedBox(
        width: MediaQuery.of(context).size.width,
        child: Row(
          mainAxisAlignment: MainAxisAlignment.start,
          children: <Widget>[
            Expanded(
              flex: 4,
              child: datePicker(context),
            ),
            Expanded(
              flex: 1,
              child: Padding(
                padding: EdgeInsets.only(top: 15),
                child: Icon(Icons.date_range_outlined),
              ),
            ),
            if (checkDate == true)
              Expanded(
                flex: 1,
                child: Padding(
                  padding: EdgeInsets.only(top: 15),
                  child: IconButton(
                    icon: Icon(Icons.close),
                    onPressed: () {
                      setState(() {
                        controller.clear();
                        checkDate = false;
                      });
                    },
                  ),
                ),
              ),
          ],
        ),
      ),
    );
  }

  TextFormField datePicker(BuildContext context) {
    return TextFormField(
      readOnly: true,
      controller: controller,
      onTap: () async {
        date = (await showDatePicker(
          context: context,
          initialDate: DateTime.now(),
          firstDate: DateTime(2021, 8),
          lastDate: DateTime(2050),
        ))!;
        if (date.day == DateTime.now().day) {
          controller.text = "TODAY";
          checkDate = true;
        } else if (date.day == DateTime.now().day + 1) {
          controller.text = "TOMORROW";
          checkDate = true;
        } else {
          controller.text = formatter.format(date);
          checkDate = true;
        }
      },
    );
  }
}

In this code, If a date is entered, I will add a delete button, but to do this, the if block needs to work again. But it's not working. Although it is 'statefull widget', it does not go inside the if block. Summary; A new button will be added when the date is entered. How can I do that ? .... ....................................................................................................................

Aucun commentaire:

Enregistrer un commentaire