如何将showTimePicker用作flutter中的Widget? [英] How to use showTimePicker as Widget in flutter?

查看:76
本文介绍了如何将showTimePicker用作flutter中的Widget?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望用户选择日期和时间,因为有日期时间选择器对话框.

但是,有没有办法,我可以在某些Flutter小部件上持续显示日期时间,并像其他任何小部件一样使用?

 容器(子:showTimePicker(上下文:上下文,initialTime:TimeOfDay.now(),builder:(BuildContext上下文,Widget子项){返回主题(数据:ThemeData.dark(),孩子:孩子,);},);} 

但我不能将 showTimePicker 用作窗口小部件.

如何将 showTimePicker()用作窗口小部件?以便可以在其顶部构建其他小部件.

解决方案

就像您前一段时间一样,我遇到了问题,我复制了源代码并制作了自定义小部件.现在,它可以像任何小部件一样使用.如果您想采用此方法,我想提几件事.

  1. 我不确定这是否适用于本地化,我没有对此进行测试.
  2. 我不确定这是否可以用于浅色主题以外的其他主题,我仅在浅色主题上进行了测试.

您可以在此处找到代码. https://gist.github.com/mithunadhikari40/b55b9990ebc15d0d8bf70fd3f87709b>

用法:从上面的链接复制代码,创建一个dart文件,粘贴代码并按以下方式使用它:

  SizedBox(子:TimePickerDialog(initialTime:TimeOfDay.fromDateTime(DateTime.now()),onTimeChange:(TimeOfDay时间){打印(我们得到的时间价值现在是$ time");},),), 

I want the user to pick the date and time, for that there is date time picker dialogue.

But, is there a way that, I could show the date-time persistently on some flutter widget and use like any other widget?

Container(
   child: showTimePicker(
          context: context,
          initialTime: TimeOfDay.now(),
          builder: (BuildContext context, Widget child) {
            return Theme(
              data: ThemeData.dark(),
              child: child,
            );
          },
        );
}

but I cannot use showTimePicker as Widget.

How to use showTimePicker() as widget? so that other widgets can be built on top of it.

解决方案

I ran into the problem just like you some time ago and I copied the source code and made my custom widget. Now, it can be used like any widget. If you want to adopt this, I want to mention a couple of things.

  1. I am not sure if this works well with localization, I did not test that.
  2. I am not sure if this works on other themes than the light theme, I only tested on the light theme.

You can find the code here. https://gist.github.com/mithunadhikari40/b55b9990ebc15d0d8bf70fd3f87709b0

Usage: Copy the code from the above link, create a dart file, paste the code and use it like this:

                     SizedBox(
                        child: TimePickerDialog(
                        initialTime: TimeOfDay.fromDateTime(DateTime.now()),
                        onTimeChange: (TimeOfDay time) {
                          print(
                              "What we get the value of the time is now $time");
                        },
                      ),
                    ),

这篇关于如何将showTimePicker用作flutter中的Widget?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆