选择 Flutter TextFormField 时 Android 模拟器键盘自动消失

发布于 2025-01-13 19:48:38 字数 3533 浏览 1 评论 0原文

我的 Flutter 应用程序中有 TextFormFields。当应用程序在网络上运行时单击它们时,一切正常。

但是,当我在任何 Android 模拟器上运行时,当我单击 TextFormField 输入数据时,智能手机键盘会弹出不到 1 秒,然后自动消失。

TextFormField 也包含在 ExpandableNotifier 中,而 ExpandableNotifier 又包含在 Container 中。单击 TextFormField 时,不仅智能手机键盘会在 1 秒后消失,包含 TextFormField 的 ExpansionNotifier 也会关闭,就好像整个小部件已重新加载一样。

单击之前(TextFormField 位于右侧):

在此处输入图像描述

点击后:

在此处输入图像描述

这里是调用 getTimes 的 FutureBuilder 中包含的 TextFormField 的代码从所选日期返回数据的函数(从左侧的 dateTimePicker ):

FutureBuilder getTimesFutureBuilder(){
    return FutureBuilder<double?>(
      future: _getTimes,
      builder: (BuildContext context, AsyncSnapshot snapshot) {
        if (snapshot.hasData){
          _timeController.clear();
          return TextFormField(
            controller: _timeController,
            textAlign: TextAlign.center,
            enabled: false,
            decoration: InputDecoration(
              hintText: snapshot.data.toString() + " h",
              contentPadding: EdgeInsets.zero,
              filled: true,
              fillColor: Colors.white70
            ),
          );
        } 
        else {
          return TextFormField(
            controller: _timeController,
            textAlign: TextAlign.center,
            enabled: true,
            decoration: InputDecoration(
              hintText: "0 h",
              contentPadding: EdgeInsets.zero,
              filled: true,
              fillColor: Colors.white
            ),
          );
        }
      }
    );
  }

这里是 getTimes 函数的代码:

Future<double?> getTimes(DateTime date) async{
    var actualLaborEffort;
    if (ContextManager.selectedTask != null) {
    if (ContextManager.times.isEmpty && ((widget.task != ContextManager.selectedTask) || (currentWeek != date.weekOfYear ))) {
      actualLaborEffort = await widget.p3provider.getSelectedTime(widget.task.id, date);
      appelsWebService++;
      currentWeek = date.weekOfYear;
      ContextManager.selectedTask = widget.task;
    } else if (ContextManager.times.isEmpty) {
      actualLaborEffort = null;
      currentWeek = date.weekOfYear;
      ContextManager.isTimeNull = true;
      ContextManager.selectedTask = widget.task;
    } else {
      for (int i = 0; i < ContextManager.times.length; i++) {
        if ((date.weekOfYear == ContextManager.times[i].date.weekOfYear) && (widget.task.id == ContextManager.times[i].taskId)) {
          actualLaborEffort = widget.p3provider.getCachedActualLaborEffort(date);
          currentWeek = date.weekOfYear;
          hasBeenCalled = true;
          ContextManager.selectedTask = widget.task;
        } else {
          actualLaborEffort = await widget.p3provider.getSelectedTime(widget.task.id, date);
          autreAppelWebServ++;
          currentWeek = date.weekOfYear;
          hasBeenCalled = true;
          ContextManager.selectedTask = widget.task;
        }
      }
    }

    hasBeenCalled = hasBeenCalled;
    //if (mounted) setState(() {
      _isButtonEnabled = ContextManager.isTimeNull;
    //});
    ContextManager.selectedTask = widget.task;
    return actualLaborEffort;
  }

我尝试将 getTimes 中调用的 setState 包装在 if (已安装)条件中,但它并没有真正改变 任何事物。

任何帮助都会受到欢迎。

I have TextFormFields in my Flutter app. When I click on them when the app is running on web everything is ok.

But when I run on any Android emulator, and when I click on the TextFormField to enter data, the smartphone keyboard pops up for less than 1 second and automatically disapear after.

Also the TextFormField is contained in an ExpandableNotifier which is contained in a Container. When clicking on the TextFormField, not only the smartphone keyboard disappear after 1 second, the ExpansionNotifier which contains the TextFormField shutdown as if the whole widget was reloaded.

Before clicking (the TextFormField is on the right) :

enter image description here

After clicking :

enter image description here

Here the code of the TextFormField which is contained in a FutureBuilder calling a getTimes function to return data from the selected date (from the dateTimePicker on the left):

FutureBuilder getTimesFutureBuilder(){
    return FutureBuilder<double?>(
      future: _getTimes,
      builder: (BuildContext context, AsyncSnapshot snapshot) {
        if (snapshot.hasData){
          _timeController.clear();
          return TextFormField(
            controller: _timeController,
            textAlign: TextAlign.center,
            enabled: false,
            decoration: InputDecoration(
              hintText: snapshot.data.toString() + " h",
              contentPadding: EdgeInsets.zero,
              filled: true,
              fillColor: Colors.white70
            ),
          );
        } 
        else {
          return TextFormField(
            controller: _timeController,
            textAlign: TextAlign.center,
            enabled: true,
            decoration: InputDecoration(
              hintText: "0 h",
              contentPadding: EdgeInsets.zero,
              filled: true,
              fillColor: Colors.white
            ),
          );
        }
      }
    );
  }

Here the code of the getTimes function :

Future<double?> getTimes(DateTime date) async{
    var actualLaborEffort;
    if (ContextManager.selectedTask != null) {
    if (ContextManager.times.isEmpty && ((widget.task != ContextManager.selectedTask) || (currentWeek != date.weekOfYear ))) {
      actualLaborEffort = await widget.p3provider.getSelectedTime(widget.task.id, date);
      appelsWebService++;
      currentWeek = date.weekOfYear;
      ContextManager.selectedTask = widget.task;
    } else if (ContextManager.times.isEmpty) {
      actualLaborEffort = null;
      currentWeek = date.weekOfYear;
      ContextManager.isTimeNull = true;
      ContextManager.selectedTask = widget.task;
    } else {
      for (int i = 0; i < ContextManager.times.length; i++) {
        if ((date.weekOfYear == ContextManager.times[i].date.weekOfYear) && (widget.task.id == ContextManager.times[i].taskId)) {
          actualLaborEffort = widget.p3provider.getCachedActualLaborEffort(date);
          currentWeek = date.weekOfYear;
          hasBeenCalled = true;
          ContextManager.selectedTask = widget.task;
        } else {
          actualLaborEffort = await widget.p3provider.getSelectedTime(widget.task.id, date);
          autreAppelWebServ++;
          currentWeek = date.weekOfYear;
          hasBeenCalled = true;
          ContextManager.selectedTask = widget.task;
        }
      }
    }

    hasBeenCalled = hasBeenCalled;
    //if (mounted) setState(() {
      _isButtonEnabled = ContextManager.isTimeNull;
    //});
    ContextManager.selectedTask = widget.task;
    return actualLaborEffort;
  }

I tried to wrap the setState called in getTimes in a if (mounted) condition but it didn't really changed anything.

Any help will be welcomed.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文