6.1 输入框的使用
1.源代码
import 'package:flutter/material.dart';
void main () => runApp(MyApp());
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
TextField(
autofocus: true,
decoration: InputDecoration(
prefixIcon:Icon(Icons.perm_identity),
labelText: "用户名",
hintText: "请输入用户名",
hintStyle: TextStyle(
color: Colors.grey,
),
),
),
TextField(
obscureText: true,
decoration: InputDecoration(
prefixIcon:Icon(Icons.lock),
labelText: "密码",
hintText: "请输入密码",
hintStyle: TextStyle(
color: Colors.grey,
),
),
),
],
),
)
)
);
}
}
2.解释源代码
import 'package:flutter/material.dart';
void main () => runApp(MyApp());
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
//输入框
TextField(
//自动获取焦点
autofocus: true,
//输入框修饰
decoration: InputDecoration(
//前置Icon
prefixIcon:Icon(Icons.perm_identity),
//输入框文本
labelText: "用户名",
//输入框提示文本
hintText: "请输入用户名",
//输入框提示文本样式
hintStyle: TextStyle(
color: Colors.grey,
),
),
),
TextField(
//输入文本不可见,用于输入密码 以····表示
obscureText: true,
decoration: InputDecoration(
prefixIcon:Icon(Icons.lock),
labelText: "密码",
hintText: "请输入密码",
hintStyle: TextStyle(
color: Colors.grey,
),
),
),
],
),
)
)
);
}
}
3.效果图