Flutter---TextField 概念TextField 是 Flutter 中用于文本输入的核心组件它不仅仅是一个输入框而是一个完整的文本编辑系统包含输入控制、焦点管理、文本编辑、验证、样式定制等完整功能源码层级// TextField 的架构层级 TextField (StatefulWidget) ↓ _TextFieldState ↓ EditableText (StatefulWidget) ← 真正的文本编辑组件 ↓ RenderEditable (RenderObject) ↓ dart:ui Paragraph TextInputConnection (平台输入法) 理解 //TextField 是 EditableText 的高级封装 //EditableText 才是真正的文本编辑组件 //通过 TextInputConnection 与平台输入法通信 //底层使用 Paragraph 渲染文本用 TextSelection 管理选择基础使用// 定义控制器 final TextEditingController _usernameInputController TextEditingController(); TextField( controller: _usernameInputController, // 控制器 decoration: InputDecoration( hintText: 请输入用户名,//提示文字 border: OutlineInputBorder(), //默认情况未点击时边框是灰色的点击后边框是蓝色的 filled: true, //启用填充 fillColor: Colors.green.withOpacity(0.5),//输入框颜色 contentPadding: EdgeInsets.all(16), // 内边距 //prefixIcon: Icon(Icons.person), // 前缀图标 //suffixIcon: IconButton( // 后缀图标可点击 // icon: Icon(Icons.clear),//如果做登录按钮可以用这个做获取验证码按钮 // onPressed: () {}, //), enabledBorder: OutlineInputBorder( //未点击的边框颜色 borderSide: BorderSide(color: Colors.grey), borderRadius: BorderRadius.circular(26),//给输入框设置圆角 ), focusedBorder: OutlineInputBorder( //点击后的边框颜色 borderSide: BorderSide(color: Colors.red, width: 2), borderRadius: BorderRadius.circular(26),//给输入框设置圆角 ), counterText: ,// 隐藏计数器 ), // obscureText: true, // 隐藏文本用于密码 // obscuringCharacter: ●, // 隐藏字符默认是圆点 maxLength: 8, // 最大长度 style: TextStyle( //输入框的文本样式 fontSize: 16, color: Colors.purple, fontWeight: FontWeight.bold, ), cursorColor: Colors.pink, // 光标颜色 cursorWidth: 1.0, // 光标宽度 cursorHeight: 20.0, // 光标高度 cursorRadius: Radius.circular(2), // 光标圆角 showCursor: true, // 显示光标 )小案例实现获取验证码行效果图代码使用suffixIcon实现获取验证码按钮//验证码 Widget buildVerificationCode() { return TextField( controller: _verificationCodeController, decoration: InputDecoration( hintText: 请输验证码, hintStyle: TextStyle( color: Color(0xFF111111).withOpacity(0.6), ), filled: true, fillColor: Colors.white, // 添加前置图标 prefixIcon: Icon( Icons.lock, // 可以替换为其他图标比如 Icons.vpn_key, Icons.security 等 color: Colors.black.withOpacity(0.5), size: 20, ), // 或者添加后置图标二选一或同时使用 // suffixIcon: Icon( // Icons.check_circle, // color: Colors.green, // size: 20, // ), enabledBorder: OutlineInputBorder( borderSide: BorderSide(color: Colors.transparent), borderRadius: BorderRadius.circular(26), ), focusedBorder: OutlineInputBorder( borderSide: BorderSide(color: Colors.transparent), borderRadius: BorderRadius.circular(26), ), suffixIcon: SizedBox( child: _buildVerificationCodeButton(), ), counterText: , ), maxLength: 6, //验证码可能是6位可能是4位 style: TextStyle( fontSize: 12, color: Colors.black, ), cursorColor: Colors.black, cursorWidth: 2.0, ); } // 构建验证码按钮 Widget _buildVerificationCodeButton() { return Container( child: ElevatedButton( onPressed: _getVerificationCode, style: ElevatedButton.styleFrom( backgroundColor: Color(0xFF2188FF), foregroundColor: Colors.white, shape: RoundedRectangleBorder( borderRadius: BorderRadius.circular(26), ), ), child: Text( 获取验证码, style: TextStyle( fontSize: 12, fontWeight: FontWeight.w500, ), ), ), ); }

相关新闻

最新新闻

SerenityOS 命令行选项解析指南:getopt 与 getopt_long 用法、返回值与底层实现

SerenityOS 命令行选项解析指南:getopt 与 getopt_long 用法、返回值与底层实现

SerenityOS 命令行选项解析指南:getopt 与 getopt_long 用法、返回值与底层实现 【免费下载链接】serenity The Serenity Operating System 🐞 项目地址: https://gitcode.com/GitHub_Trending/se/serenity 导读 本文以 getopt(3) 手册 为核心&a…

2026/10/6 14:10:51
轻量服务器还是ECS?大促云服务器选购与避坑实战指南

轻量服务器还是ECS?大促云服务器选购与避坑实战指南

每年大促节点,群里永远有人在问同一个问题:“38元的轻量服务器到底怎么抢?为什么我每次点进去都是已售罄?68元直购和99元的ECS我到底选哪个?”作为一个常年帮团队和自己采购云服务器的老用户,我太清楚这种纠…

2026/10/6 12:50:27
为 AI 代理的 Review 动作编写 Cedar 审批门控策略:review-agent-governance 策略编写实战指南

为 AI 代理的 Review 动作编写 Cedar 审批门控策略:review-agent-governance 策略编写实战指南

为 AI 代理的 Review 动作编写 Cedar 审批门控策略:review-agent-governance 策略编写实战指南 【免费下载链接】agents Multi-harness agentic plugin marketplace for Claude Code, Codex, Cursor, OpenCode, GitHub Copilot, and Google Antigravity 项目地址:…

2026/10/5 19:39:38
PaddleOCR 手写数学公式识别算法 CAN 实战指南:Counting-Aware Network 训练、评估与推理部署

PaddleOCR 手写数学公式识别算法 CAN 实战指南:Counting-Aware Network 训练、评估与推理部署

PaddleOCR 手写数学公式识别算法 CAN 实战指南:Counting-Aware Network 训练、评估与推理部署 【免费下载链接】PaddleOCR Turn any PDF or image document into structured data for your AI. A powerful, lightweight OCR toolkit that bridges the gap between i…

2026/10/5 16:06:34
Spring源码解析:构造器注入的类型转换与候选匹配机制

Spring源码解析:构造器注入的类型转换与候选匹配机制

/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views …

2026/10/6 12:44:38
openai-agents-python 多模型接入指南:深入解析 AnyLLMModel 适配层与 any-llm 路由

openai-agents-python 多模型接入指南:深入解析 AnyLLMModel 适配层与 any-llm 路由

openai-agents-python 多模型接入指南:深入解析 AnyLLMModel 适配层与 any-llm 路由 【免费下载链接】openai-agents-python A lightweight, powerful framework for multi-agent workflows 项目地址: https://gitcode.com/GitHub_Trending/op/openai-agents-pyth…

2026/10/6 12:38:14

日新闻

周新闻

月新闻