刷新对话框

时间:2021-01-14 16:07:20

标签: flutter dart

不确定我的想法是否正确,

我正在制作一个用于设置计时器的对话框, 我在 AlertDialog 中添加了一个“清除”按钮,将所有内容恢复为默认值,

有没有办法做到这一点?

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Container(
      child: Column(
        children: <Widget>[Screenshot from 2021-01-14 23-48-45
          Text(reminderTime.toString()),
          FlatButton(
            onPressed: (){
              showDialog(
                  context: context,builder: (BuildContext context){
                return AlertDialog(
                    content: Container(
                      child: Wrap(
                          alignment: WrapAlignment.center,
                          children: <Widget>[
                            Container(
                              //constraints: ,
                              child:
                              Row(children: <Widget>[
                                Container(
                                  child: new FittedBox(
                                    fit: BoxFit.fill,
                                    child: new Icon(Icons.alarm_add_rounded),
                                  ),),
                                Container(
                                  child:Expanded(
                                      child:
                                      new ReminderTimePickerWidget(selectedTime: TimeOfDay(
                                        hour: widget.reminderTime != '' ? int.parse(widget.reminderTime.substring(0,2)): 00,
                                        minute: widget.reminderTime != ''? int.parse(widget.reminderTime.substring(3,5)): 00,),
                                        callBackTime: (String time){
                                          setState(() {
                                            reminderTime = time;
                                          });
                                        },
                                      )),)
                              ],),
                            ),
                            FlatButton(onPressed: (){
                              setState(() {
                               reminderTime = '' ;
                              });
                            }, child: Text('Clear')),
                          ]
                      ),
                    )
                );
              }
              );
            },
          ),
        ],
      ),

    );  }

enter image description here

enter image description here

选择时间后,按“清除”应变为“选择时间..”

任何建议将不胜感激,谢谢!!

已编辑:

ReminderTimePickerWidget:

我发现有人这样做,所以我就跟着他们。

class ReminderTimePickerWidgetState extends State<ReminderTimePickerWidget>{
  String dateTime, hour, minute, time, _setTime;
  TimeOfDay selectedTime;
  TextEditingController timeController = TextEditingController();
  StringCallback callBackTime;
  DateFormat newDateFormat = new DateFormat('hh:mm a');


  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    callBackTime = widget.callBackTime;
    selectedTime = widget.selectedTime;

    if(widget.selectedTime.hour != 00 && widget.selectedTime.minute != 00){
      selectedTime = widget.selectedTime;
      timeController.text = newDateFormat.format(DateTime(2222,01,2,selectedTime.hour, selectedTime.minute));
    }else{
      timeController.text = 'Pick a time..';
      selectedTime  = TimeOfDay(hour: DateTime.now().hour, minute: DateTime.now().minute);
    }
    super.initState();
  }


  Future<Null> _selectTime(BuildContext context) async {
    final TimeOfDay picked = await showTimePicker(
      context: context,
      initialTime: selectedTime,
    );
    if (picked != null) {
      setState(() {
        selectedTime = picked;
        hour = selectedTime.hour.toString();
        minute = selectedTime.minute.toString();
        time = hour + ":" + minute;
        timeController.text = formatDate(
            DateTime(2019, 08, 1, selectedTime.hour, selectedTime.minute),
            [hh, ':', nn, " ", am]).toString();
        callBackTime(time);
      });
    }else {
      setState(() {
        hour = selectedTime.hour.toString();
        minute = selectedTime.minute.toString();
        time = hour + ":" + minute;
        timeController.text = formatDate(
            DateTime(2019, 08, 1, selectedTime.hour, selectedTime.minute),
            [hh, ':', nn, " ", am]).toString();
        callBackTime(time);
      });
    };
  }


  @override
  Widget build(BuildContext context) {
    dateTime = DateFormat.yMd().format(DateTime.now());
    return Container(
      child: Column(
        children: <Widget>[
          Column(
            children: <Widget>[
              InkWell(
                onTap: () {
                  _selectTime(context);
                },
                child: Container(
                  alignment: Alignment.center,
                  decoration: BoxDecoration(color: Colors.grey[200]),
                  child: TextFormField(
                    style: TextStyle(fontSize: 40),
                    textAlign: TextAlign.center,
                    onSaved: (String val) {
                      _setTime = val;
                    },
                    enabled: false,
                    keyboardType: TextInputType.text,
                    controller: timeController,
                    decoration: InputDecoration(
                        disabledBorder:
                        UnderlineInputBorder(borderSide: BorderSide.none),
                        // labelText: 'Time',
                        contentPadding: EdgeInsets.all(5)),
                  ),
                ),
              ),
            ],
          ),
        ],
      ),

    );
  }

0 个答案:

没有答案