Flutter:input输入框

输入框:

js 复制代码
// 是否显示关闭按钮
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), // 文字颜色
)

实现下图功能的完整代码


js 复制代码
class SearchBar extends StatefulWidget {
  @override
  State<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;
      });
    }
  }

  @override
  Widget 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('取消'),
                )
              ],
            ),
          )
        ],
      ),
    );
  }
}
相关推荐
fatiaozhang95271 小时前
中兴B860AV1.1_晨星MSO9280芯片_4G和8G闪存_TTL-BIN包刷机固件包
android·linux·adb·电视盒子·av1·魔百盒刷机
fatiaozhang95272 小时前
中兴B860AV1.1_MSO9280_降级后开ADB-免刷机破解教程(非刷机)
android·adb·电视盒子·av1·魔百盒刷机·移动魔百盒·魔百盒固件
二流小码农2 小时前
鸿蒙开发:绘制服务卡片
android·ios·harmonyos
微信公众号:AI创造财富2 小时前
adb 查看android 设备的硬盘及存储空间
android·adb
码农果果3 小时前
Google 提供的一组集成测试套件----XTS
android
火柴就是我3 小时前
每日见闻之Rust中的引用规则
android
vvilkim3 小时前
Flutter 常用组件详解:Text、Button、Image、ListView 和 GridView
前端·flutter
getapi3 小时前
flutter把 pubspec.yaml 中的name改成了新的值
flutter·macos·cocoa
vvilkim3 小时前
Flutter 命名路由与参数传递完全指南
前端·flutter