Flutter基礎(chǔ)組件: TextFiled

TextFiled

TextFiled的簡單使用

class _MyHomeApp extends StatefulWidget {
  const _MyHomeApp({Key? key}) : super(key: key);

  @override
  State<_MyHomeApp> createState() => _MyHomeAppState();
}

class _MyHomeAppState extends State<_MyHomeApp> {
  String userName = '';
  String password = "";
  // 綁定在textFiled上, 用于獲取textFiled輸入的文本
  final TextEditingController _userNameController = TextEditingController();

  // 綁定在textFiled上, 用于獲取textFiled輸入的文本
  final TextEditingController _passwordController = TextEditingController();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: const Text("設(shè)置"),
          centerTitle: true,
        ),
        body: Column(
          children: [
            TextField(
              autofocus: true,
              controller: _userNameController,
              decoration: const InputDecoration(
                labelText: '用戶名',
                hintText: '手機號/郵箱',
                prefixIcon: Icon(Icons.person),
              ),
            ),
            TextField(
              controller: _passwordController,
              decoration: const InputDecoration(
                  labelText: '密碼',
                  hintText: '請輸入密碼',
                  prefixIcon: Icon(Icons.lock)),
                  obscureText: true,
            ),
            Text('賬號:::$userName'),
            Text('密碼:::$password'),
            ElevatedButton(
                onPressed: () {
                  setState(() {
                    userName = _userNameController.text;
                    password = _passwordController.text;
                  });
                },
                child: const Text('登錄'))
          ],
          crossAxisAlignment: CrossAxisAlignment.center,
        ));
  }
}
效果

自定義TextFiled樣式

隱藏icon

不設(shè)置prefixIcon即可

設(shè)置label的樣式
  • 隱藏label, 不設(shè)置labelText即可
  • 修改labelText樣式, 在decoration里設(shè)置
labelStyle: TextStyle(color: Colors.green),
設(shè)置輸入內(nèi)容的樣式
TextField(
   style: const TextStyle(color: Colors.red),
)
修改下劃線的效果, 在decoration里設(shè)置樣式
// 未獲得焦點下劃線設(shè)為灰色
enabledBorder: UnderlineInputBorder(
  borderSide: BorderSide(color: Colors.grey),
),
//獲得焦點下劃線設(shè)為藍色
focusedBorder: UnderlineInputBorder(
  borderSide: BorderSide(color: Colors.red),
),

Form表單

flutter里, Form表單的TextFiledTextFormFiled組件

class _MyHomeAppState extends State<_MyHomeApp> {
  String userName = '';
  String password = "";

  // 綁定在textFiled上, 用于獲取textFiled輸入的文本
  final TextEditingController _userNameController = TextEditingController();

  // 綁定在textFiled上, 用于獲取textFiled輸入的文本
  final TextEditingController _passwordController = TextEditingController();

  // form的key, 用于獲取state, 獲取state以后校驗規(guī)則
  final GlobalKey _globalKey = GlobalKey<FormState>();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: const Text("設(shè)置"),
          centerTitle: true,
        ),
        body: Form(
            key: _globalKey,
            autovalidateMode: AutovalidateMode.onUserInteraction,
            child: Column(
              children: [
                TextFormField(
                  autofocus: true,
                  controller: _userNameController,
                  decoration: const InputDecoration(
                    labelText: '用戶名',
                    hintText: '手機號/郵箱',
                    prefixIcon: Icon(Icons.person),
                  ),
                  validator: (v) {
                    return v!.trim().isNotEmpty ? null : "用戶名不能為空";
                  },
                ),
                TextFormField(
                  controller: _passwordController,
                  decoration: const InputDecoration(
                      labelText: '密碼',
                      hintText: '請輸入密碼',
                      prefixIcon: Icon(Icons.lock)),
                  obscureText: true,
                  validator: (v) {
                    return v!.trim().length > 5 ? null : "密碼不能少于6位";
                  },
                ),
                Text('賬號:::$userName'),
                Text('密碼:::$password'),
                ElevatedButton(
                    onPressed: () {
                      setState(() {
                        if ((_globalKey.currentState as FormState).validate()) {
                          userName = _userNameController.text;
                          password = _passwordController.text;
                        }
                      });
                    },
                    child: const Text('登錄'))
              ],
              crossAxisAlignment: CrossAxisAlignment.center,
            )));
  }
}
form
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容