6. Flutter - 基础组件 之TextField

2021-03-16  本文已影响0人  Aliv丶Zz

TextField用于接收用户的文本输入,属性介绍。
常见的属性:

代码示例:


import 'package:flutter/material.dart';

main(List<String> args) {
  // imageCache : 图片缓存类
  // imageCache.clear();
  runApp(MaterialApp(
    home: Scaffold(
      appBar: AppBar(
        title: Text('基本部件 - TextField'),
      ),
      body: Center(
        child: TextFieldDemo(),
      ),
    ),
  ));
}

class TextFieldDemo extends StatefulWidget {
  @override
  _TextFieldDemoState createState() => _TextFieldDemoState();
}

class _TextFieldDemoState extends State<TextFieldDemo> {
  final nameC = TextEditingController();
  final passwordC = TextEditingController();

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        //姓名
        UserNameField(nameC),
        //密码
        PasswordField(passwordC),
        //登录
        Container(
          width: 180,
          height: 44,
          child: TextButton(
            style: ButtonStyle(
              textStyle: MaterialStateProperty.all(TextStyle()),
              backgroundColor:
                  MaterialStateColor.resolveWith((states) => Colors.red),
            ),
            onPressed: () {
              print('commit ${nameC.text}-${passwordC.text}');
            },
            child: Text('登录'),
          ),
        )
      ],
    );
    ;
  }
}

// ignore: must_be_immutable
class UserNameField extends StatelessWidget {
  TextEditingController userNameController;

  UserNameField(TextEditingController controller) {
    userNameController = controller;
  }

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: EdgeInsets.all(18),
      child: TextField(
        controller: userNameController,
        decoration: InputDecoration(
          labelText: '姓名',
          hintText: '请输入',
          hintStyle: TextStyle(color: Colors.green),
          icon: Icon(Icons.people),
          border: OutlineInputBorder(
              borderSide: BorderSide(
                  color: Colors.red, width: 0.1, style: BorderStyle.none)),
          filled: true,
          fillColor: Colors.lightGreen[50], // 需设置filled为true才可生效
          hoverColor: Colors.red,
        ),
        onChanged: (value) {
          print('姓名: $value');
        },
        onSubmitted: (value) {
          print('姓名提交: $value');
        },
      ),
    );
  }
}

// ignore: must_be_immutable
class PasswordField extends StatelessWidget {
  TextEditingController passwordController;

  PasswordField(TextEditingController controller) {
    passwordController = controller;
  }

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: EdgeInsets.all(18),
      child: TextField(
        controller: passwordController,
        decoration: InputDecoration(
          labelText: '密码',
          hintText: '请输入',
          hintStyle: TextStyle(color: Colors.green),
          icon: Icon(Icons.lock),
          border: OutlineInputBorder(
              borderSide: BorderSide(
                  color: Colors.red, width: 0.1, style: BorderStyle.none)),
          filled: true,
          fillColor: Colors.lightGreen[50], // 需设置filled为true才可生效
          hoverColor: Colors.red,
        ),
        onChanged: (value) {
          print('密码: $value');
        },
        onSubmitted: (value) {
          print('密码提交: $value');
        },
      ),
    );
  }
}
效果图.png
上一篇 下一篇

猜你喜欢

热点阅读