关闭抽屉后重新聚焦 TextField
Refocus TextField after dismissing Drawer
我想在用户关闭抽屉时 return 关注 TextField
。在此处的示例应用程序中(从 https://flutter.dev/docs/cookbook/design/drawer 中提取并稍作修改)有一个 TextField
具有焦点。打开抽屉,然后通过点击它旁边的关闭抽屉关闭抽屉,但 TextField
上的焦点已经消失。
如何 return 聚焦到 TextField
?
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
final appTitle = 'Drawer Demo';
@override
Widget build(BuildContext context) {
return MaterialApp(
title: appTitle,
home: MyHomePage(title: appTitle),
);
}
}
class MyHomePage extends StatelessWidget {
final String title;
MyHomePage({Key key, this.title}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text(title)),
body: Center(child: TextField(autofocus: true)),
drawer: Drawer(
// Add a ListView to the drawer. This ensures the user can scroll
// through the options in the drawer if there isn't enough vertical
// space to fit everything.
child: ListView(
// Important: Remove any padding from the ListView.
padding: EdgeInsets.zero,
children: <Widget>[
DrawerHeader(
child: Text('Drawer Header'),
decoration: BoxDecoration(
color: Colors.blue,
),
),
ListTile(
title: Text('Item 1'),
onTap: () {
// Update the state of the app
// ...
// Then close the drawer
Navigator.pop(context);
},
),
ListTile(
title: Text('Item 2'),
onTap: () {
// Update the state of the app
// ...
// Then close the drawer
Navigator.pop(context);
},
),
],
),
),
);
}
}
使用 FocusNode
和自定义 Drawer
小部件:
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
final appTitle = 'Drawer Demo';
@override
Widget build(BuildContext context) {
return MaterialApp(
title: appTitle,
home: MyHomePage(title: appTitle),
);
}
}
class MyHomePage extends StatelessWidget {
final String title;
MyHomePage({Key key, this.title}) : super(key: key);
@override
Widget build(BuildContext context) {
FocusNode fieldNode = FocusNode();
return Scaffold(
appBar: AppBar(title: Text(title)),
body: Center(
child: Container(
width: 150.0,
child: TextField(autofocus: true,focusNode: fieldNode,)
),
),
drawer: CustomDrawer(node: fieldNode,),
);
}
}
class CustomDrawer extends StatefulWidget {
final FocusNode node ;
CustomDrawer({Key key,this.node}): super(key: key);
@override
_CustomDrawerState createState() => _CustomDrawerState();
}
class _CustomDrawerState extends State<CustomDrawer> {
@override
void initState() {
super.initState();
widget.node.unfocus();
}
@override
void dispose() {
widget.node.requestFocus();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Drawer(
// Add a ListView to the drawer. This ensures the user can scroll
// through the options in the drawer if there isn't enough vertical
// space to fit everything.
child: ListView(
// Important: Remove any padding from the ListView.
padding: EdgeInsets.zero,
children: <Widget>[
DrawerHeader(
child: Text('Drawer Header'),
decoration: BoxDecoration(
color: Colors.blue,
),
),
ListTile(
title: Text('Item 1'),
onTap: () {
// Update the state of the app
// ...
// Then close the drawer
Navigator.pop(context);
},
),
ListTile(
title: Text('Item 2'),
onTap: () {
// Update the state of the app
// ...
// Then close the drawer
Navigator.pop(context);
},
),
],
),
);
}
}
我想在用户关闭抽屉时 return 关注 TextField
。在此处的示例应用程序中(从 https://flutter.dev/docs/cookbook/design/drawer 中提取并稍作修改)有一个 TextField
具有焦点。打开抽屉,然后通过点击它旁边的关闭抽屉关闭抽屉,但 TextField
上的焦点已经消失。
如何 return 聚焦到 TextField
?
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
final appTitle = 'Drawer Demo';
@override
Widget build(BuildContext context) {
return MaterialApp(
title: appTitle,
home: MyHomePage(title: appTitle),
);
}
}
class MyHomePage extends StatelessWidget {
final String title;
MyHomePage({Key key, this.title}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text(title)),
body: Center(child: TextField(autofocus: true)),
drawer: Drawer(
// Add a ListView to the drawer. This ensures the user can scroll
// through the options in the drawer if there isn't enough vertical
// space to fit everything.
child: ListView(
// Important: Remove any padding from the ListView.
padding: EdgeInsets.zero,
children: <Widget>[
DrawerHeader(
child: Text('Drawer Header'),
decoration: BoxDecoration(
color: Colors.blue,
),
),
ListTile(
title: Text('Item 1'),
onTap: () {
// Update the state of the app
// ...
// Then close the drawer
Navigator.pop(context);
},
),
ListTile(
title: Text('Item 2'),
onTap: () {
// Update the state of the app
// ...
// Then close the drawer
Navigator.pop(context);
},
),
],
),
),
);
}
}
使用 FocusNode
和自定义 Drawer
小部件:
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
final appTitle = 'Drawer Demo';
@override
Widget build(BuildContext context) {
return MaterialApp(
title: appTitle,
home: MyHomePage(title: appTitle),
);
}
}
class MyHomePage extends StatelessWidget {
final String title;
MyHomePage({Key key, this.title}) : super(key: key);
@override
Widget build(BuildContext context) {
FocusNode fieldNode = FocusNode();
return Scaffold(
appBar: AppBar(title: Text(title)),
body: Center(
child: Container(
width: 150.0,
child: TextField(autofocus: true,focusNode: fieldNode,)
),
),
drawer: CustomDrawer(node: fieldNode,),
);
}
}
class CustomDrawer extends StatefulWidget {
final FocusNode node ;
CustomDrawer({Key key,this.node}): super(key: key);
@override
_CustomDrawerState createState() => _CustomDrawerState();
}
class _CustomDrawerState extends State<CustomDrawer> {
@override
void initState() {
super.initState();
widget.node.unfocus();
}
@override
void dispose() {
widget.node.requestFocus();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Drawer(
// Add a ListView to the drawer. This ensures the user can scroll
// through the options in the drawer if there isn't enough vertical
// space to fit everything.
child: ListView(
// Important: Remove any padding from the ListView.
padding: EdgeInsets.zero,
children: <Widget>[
DrawerHeader(
child: Text('Drawer Header'),
decoration: BoxDecoration(
color: Colors.blue,
),
),
ListTile(
title: Text('Item 1'),
onTap: () {
// Update the state of the app
// ...
// Then close the drawer
Navigator.pop(context);
},
),
ListTile(
title: Text('Item 2'),
onTap: () {
// Update the state of the app
// ...
// Then close the drawer
Navigator.pop(context);
},
),
],
),
);
}
}