flutter 更改日期时显示“文本中的日期”小部件

eoxn13cs  于 2023-02-25  发布在  Flutter
关注(0)|答案(2)|浏览(125)

我正在尝试使用showDialog()显示日期。我正在使用calendar_date_picker包来显示我的日历。
我设置了默认日期为今天,我的要求是在calendar_date_picker小部件中更改日期时显示日期,但在Text小部件中没有获得日期。
下面你可以细代码和各自的屏幕截图.

showDialog(
      barrierColor: const Color(0xff0F559E).withOpacity(0.80),
      context: context,
      builder: (context) {
        return Dialog(
          insetPadding: EdgeInsets.all(30),
          shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(18)),
          child: Container(
            width: MyUtility(context).width,
            // height: MyUtility(context).height,
            child: Column(
              children: [
                Row(
                  children: [
                    Expanded(
                      child: GestureDetector(
                        onTap: () {
                          print('fromActive');
                          setState(() {
                            toActive = false;
                            fromActive = true;
                          });
                        },
                        child: Container(
                          decoration: const BoxDecoration(
                            border: Border(
                              right: BorderSide(width: 0.5, color: Color(0xffD9D9D9)),
                              bottom: BorderSide(width: 1.0, color: Color(0xffD9D9D9)),
                            ),
                          ),
                          child: Padding(
                            padding: const EdgeInsets.fromLTRB(10, 13, 0, 0),
                            child: Column(
                              crossAxisAlignment: CrossAxisAlignment.start,
                              children: [
                                const Text(
                                  'From',
                                  style: TextStyle(fontFamily: 'Outfit', fontSize: 16, fontWeight: FontWeight.w400, color: Color(0xff828282)),
                                ),
                                SizedBox(
                                  height: MyUtility(context).height * 0.01,
                                ),
                                Text(
                                  _getValueText(
                                    config.calendarType,
                                    todayReport,
                                  ),
                                  style: TextStyle(
                                      fontFamily: 'Outfit',
                                      fontSize: 18,
                                      fontWeight: FontWeight.w600,
                                      color: fromActive ? const Color(0xff015CBD) : Colors.black),
                                ),
                                SizedBox(
                                  height: MyUtility(context).height * 0.01,
                                ),
                                Text(
                                  '03:30',
                                  style: TextStyle(
                                      fontFamily: 'Outfit',
                                      fontSize: 18,
                                      fontWeight: FontWeight.w600,
                                      color: fromActive ? const Color(0xff015CBD) : Colors.black),
                                ),
                                SizedBox(
                                  height: MyUtility(context).height * 0.01,
                                ),
                              ],
                            ),
                          ),
                        ),
                      ),
                    ),
                    Expanded(
                      child: GestureDetector(
                        onTap: () {
                          print('toActive');
                          setState(() {
                            toActive = true;
                            fromActive = false;
                          });
                        },
                        child: Container(
                          decoration: const BoxDecoration(
                            border: Border(
                              left: BorderSide(width: 0.5, color: Color(0xffD9D9D9)),
                              bottom: BorderSide(width: 1.0, color: Color(0xffD9D9D9)),
                            ),
                          ),
                          child: Padding(
                            padding: const EdgeInsets.fromLTRB(10, 13, 0, 0),
                            child: Column(
                              crossAxisAlignment: CrossAxisAlignment.start,
                              children: [
                                const Text(
                                  'To',
                                  style: TextStyle(fontFamily: 'Outfit', fontSize: 16, fontWeight: FontWeight.w400, color: Color(0xff828282)),
                                ),
                                SizedBox(
                                  height: MyUtility(context).height * 0.01,
                                ),
                                Text(
                                  _getValueText(
                                    config.calendarType,
                                    todayReport,
                                  ),
                                  style: TextStyle(
                                      fontFamily: 'Outfit',
                                      fontSize: 18,
                                      fontWeight: FontWeight.w600,
                                      color: toActive ? Color(0xff015CBD) : Colors.black),
                                ),
                                SizedBox(
                                  height: MyUtility(context).height * 0.01,
                                ),
                                Text(
                                  '01:30',
                                  style: TextStyle(
                                      fontFamily: 'Outfit',
                                      fontSize: 18,
                                      fontWeight: FontWeight.w600,
                                      color: toActive ? Color(0xff015CBD) : Colors.black),
                                ),
                                SizedBox(
                                  height: MyUtility(context).height * 0.01,
                                ),
                              ],
                            ),
                          ),
                        ),
                      ),
                    )
                  ],
                ),
                Row(
                  children: [
                    Expanded(
                      child: CalendarDatePicker2(
                        config: config,
                        initialValue: todayReport,
                        onValueChanged: (values) {
                          print('printL: $values');
                          setState(() => todayReport = values);
                        },
                      ),
                    ),
                  ],
                ),
                const SizedBox(height: 10),
                Row(
                  mainAxisSize: MainAxisSize.min,
                  children: [
                    const Text('Selection(s):  '),
                    const SizedBox(width: 10),
                    Text(
                      _getValueText(
                        config.calendarType,
                        todayReport,
                      ),
                    ),
                  ],
                ),
              ],
            ),
          ),
        );
      },
    )

i2byvkas

i2byvkas1#

可以使用StatefulBuilder更新对话框ui,将Dialog Package 为

showDialog(
      barrierColor: const Color(0xff0F559E).withOpacity(0.80),
      context: context,
      builder: (context) {
        return StatefulBuilder(
          builder: (context, setStateSB) => Dialog( //use this `setStateSB` as setState , or renamed just to avoid confusion
wqsoz72f

wqsoz72f2#

使用StatefulBuilder在对话框中使用setState,并仅在对话框中更新小部件。

void dialog(context) {
  showDialog(
    context: context,
    builder: (context) {
      String text = "Hello";
      return StatefulBuilder(
        builder: (context, setState) {
          return AlertDialog(
            title: const Text("Title of Dialog"),
            content: Text(text),
            actions: <Widget>[
              TextButton(
                onPressed: () => Navigator.pop(context),
                child: const Text("Cancel"),
              ),
              TextButton(
                onPressed: () {
                  setState(() {
                    text = "Text Change";
                  });
                },
                child: const Text("Change"),
              ),
            ],
          );
        },
      );
    },
  );
}

相关问题