Flutter 3 基础12: 选择并显示时间

上一篇文章中,我们掌握了如何选择并显示一定格式的日期内容,以及如何本地化日期选择对话框的文字语言。

本文继续添加一个新的部件:时间选择。

启动 Android Studio,打开 hello_world 项目,运行虚拟机,这样就可以实时看到编码产生的效果。现在,我们的部件页面展示了日期选择,文本框和按钮三个部件。

打开时间选择对话框

  1. lib/widget 文件夹下,打开 my_widgets.dart 文件,定位到下列代码:
dart 复制代码
Widget buildDateField(BuildContext context) {
  1. 在这一行代码的上方,添加下列代码:
dart 复制代码
// 选择时间
Widget buildTimeField(BuildContext context) {
  return Column(
    crossAxisAlignment: CrossAxisAlignment.start,
    children: [
      Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          const Text(
            '时间',
            style: TextStyle(fontSize: 20.0),
          ),
          TextButton(
            child: const Text(
              '选择',
              style: TextStyle(fontSize: 20.0),),
            onPressed: () async {
              showTimePicker(
                initialTime: TimeOfDay.now(),
                context: context,
              );
            },
          ),
        ],
      ),
    ],
  );
}
  1. 继续在 my_widgets.dart 文件中,定位到下列代码:
dart 复制代码
buildMaterialButton(),
  1. 在这一行代码的上方,添加下列代码:
dart 复制代码
buildTimeField(context),
  1. 热重启项目。点击部件导航图标,页面上出现了时间选择的按钮。
  1. 点击时间的选择按钮,会弹出时间选择的对话框。

显示选择的时间

  1. 继续在 my_widgets.dart 文件中,定位到下列代码:
dart 复制代码
DateTime _dueDate = DateTime.now();
  1. 在这一行代码的下方,添加下面的代码:
dart 复制代码
TimeOfDay _timeOfDay = TimeOfDay.now();
  1. 继续在 my_widgets.dart 文件中,定位到下列代码:
dart 复制代码
showTimePicker(
  1. 把这一行代码,替换为以下代码:
dart 复制代码
final timeOfDay = await showTimePicker(
  1. 继续在 my_widgets.dart 文件中,定位到下列代码:
dart 复制代码
final timeOfDay = await showTimePicker(
  initialTime: TimeOfDay.now(),
  context: context,
);
  1. 在这一块代码的下方,添加下面的代码:
dart 复制代码
setState(() {  
  if (timeOfDay != null) {  
    _timeOfDay = timeOfDay;  
  }  
});
  1. 继续在 my_widgets.dart 文件中,定位到下列代码:
dart 复制代码
Row(  
  mainAxisAlignment: MainAxisAlignment.spaceBetween,  
  children: [  
    const Text(  
      '时间',  
      style: TextStyle(fontSize: 20.0),  
    ),  
    TextButton(  
      child: const Text(  
        '选择',  
        style: TextStyle(fontSize: 20.0),),  
      onPressed: () async {  
        final timeOfDay = await showTimePicker(  
          // 2  
          initialTime: TimeOfDay.now(),  
          context: context,  
        );  
  
        setState(() {  
          if (timeOfDay != null) {  
            _timeOfDay = timeOfDay;  
          }  
        });  
      },  
    ),  
  ],  
),
  1. 在这一块代码的下方,添加下面的代码:
dart 复制代码
Text(_timeOfDay.format(context)),
  1. 保存文件,热重启项目。点击部件导航图标,点击时间的选择按钮,选择一个时间,点击 确定 按钮,这时,时间显示在了页面上。

提交代码

我们已经实现了时间的选择,又到达了一个小小的里程碑,应该对代码进行提交,保持良好编程习惯。

shell 复制代码
git add .
git commit -m '选择并显示时间。'
相关推荐
RickyWasYoung17 分钟前
【matlab】字符串数组 转 double
android·java·javascript
bluetata1 小时前
Rokid AR眼镜开发入门:构建智能演讲提词器Android应用
android·人工智能·云计算·ar·ai编程
马 孔 多 在下雨2 小时前
手机App上的轮播图是如何实现的—探究安卓轮播图
android·智能手机
00后程序员张2 小时前
iOS 26 开发者工具推荐,构建高效调试与性能优化工作流
android·ios·性能优化·小程序·uni-app·iphone·webview
技术男3 小时前
flutter中怎么局部刷新
flutter
小范馆4 小时前
通过 useEventBus 和 useEventCallBack 实现与原生 Android、鸿蒙、iOS 的事件交互
android·ios·harmonyos
恋猫de小郭4 小时前
Flutter 也有类 React Flow 的节点流程编辑器,快来了解下刚刚开源的 vyuh_node_flow
android·前端·flutter
2501_916008894 小时前
iOS 26 文件导出与数据分析,多工具组合下的开发者实践指南
android·macos·ios·小程序·uni-app·cocoa·iphone
2501_916008894 小时前
iOS混淆实战用多工具组合把IPA加固做成可复用的工程能力(iOS混淆 IPA加固 无源码混淆
android·ios·小程序·https·uni-app·iphone·webview
wangdaoyin20104 小时前
UniApp 在手机端(Android)打开选择文件和文件写入
android·前端·uni-app