dart 在清除表单字段并将焦点设置为该字段后,不允许输入Flutter文本字段

0dxa2lsx  于 2024-01-04  发布在  Flutter
关注(0)|答案(1)|浏览(205)

**更新:**我将代码编辑为一个最小的可复制示例,您可以将其复制到Flutter项目中并立即运行它。

这看起来像是一个Flutter bug,但我想在提交bug报告之前先问一下这里。我有下面的表单。当表单打开时,单选按钮选择默认为Check Out。当用户将其更改为其他单选按钮时,表单字段应该清除,焦点应该设置为位置字段并立即允许输入。
当我打开表单并选择“签入”时(即其他按钮),表单将焦点切换到位置字段,并让我像正常一样输入内容。然而,如果我随后切换到 checkout 选项,焦点仍然显示在位置字段上,但当我尝试输入内容时,什么也没有显示。然后当我再次选择签入时,同样的事情发生了-它不让我输入任何东西。也许我做错了什么,但似乎在输入任何东西之前连续多次请求焦点是错误的。

打开表单:

x1c 0d1x的数据

选择Check In选项(焦点切换到位置字段,我可以立即开始键入):


选择 checkout 选项(位置字段清除,光标仍然显示在上面,但当我键入内容时,什么也没有显示):

  1. import 'package:flutter/material.dart';
  2. void main() {
  3. runApp(const MyApp());
  4. }
  5. class MyApp extends StatelessWidget {
  6. const MyApp({super.key});
  7. // This widget is the root of your application.
  8. @override
  9. Widget build(BuildContext context) {
  10. return MaterialApp(
  11. title: 'Flutter Demo',
  12. theme: ThemeData(
  13. colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
  14. useMaterial3: true,
  15. ),
  16. home: const MyHomePage(title: 'Flutter Demo Home Page'),
  17. );
  18. }
  19. }
  20. enum CheckInOutType { checkIn, checkOut }
  21. class MyHomePage extends StatefulWidget {
  22. const MyHomePage({super.key, required this.title});
  23. final String title;
  24. @override
  25. State<MyHomePage> createState() => _MyHomePageState();
  26. }
  27. class _MyHomePageState extends State<MyHomePage> {
  28. CheckInOutType? selectedCheckInOut = CheckInOutType.checkOut;
  29. final locController = TextEditingController();
  30. late FocusNode locFocusNode; // = FocusNode();
  31. @override
  32. void initState() {
  33. super.initState(); //Run this first
  34. initialize();
  35. }
  36. @override
  37. dispose() {
  38. super.dispose(); //Run this last
  39. locFocusNode.dispose();
  40. }
  41. void initialize() async {
  42. locFocusNode = FocusNode();
  43. }
  44. void clearFields() {
  45. locController.text = '';
  46. locFocusNode.requestFocus();
  47. setState(() {
  48. });
  49. }
  50. @override
  51. Widget build(BuildContext context) {
  52. return Scaffold(
  53. appBar: AppBar(
  54. title: const Text('Overflow Inventory'),
  55. centerTitle: true,
  56. leading: IconButton(
  57. icon: const Icon(Icons.arrow_back),
  58. onPressed: () {
  59. Navigator.pop(context);
  60. },
  61. ),
  62. ),
  63. body: Column(
  64. children: [
  65. Row(
  66. children: [
  67. Expanded(
  68. flex: 1,
  69. child: ListTile(
  70. title: const Text('Check In'),
  71. leading: Radio<CheckInOutType>(
  72. value: CheckInOutType.checkIn,
  73. groupValue: selectedCheckInOut,
  74. onChanged: (CheckInOutType? value) {
  75. setState(() {
  76. selectedCheckInOut = value;
  77. });
  78. clearFields();
  79. },
  80. ),
  81. ),
  82. ),
  83. Expanded(
  84. flex: 1,
  85. child: ListTile(
  86. title: const Text('Check Out'),
  87. leading: Radio<CheckInOutType>(
  88. value: CheckInOutType.checkOut,
  89. groupValue: selectedCheckInOut,
  90. onChanged: (CheckInOutType? value) {
  91. setState(() {
  92. selectedCheckInOut = value;
  93. });
  94. clearFields();
  95. },
  96. ),
  97. ),
  98. ),
  99. ],
  100. ),
  101. Row(
  102. children: [
  103. Expanded(
  104. child: Text('${selectedCheckInOut == CheckInOutType.checkOut ? 'From' : 'To'} Location:', style: const TextStyle(fontSize: 18, fontWeight: FontWeight.w700))
  105. ),
  106. Expanded(
  107. child: TextField(
  108. focusNode: locFocusNode,
  109. controller: locController,
  110. )
  111. ),
  112. ],
  113. ),
  114. ],
  115. ),
  116. );
  117. }
  118. }

字符串

hyrbngr7

hyrbngr71#

我使用Flutter支持,通过使用flutter upgrade --force将Flutter从3.10升级到3.16,解决了这个问题。

相关问题