当我使用带有自动焦点的文本字段时,Flutter 会抛出错误

标签 flutter input dart textfield

当我使用 文本字段 Flutter 中的小部件并设置属性值 自动对焦

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: TextField(
          autofocus: true,
        ),
      ),
    );
  }
}

flutter 抛出错误
I/flutter (31721): ══╡ EXCEPTION CAUGHT BY FOUNDATION LIBRARY ╞════════════════════════════════════════════════════════
I/flutter (31721): The following assertion was thrown while dispatching notifications for FocusNode:
I/flutter (31721): RenderBox was not laid out: RenderEditable#bf516 NEEDS-LAYOUT NEEDS-PAINT
I/flutter (31721): 'package:flutter/src/rendering/box.dart':
I/flutter (31721): Failed assertion: line 1687 pos 12: 'hasSize'
I/flutter (31721):
I/flutter (31721): Either the assertion indicates an error in the framework itself, or we should provide substantially
I/flutter (31721): more information in this error message to help you determine and fix the underlying cause.
I/flutter (31721): In either case, please report this assertion by filing a bug on GitHub:
I/flutter (31721):   https://github.com/flutter/flutter/issues/new?template=BUG.md
I/flutter (31721):
I/flutter (31721): When the exception was thrown, this was the stack:
I/flutter (31721): #2      RenderBox.size 
package:flutter/…/rendering/box.dart:1687
I/flutter (31721): #3      EditableTextState._updateSizeAndTransform 
package:flutter/…/widgets/editable_text.dart:1729
I/flutter (31721): #4      EditableTextState._openInputConnection 
package:flutter/…/widgets/editable_text.dart:1415
I/flutter (31721): #5      EditableTextState._openOrCloseInputConnectionIfNeeded 
package:flutter/…/widgets/editable_text.dart:1441
I/flutter (31721): #6      EditableTextState._handleFocusChanged 
package:flutter/…/widgets/editable_text.dart:1707
I/flutter (31721): #7      ChangeNotifier.notifyListeners 
package:flutter/…/foundation/change_notifier.dart:206
I/flutter (31721): #8      FocusNode._notify 
package:flutter/…/widgets/focus_manager.dart:808
I/flutter (31721): #9      FocusManager._applyFocusChange 
package:flutter/…/widgets/focus_manager.dart:1401
I/flutter (31721): (elided 12 frames from class _AssertionError and package dart:async)
I/flutter (31721):
I/flutter (31721): The FocusNode sending notification was:
I/flutter (31721):   FocusNode#ce6fe
I/flutter (31721): ════════════════════════════════════════════════════════════════════════════════════════════════════

为什么,我该如何解决这个问题。

我的 flutter 版本:

[√] Flutter (Channel stable, v1.12.13+hotfix.5, on Microsoft Windows [Version 10.0.18362.535], locale en-US) [√] Android toolchain - 为Android设备开发(Android SDK version 29.0.2)[ √] Android Studio(3.5 版) [√] VS Code(1.41.0 版) [√] 已连接设备(1 个可用) • 未发现问题!

================================================== ======================

最佳答案

即使在官方示例中也存在此问题 https://flutter.dev/docs/cookbook/forms/focus
因为使用自动对焦,键盘首先出现但文本字段仍然不存在
解决方法是使用 FocusNodeWidgetsBinding.instance.addPostFrameCallback当 TextField 显示然后将焦点移到此 TextField 时

代码片段

@override
  void initState(){
    super.initState();
    myFocusNode = FocusNode();
    WidgetsBinding.instance.addPostFrameCallback((_){
      FocusScope.of(context).requestFocus(myFocusNode);
    });
  }

完整代码
import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  FocusNode myFocusNode;

  @override
  void initState(){
    super.initState();
    myFocusNode = FocusNode();
    WidgetsBinding.instance.addPostFrameCallback((_){
      FocusScope.of(context).requestFocus(myFocusNode);
    });
  }

  @override
  void dispose() {
    // Clean up the focus node when the Form is disposed.
    myFocusNode.dispose();

    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: TextField(
          //autofocus: true,
          focusNode: myFocusNode,
        ),
      ),
    );
  }
}

关于当我使用带有自动焦点的文本字段时,Flutter 会抛出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59382235/

相关文章:

arrays - 嵌套的循环无法在Flutter中正常工作

dart - 如何在DART_API中从C++调用运算符?

java - 如何使用 try 和 catch 继续循环以获得正确的输入?

html - 隐藏输入时复选框标签之间的边距

android - 如何创建一个 "type along"风格的界面?

dart - 找不到引用的源:package:web_ui/web_ui.dart

dart - Flutter Firebase Analytics 不记录事件

android - 发布到 Google Play 商店后,MissingPluginException(在 channel plugins.flutter.io/google_sign_in 上找不到方法 init 的实现)

flutter - 在 Android 移动应用中实现多个过滤器的简单方法

flutter - Flutter缓存管理器:清除特定URL的缓存