import 'package:flutter/material.dart'; void main() { runApp(const MyApp()); } class MyApp extends StatelessWidget { const MyApp({Key? key}) : super(key: key); @override Widget build(BuildContext context) { return MaterialApp( title: 'Flutter Demo', theme: ThemeData( primarySwatch: Colors.blue, ), home: Scaffold( appBar: AppBar( title: const Text('Flutter Demo'), ), body: Center( child: Container( padding: const EdgeInsets.all(8), child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ const Text('You have pushed the button this many times:'), TextField( focusNode: FocusNode(), decoration: const InputDecoration( icon: Icon(Icons.search), hintText: '请输入内容', border: InputBorder.none, ), onChanged: (value) { print(value); }, ), const SizedBox( height: 10, ), TextField( obscureText: true, decoration: const InputDecoration( icon: Icon(Icons.password), border: OutlineInputBorder( borderSide: BorderSide( width: 0, style: BorderStyle.none, ), ), filled: true, // fillColor: Color.fromARGB(255, 21, 69, 25), hintText: '请输入密码', ), onChanged: (value) { print(value); }, ), Container( height: 50, width: double.infinity, margin: const EdgeInsets.all(10), decoration: const BoxDecoration( borderRadius: BorderRadius.all(Radius.circular(10)), color: Colors.black12, ), child: TextButton( style: ButtonStyle( foregroundColor: MaterialStateProperty.all(Colors.black), backgroundColor: MaterialStateProperty.all(Colors.black), ), onPressed: () {}, child: const Text( '登录按钮', style: TextStyle(color: Colors.white), ), ), ), ], ), ), ), ), ); } }
TextField 是 Flutter 提供的文本输入框,TextField 的属性非常多,常用的属性如下:
- keyboardType:键盘类型,可以指定是数字、字母、电话号码、邮箱、日期等多种方式,通过与表单内容匹配的键盘类型可以提供输入效率,进而改善用户体验。
- controller:TextEditingController 对象,TextEditingController 主要用于控制文本框的初始值,清除内容的操作。
- obscureText:是否需要隐藏输入内容,如果为 true,则输入内容会使用圆点显示,通常用与密码。
- decoration:文本框的装饰,属性也很多,可以指定前置图标,边框类型、后置组件等多种属性,因此可以通过 decoration 获得想要的文本框样式。
- focusNode:聚焦点,可以通过这个来控制文本框是否获取焦点,从而实现类似上一个下一个的输入控制。
- onChanged:输入值改变事件回调,通常用这个方法实现双向绑定。
TextButton 包含了样式ButtonStyle可以自定义Button