[英][Flutter][Web] - How to restrict the focus removing from the text field when we tap on any other widgets?
在下面的簡單示例中,我有 Textfields 以及 Text 和 Container 小部件。 如果我們在焦點位於文本字段中時鼠標點擊容器或文本小部件,則文本字段焦點將丟失。
在這種情況下,我希望焦點始終保留在文本字段中,直到點擊任何其他可聚焦的小部件,即另一個文本字段。 有什么建議嗎? 我怎樣才能做到這一點?
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Flutter Demo',
theme: ThemeData(primarySwatch: Colors.blue),
home: const MyHomePage());
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key}) : super(key: key);
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
FocusNode focusNode = FocusNode();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
actions: [
IconButton(icon: const Icon(Icons.menu), onPressed: (() {})),
IconButton(icon: const Icon(Icons.remove), onPressed: (() {}))
],
title: const Text('Flutter Demo Home Page'),
),
body: Center(
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: <Widget>[
const TextField(),
const Text('Home Page'),
Container(
color: Colors.amberAccent,
width: 400,
height: 100,
child: const Center(child: Text('Container'))),
const TextField(),
])),
);
}
}
謝謝!
除非每次關閉鍵盤時都重新激活焦點模式,否則無法保持焦點固定
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.