在项目中有时需要点击某个地方的时候让一个文本框获取焦点以弹起键盘比如前端经常使用的input.focus(),但是在flutter中没有.focus()这个方法不过我们可以通过FocusScope.of(context).requestFocus()来实现这一操作
先看一下实现场景,点击某一条留言然后让文本框获取焦点弹出键盘:
1038765-20190710093834054-1725016072.gif
要使用FocusScope.of(context).requestFocus()需要先定义一个FocusNode

  1. FocusNode _commentFocus = FocusNode();
  2. TextField(
  3. focusNode: _commentFocus,
  4. ),

获取焦点

当点击时用FocusScope.of(context).requestFocus()获取焦点

  1. FocusScope.of(context).requestFocus(_commentFocus); // 获取焦点

失去焦点

当发送留言之后可以通过unfocus()让其失去焦点

  1. _commentFocus.unfocus(); // 失去焦点

最后附上完整代码

  1. import 'package:flutter/material.dart';
  2. class CommentTest extends StatefulWidget {
  3. @override
  4. _CommentTestState createState() => _CommentTestState();
  5. }
  6. class _CommentTestState extends State<CommentTest> {
  7. TextEditingController _textEditingController = TextEditingController();
  8. String _currentTipsText = "有爱评论,说点儿好听的~";
  9. FocusNode _commentFocus = FocusNode();
  10. List<Map> _commentList = [
  11. {
  12. 'name': '涂山雏雏',
  13. 'headerImg': 'http://i2.hdslb.com/bfs/face/cab3e9ec886ff98bc7ac6cb2dca194051895dfba.jpg@52w_52h.webp',
  14. 'content': '你以为我是红细胞,其实我是兵库北哒(`・ω・´)~'
  15. },
  16. {
  17. 'name': '漆黑的魂焰魔法使',
  18. 'headerImg': 'http://i0.hdslb.com/bfs/face/6edd973203eb1ec2b576a3bc61ee555e3757b674.jpg@52w_52h.webp',
  19. 'content': '你说我的头发怎么了啊,啊!'
  20. },
  21. {
  22. 'name': '汐华初流艿',
  23. 'headerImg': 'http://i0.hdslb.com/bfs/face/ecf4c932d4f09ffdcd769b423764210488d03209.jpg@52w_52h.webp',
  24. 'content': '你们因为你们身体里面真的有萌妹子吗,其实全都是dio哒'
  25. }
  26. ];
  27. @override
  28. Widget build(BuildContext context) {
  29. return Scaffold(
  30. appBar: AppBar(
  31. title: Text('点击留言输入框获取焦点',style: TextStyle(color: Colors.white,fontSize: 20),),
  32. ),
  33. body: Stack(
  34. children: <Widget>[
  35. ListView.builder(
  36. itemCount: _commentList.length,
  37. itemBuilder: (context,index){
  38. return ListTile(
  39. leading: ClipRRect(borderRadius: BorderRadius.circular(20),child: Image.network(_commentList[index]['headerImg'],width: 40,height: 40,),),
  40. title: Text(_commentList[index]['name']),
  41. subtitle: Text(_commentList[index]['content']),
  42. onTap: (){
  43. _switchReply(_commentList[index]['name']);
  44. },
  45. );
  46. },
  47. ),
  48. Positioned(
  49. left: 0,
  50. bottom: 0,
  51. child: Container(
  52. width: MediaQuery.of(context).size.width,
  53. color: Color.fromRGBO(222, 222, 222, 1),
  54. height: 50,
  55. child: Row(
  56. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  57. crossAxisAlignment: CrossAxisAlignment.center,
  58. children: <Widget>[
  59. Expanded(
  60. child: TextField(
  61. controller: _textEditingController,
  62. focusNode: _commentFocus,
  63. decoration: InputDecoration(
  64. hintText: _currentTipsText,
  65. contentPadding: EdgeInsets.only(left: 10,top: 17,bottom: 17),
  66. ),
  67. ),
  68. ),
  69. InkWell(
  70. child: Container(
  71. width: 50,
  72. height: 50,
  73. alignment: Alignment.center,
  74. child: Icon(Icons.near_me,size: 25.5,color: Color.fromRGBO(50, 50, 50, 1)),
  75. ),
  76. onTap: (){
  77. _sendMessage();
  78. },
  79. ),
  80. ],
  81. ),
  82. ),
  83. ),
  84. ],
  85. )
  86. );
  87. }
  88. // 发送回复评论
  89. void _sendMessage() {
  90. _commentList.add({
  91. 'name': '爱吃汉堡包的天残',
  92. 'headerImg': 'http://i1.hdslb.com/bfs/face/1cb09a8cfec19bd06fbbeba5b978c1ee52a62d3f.jpg@52w_52h.webp',
  93. 'content': _textEditingController.text
  94. });
  95. _currentTipsText = "有爱评论,说点儿好听的~";
  96. _textEditingController.text = '';
  97. _commentFocus.unfocus(); // 失去焦点
  98. }
  99. // 获取焦点拉起键盘
  100. void _switchReply(nickname) {
  101. setState(() {
  102. _currentTipsText = '回复 '+nickname+':';
  103. });
  104. FocusScope.of(context).requestFocus(_commentFocus); // 获取焦点
  105. }
  106. }

转自:https://www.cnblogs.com/gxsyj/p/11156323.html