焦點與文字欄位
當文字欄位被選中並接受輸入時,它被稱為擁有“焦點”。通常,使用者透過點選將焦點轉移到文字欄位,而開發者則透過本食譜中描述的工具以程式設計方式將焦點轉移到文字欄位。
管理焦點是建立具有直觀流程的表單的基本工具。例如,假設你有一個帶有文字欄位的搜尋螢幕。當用戶導航到搜尋螢幕時,你可以將焦點設定到搜尋詞的文字欄位。這允許使用者在螢幕可見時立即開始輸入,而無需手動點選文字欄位。
在本食譜中,學習如何在文字欄位可見時立即為其提供焦點,以及如何在點選按鈕時為其提供焦點。
文字欄位可見時立即獲取焦點
#要使文字欄位在可見時立即獲取焦點,請使用 autofocus 屬性。
TextField(
autofocus: true,
);有關處理輸入和建立文字欄位的更多資訊,請參閱食譜的表單部分。
點選按鈕時文字欄位獲取焦點
#除了立即將焦點轉移到特定文字欄位外,您可能還需要在稍後的時間點將焦點轉移到文字欄位。在現實世界中,您可能還需要響應 API 呼叫或驗證錯誤,將焦點轉移到特定文字欄位。在此示例中,按照以下步驟在使用者按下按鈕後將焦點轉移到文字欄位
- 建立一個
FocusNode。 - 將
FocusNode傳遞給TextField。 - 點選按鈕時讓
TextField獲取焦點。
1. 建立一個 FocusNode
#首先,建立一個 FocusNode。使用 FocusNode 在 Flutter 的“焦點樹”中識別特定的 TextField。這允許您在接下來的步驟中將焦點賦予 TextField。
由於焦點節點是長生命週期的物件,請使用 State 物件管理其生命週期。使用以下說明在 State 類的 initState() 方法中建立一個 FocusNode 例項,並在 dispose() 方法中進行清理
// Define a custom Form widget.
class MyCustomForm extends StatefulWidget {
const MyCustomForm({super.key});
@override
State<MyCustomForm> createState() => _MyCustomFormState();
}
// Define a corresponding State class.
// This class holds data related to the form.
class _MyCustomFormState extends State<MyCustomForm> {
// Define the focus node. To manage the lifecycle, create the FocusNode in
// the initState method, and clean it up in the dispose method.
late FocusNode myFocusNode;
@override
void initState() {
super.initState();
myFocusNode = FocusNode();
}
@override
void dispose() {
// Clean up the focus node when the Form is disposed.
myFocusNode.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
// Fill this out in the next step.
}
}2. 將 FocusNode 傳遞給 TextField
#現在您已經有了一個 FocusNode,將其傳遞給 build() 方法中的特定 TextField。
@override
Widget build(BuildContext context) {
return TextField(focusNode: myFocusNode);
}3. 點選按鈕時讓 TextField 獲取焦點
#最後,當用戶點選浮動操作按鈕時,讓文字欄位獲取焦點。使用 requestFocus() 方法執行此任務。
FloatingActionButton(
// When the button is pressed,
// give focus to the text field using myFocusNode.
onPressed: () => myFocusNode.requestFocus(),
),互動示例
#import 'package:flutter/material.dart';
void main() => runApp(const MyApp());
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return const MaterialApp(title: 'Text Field Focus', home: MyCustomForm());
}
}
// Define a custom Form widget.
class MyCustomForm extends StatefulWidget {
const MyCustomForm({super.key});
@override
State<MyCustomForm> createState() => _MyCustomFormState();
}
// Define a corresponding State class.
// This class holds data related to the form.
class _MyCustomFormState extends State<MyCustomForm> {
// Define the focus node. To manage the lifecycle, create the FocusNode in
// the initState method, and clean it up in the dispose method.
late FocusNode myFocusNode;
@override
void initState() {
super.initState();
myFocusNode = FocusNode();
}
@override
void dispose() {
// Clean up the focus node when the Form is disposed.
myFocusNode.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Text Field Focus')),
body: Padding(
padding: const EdgeInsets.all(16),
child: Column(
children: [
// The first text field is focused on as soon as the app starts.
const TextField(autofocus: true),
// The second text field is focused on when a user taps the
// FloatingActionButton.
TextField(focusNode: myFocusNode),
],
),
),
floatingActionButton: FloatingActionButton(
// When the button is pressed,
// give focus to the text field using myFocusNode.
onPressed: () => myFocusNode.requestFocus(),
tooltip: 'Focus Second Text Field',
child: const Icon(Icons.edit),
), // This trailing comma makes auto-formatting nicer for build methods.
);
}
}