【问题标题】:ShowTimePicker i need to show the time picker on text fieldShowTimePicker 我需要在文本字段上显示时间选择器
【发布时间】:2021-11-26 15:52:01
【问题描述】:
defultFormField(
                          controller: timeController,
                          onSubmit: () {},
                          onTap: () {
                            showTimePicker(
                              useRootNavigator: true,
                              context: context,
                              initialTime: TimeOfDay.now(),
                            ).then((value) {
                              print(value);
                            });
                          },

and this is the components 

   Widget defultFormField({
  required  Function onTap,
  required Function onSubmit,
  required TextEditingController controller,
  // required TextInputType type,
  required String text,
  required IconData prefix,
  required Function valedate,
}) =>
    TextFormField(
      controller: controller,
      // keyboardType: type,
      onTap: () {},
      onFieldSubmitted: (s) {},
      validator: (s) {
        valedate();
      },
      decoration: InputDecoration(
        border: OutlineInputBorder(),
        labelText: text,
        prefixIcon: Icon(prefix),
      ),
    );

【问题讨论】:

  • 请不要通过破坏您的帖子为他人增加工作量。通过在 Stack Exchange 网络上发帖,您已在 CC BY-SA 4.0 license 下授予 Stack Exchange 分发该内容的不可撤销的权利(即无论您未来的选择如何)。根据 Stack Exchange 政策,帖子的非破坏版本是分发的版本。因此,任何破坏行为都将被撤销。如果您想了解更多关于删除帖子的信息,请参阅:How does deleting work?

标签: flutter mobile


【解决方案1】:

试试下面的代码希望它对你有帮助。或者你也可以使用this 包。

声明 TimeOfDay 类

TimeOfDay _time = TimeOfDay(hour: 00, minute: 00);

为 TimePicker 创建函数

void _selectTime() async {
    final TimeOfDay? newTime = await showTimePicker(
      context: context,
      initialTime: _time,
    );
    if (newTime != null) {
      setState(() {
        _time = newTime;
      });
    }
  }

您的小部件

Column(
      mainAxisAlignment: MainAxisAlignment.center,
      children: [
        Padding(
          padding: const EdgeInsets.all(8.0),
          child: TextFormField(
            decoration: InputDecoration(
              hintText: 'Select Time',
              border: OutlineInputBorder(),
            ),
            onTap: _selectTime,
          ),
        ),
        SizedBox(height: 8),
        Text(
          'Selected time: ${_time.format(context)}',
        ),
      ],
    ),

【讨论】:

    猜你喜欢
    • 2012-10-01
    • 2011-08-29
    • 1970-01-01
    • 2023-04-09
    • 1970-01-01
    • 2015-05-24
    • 2021-10-19
    • 2023-03-17
    • 2016-03-15
    相关资源
    最近更新 更多