自定义组合控件
- LoginInputText代码如下:
import 'package:flutter/material.dart';
class LoginInputText extends StatelessWidget {
final Icon icon;
final imageName;
final text;
final hintText;
final double width;
final double height;
final Color color;
final double size;
final double hintSize;
final bool obscureText;
final textValue;
final TextEditingController controller;
const LoginInputText({
Key key,
this.icon,
this.text,
this.width = 200.0,
this.height = 44.0,
this.color,
this.size = 14.0,
this.imageName,
this.hintText = '',
this.hintSize = 14.0,
this.obscureText = false,
this.textValue,
this.controller,
}) : super(key: key);
@override
Widget build(BuildContext context) {
return new Container(
width: width,
height: height,
decoration: new BoxDecoration(
color: color,
),
child: new Row(
children: <Widget>[
new Image.asset(
imageName,
width: 24.0,
height: 24.0,
),
//new Icon(Icons.assessment),
new Container(
width: 10.0,
),
new Text(text),
new Container(
width: 5.0,
),
new Expanded(
child: new TextField(
keyboardType: TextInputType.text,
decoration: InputDecoration(
hintText: hintText,
hintStyle: new TextStyle(
fontSize: hintSize,
),
border: InputBorder.none,
),
obscureText: obscureText,
autofocus: false,
controller: controller,
/*onChanged: (value) {
controller.text = value;
print('onChanged:$value');
},*/
),
),
],
),
);
}
}
- 使用方式如下
TextEditingController _name = TextEditingController();
TextEditingController _pass = TextEditingController();
new LoginInputText(
width: 250.0,
height: 44.0,
text: '帐号: ',
//color: Colors.yellow,
imageName: 'images/me.png',
hintText: '请输入用户名',
controller: _name,
),
new LoginInputText(
width: 250.0,
height: 44.0,
text: '密码: ',
//color: Colors.yellow,
imageName: 'images/icon_password.png',
hintText: '请输入密码',
obscureText: true,
controller: _pass,
),
- 使用TextEditingController获取TextField输入的数据
- 在使用TextField添加onChanged:(value){}方法会导致光标位置始终在0的位置
源码地址https://github.com/davidxuqinjun/flutterDemo/blob/master/lib/login/Login.dart
好记性不如烂笔头_记录学习中的坑
网友评论