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

0dxa2lsx  于 5个月前  发布在  Flutter
关注(0)|答案(1)|浏览(63)

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

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

打开表单:

x1c 0d1x的数据

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


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

import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
        useMaterial3: true,
      ),
      home: const MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

enum CheckInOutType { checkIn, checkOut }

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key, required this.title});

  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  CheckInOutType? selectedCheckInOut = CheckInOutType.checkOut;
  final locController = TextEditingController();
  late FocusNode locFocusNode; // = FocusNode();

  @override
  void initState() {
    super.initState(); //Run this first
    initialize();
  }

  @override
  dispose() {
    super.dispose(); //Run this last
    locFocusNode.dispose();
  }

  void initialize() async {
    locFocusNode = FocusNode();
  }

  void clearFields() {
    locController.text = '';
    locFocusNode.requestFocus();
    setState(() {

    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: const Text('Overflow Inventory'),
          centerTitle: true,
          leading: IconButton(
            icon: const Icon(Icons.arrow_back),
            onPressed: () {
              Navigator.pop(context);
            },
          ),
        ),
        body: Column(
          children: [
            Row(
              children: [
                Expanded(
                  flex: 1,
                  child: ListTile(
                    title: const Text('Check In'),
                    leading: Radio<CheckInOutType>(
                      value: CheckInOutType.checkIn,
                      groupValue: selectedCheckInOut,
                      onChanged: (CheckInOutType? value) {
                        setState(() {
                          selectedCheckInOut = value;
                        });
                        clearFields();
                      },
                    ),
                  ),
                ),
                Expanded(
                  flex: 1,
                  child: ListTile(
                    title: const Text('Check Out'),
                    leading: Radio<CheckInOutType>(
                      value: CheckInOutType.checkOut,
                      groupValue: selectedCheckInOut,
                      onChanged: (CheckInOutType? value) {
                        setState(() {
                          selectedCheckInOut = value;
                        });
                        clearFields();
                      },
                    ),
                  ),
                ),
              ],
            ),
            Row(
              children: [
                Expanded(
                    child: Text('${selectedCheckInOut == CheckInOutType.checkOut ? 'From' : 'To'} Location:', style: const TextStyle(fontSize: 18, fontWeight: FontWeight.w700))
                ),
                Expanded(
                    child: TextField(
                      focusNode: locFocusNode,
                      controller: locController,
                    )
                ),
              ],
            ),
          ],
        ),
      );
  }
}

字符串

hyrbngr7

hyrbngr71#

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

相关问题