【问题标题】:Flutter: How to change focus to another widget when a certain key is pressed on physical keyboardFlutter:如何在物理键盘上按下某个键时将焦点更改为另一个小部件
【发布时间】:2021-06-25 03:03:08
【问题描述】:

我正在 Windows 上开发一个桌面应用程序,并试图在另一个文本字段上按下 ENTER 键时将焦点移到一个文本字段。 我能够使用RawKeyboardListener 检测到已按下键,但焦点未更改为新字段。我怎样才能让它工作?

import 'package:flutter/material.dart';

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

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

class _MyAppState extends State<MyApp> {
  late FocusNode _txtNode;

  @override
  void initState() {
    super.initState();
    _txtNode = FocusNode();
  }

  @override
  void dispose() {
    _txtNode.dispose();

    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: SafeArea(
          child: Stack(
            children: [
              Container(
              height: 400,
              width: 500,
              color: Colors.grey[350],
              child: Column(
                children: [
                  Container(
                      height: 100,
                      child: RawKeyboardListener(
                        focusNode: FocusNode(),
                          onKey: (event){
                          if (event.toString().contains('RawKeyDownEvent') && event.toString().contains('Enter')) {
                            print("pressed ENTER");
                            _txtNode.requestFocus();
                          }
                          },
                          child: TextField(readOnly: true,))
                  ),
                  TextField(
                  ),
                  TextField(
                    focusNode: _txtNode,
                  ),
                ],
              ),
            ),
          ]
          ),
        ),
      ),
    );
  }
}

【问题讨论】:

    标签: flutter dart flutter-desktop


    【解决方案1】:

    我已经通过修改您的代码 sn-ps 实现了您的用例。请检查以下代码 sn-ps。 FocusScope.of(context).nextFocus() 用于将焦点移动到下一个。如果要将焦点移动到特定小部件。你就叫它focusNode.requestFocus()

    
    class FocusDemo extends StatefulWidget {
      @override
      _FocusDemoState createState() => _FocusDemoState();
    }
    
    class _FocusDemoState extends State<FocusDemo> {
      late FocusNode _txtNode;
    
      @override
      void initState() {
        super.initState();
        _txtNode = FocusNode();
      }
    
      @override
      void dispose() {
        _txtNode.dispose();
        super.dispose();
      }
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          body: SafeArea(
            child: Focus(
              focusNode: _txtNode,
              onKey: (focusNode, event) {
                if (event.runtimeType == RawKeyUpEvent &&
                    event.logicalKey == LogicalKeyboardKey.enter) {
                  focusNode.nextFocus();
                  return KeyEventResult.handled;
                }
                return KeyEventResult.ignored;
              },
              child: Container(
                height: 400,
                width: 500,
                color: Colors.grey[350],
                child: Column(
                  children: [
                    TextField(),
                    TextField(),
                    TextField(),
                  ],
                ),
              ),
            ),
          ),
        );
      }
    }
    
    

    【讨论】:

    • 我重新测试过,它似乎没有工作。未设置焦点
    • 好的兄弟,我会检查并回复。
    • @West,你能检查一下当前的代码 sn-ps 吗?
    • 屏幕不再呈现我收到一个错误No MediaQuery widget ancestor found.The relevant error-causing widget was: FocusDemo file:///C:/Users/Public/Android/Projects/tests/lib/main.dart:5:10 您在发布之前是否测试了代码,它是否对您有效?
    • 是的,我保证。请将MaterialApp 小部件包装到Scaffold 小部件的父级并检查?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-10-12
    • 2019-01-10
    • 1970-01-01
    • 2016-02-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多