我有一个TextField
,当一个按钮被按下时,它会呈现。我也试图关注这个TextField
,但是它不工作。下面是我的代码:
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key, required this.title});
final String title;
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
bool _buttonPressed = false;
FocusNode _focusNode = FocusNode();
@override
Widget build(BuildContext context) {
return Scaffold(
body: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
ElevatedButton(
onPressed: () {
_buttonPressed = true;
FocusScope.of(context).requestFocus(_focusNode);
},
child: Text('Button'),
),
_buttonPressed
? TextField(
focusNode: _focusNode,
)
: Container(),
],
),
);
}
}
如何在按下按钮后将焦点放在TextField上?
1条答案
按热度按时间lmvvr0a81#
已编辑代码并已测试。
快乐编码😀