当前位置: 首页 > news >正文

Flutter:input输入框

输入框:

// 是否显示关闭按钮
bool _showClear = false;
// 文字编辑控制器,监听搜索框的变化。
final TextEditingController _controller = TextEditingController();
// 输入框发生变化事件
void _onChange(String value){if(value.length > 0){setState(() {_showClear = true;});}else{setState(() {_showClear = false;});}
}TextField(controller: _controller, // 文字编辑控制器,配合_onChange方法使用onChanged: _onChange, // 监听输入框的变化autofocus: true, // 是否自动聚焦光标cursorColor: Colors.green, // 默认边框的颜色decoration: const InputDecoration( // 装饰器contentPadding: EdgeInsets.only(left: 10, bottom: 10), // 内容偏移border: InputBorder.none, // 隐藏默认边框hintText: '搜索', // 默认提示问题),style: const TextStyle(fontSize: 18, fontWeight: FontWeight.w300, color: Colors.black), // 文字颜色
)

实现下图功能的完整代码

在这里插入图片描述
在这里插入图片描述

class SearchBar extends StatefulWidget {@overrideState<SearchBar> createState() => SearchBarState();
}class SearchBarState extends State<SearchBar> {// 是否显示关闭按钮bool _showClear = false;// 文字编辑控制器,监听搜索框的变化。final TextEditingController _controller = TextEditingController();// 输入框发生变化事件void _onChange(String value){if(value.length > 0){setState(() {_showClear = true;});}else{setState(() {_showClear = false;});}}@overrideWidget build(BuildContext context) {return Container(height: 84,color: mainThemeColor,child: Column(children: [const SizedBox(height: 40,), // 上半部分留空,时间栏Container(height: 44,color: Colors.redAccent,child: Row(children: [Container(width: screenWidth(context) - 60,height: 34,margin: const EdgeInsets.only(left: 10),padding: const EdgeInsets.only(left: 10, right: 10),decoration: BoxDecoration(color: Colors.white, borderRadius: BorderRadius.circular(6.0)),child: Row(mainAxisAlignment: MainAxisAlignment.spaceBetween,children: [const Image(image: AssetImage('images/icon.png'),width: 20,color: Colors.grey,),Expanded(flex: 1,child: TextField(controller: _controller, // 文字编辑控制器,配合_onChange方法使用onChanged: _onChange, // 监听输入框的变化autofocus: true, // 是否自动聚焦光标cursorColor: Colors.green, // 默认边框的颜色decoration: const InputDecoration( // 装饰器contentPadding: EdgeInsets.only(left: 10, bottom: 10), // 内容偏移border: InputBorder.none, // 隐藏默认边框hintText: '搜索', // 默认提示问题),style: const TextStyle(fontSize: 18, fontWeight: FontWeight.w300, color: Colors.black), // 文字颜色)),_showClear ? GestureDetector(onTap: (){setState(() {_controller.clear();// 只会把内容清空,不会触发_onChange回调_onChange('');});},child: const Icon(Icons.close,color: Colors.grey,weight: 20,),) : Container()],),),const SizedBox(width: 10,),GestureDetector(onTap: (){Navigator.pop(context);},child: const Text('取消'),)],),)],),);}
}
http://www.lryc.cn/news/485123.html

相关文章:

  • RabbitMQ 与 PHP Swoole 实现
  • 【计算机体系架构】 MESI缓冲一致性
  • STM32设计学生宿舍监测控制系统
  • 企业生产环境-麒麟V10(ARM架构)操作系统部署kafka高可用集群
  • awk(常用)
  • Amazon Web Services (AWS)
  • Java EE 技术基础知识体系梳理
  • 【2025最新计算机毕业设计】基于SpringBoot+Vue电脑在线装机指南教程网站【源码+文档】
  • HDFS新增节点和删除datanode节点
  • 数据结构-线性表-具有独立头节点的双向循环链表
  • CSS 响应式设计之媒体查询技术
  • HARCT 2025 分论坛4:智能系统传感、传感器开发和数据融合中的智能数据分析
  • 云计算研究实训室建设方案
  • VRT: 关于视频修复的模型
  • 实习冲刺Day22
  • datawhale2411组队学习之模型压缩技术1:模型剪枝
  • 高防服务器的费用受到哪些原因影响?
  • 中断和异常处理,嵌入式行业的门槛?
  • latex中英文环境中双引号怎么输入
  • 用 Python 从零开始创建神经网络(三):添加层级(Adding Layers)
  • 前端知识点---构造函数(javascript)
  • Nginx 上安装 SSL 证书并启用 HTTPS 访问
  • 谷歌Gemini发布iOS版App,live语音聊天免费用!
  • docker运行ActiveMQ-Artemis
  • 90.选择排序(理论分析)
  • GitLab 如何跨版本升级?
  • 【Fermat】费马小定理
  • NVMe(Non-Volatile Memory Express)非易失性存储器访问和传输协议
  • C++初阶——queue
  • 达梦数据库迁移j脚本