TextField

时间:2021-06-25 04:01:42
TextFiled 是一个输入Widget,属性如下:
this.controller,//这个是传输数据用的
this.focusNode,
this.decoration = const InputDecoration(),这个是UI设置用的
TextInputType keyboardType,这个可以设置输入键盘是数字还是字符;
this.textInputAction,
this.textCapitalization = TextCapitalization.none,
this.style,
this.strutStyle,
this.textAlign = TextAlign.start,
this.textDirection,
this.autofocus = false,
this.obscureText = false,
this.autocorrect = true,
this.maxLines = 1,
this.minLines,
this.expands = false,
this.maxLength,
this.maxLengthEnforced = true,
this.onChanged, 这个是回调函数
this.onEditingComplete,
this.onSubmitted,
this.inputFormatters,
this.enabled,
this.cursorWidth = 2.0,
this.cursorRadius,
this.cursorColor,
this.keyboardAppearance,
this.scrollPadding = const EdgeInsets.all(20.0),
this.dragStartBehavior = DragStartBehavior.start,
this.enableInteractiveSelection,
this.onTap,
this.buildCounter,
下面是一个完整的示例:
import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart'; void main() => runApp(MyApp()); class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: ExampleWidget(),
),
);
}
}
/// Opens an [AlertDialog] showing what the user typed.
class ExampleWidget extends StatefulWidget {
@override
_ExampleWidgetState createState() => new _ExampleWidgetState();
} class _ExampleWidgetState extends State<ExampleWidget> {
final TextEditingController _controller = TextEditingController();
@override
Widget build(BuildContext context) {
return Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
new TextField(
controller: _controller,
decoration: InputDecoration(
contentPadding: EdgeInsets.all(10.0),
labelText:'Be noticed to write something down',
hintText: 'Type something',
),
onChanged: (text){setState(() {
print(text);
});},
),
new RaisedButton(
onPressed: () {
showDialog(
context: context,
builder: (context){
return AlertDialog(
title:Text('What you typed'),
content:Text(_controller.text),
);
}
);
},
child: new Text('DONE'),
),
],
);
}
}