flutter开发----图标库的使用
前言
今天想写一个注册登录页面,遇到图标不知道怎么处理,后来找到了一个插件感觉挺不错的,推荐给大家
准备工具
这套课程是采用Android Studio进行开发的。当前在此之前请准备好Flutter开发环境,我这里就不进行讲解了。
展示结果
导入插件
font_awesome_flutter这个就是今天的主角了,我今天遇到QQ,微信的图标该怎么导入,百度以后,发现了这个插件,挺不错的,大家可以到官网去找自己想要的icon
首先导入我们的图标
font_awesome_flutter: ^8.4.0
使用插件
//引入插件
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
//具体的使用
{
"title": "QQ",
"icon": FontAwesomeIcons.qq,
},
{
"title": "微信",
"icon": FontAwesomeIcons.weixin,
},
注册页面
这里就做一个小实例,布局一个注册页面
import 'package:flutter/material.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
class LoginPage extends StatefulWidget {
@override
_LoginPageState createState() => _LoginPageState();
}
class _LoginPageState extends State<LoginPage> {
final _formKey = GlobalKey<FormState>();
String _email, _password;
bool _isObscure = true;
Color _eyeColor;
List _loginMethod = [
{
"title": "QQ",
"icon": FontAwesomeIcons.qq,
},
{
"title": "微信",
"icon": FontAwesomeIcons.weixin,
},
];
@override
Widget build(BuildContext context) {
return Scaffold(
body: Form(
key: _formKey,
child: ListView(
padding: EdgeInsets.symmetric(horizontal: 22.0),
children: <Widget>[
SizedBox(
height: kToolbarHeight,
),
buildTitle(),
buildTitleLine(),
SizedBox(height: 70.0),
buildEmailTextField(),
SizedBox(height: 30.0),
buildPasswordTextField(context),
buildForgetPasswordText(context),
SizedBox(height: 60.0),
buildLoginButton(context),
SizedBox(height: 30.0),
buildOtherLoginText(),
buildOtherMethod(context),
buildRegisterText(context),
],
)));
}
Align buildRegisterText(BuildContext context) {
return Align(
alignment: Alignment.center,
child: Padding(
padding: EdgeInsets.only(top: 10.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text('没有账号?'),
GestureDetector(
child: Text(
'点击注册',
style: TextStyle(color: Colors.green),
),
onTap: () {
//TODO 跳转到注册页面
print('去注册');
Navigator.pop(context);
},
),
],
),
),
);
}
ButtonBar buildOtherMethod(BuildContext context) {
return ButtonBar(
alignment: MainAxisAlignment.center,
children: _loginMethod
.map((item) => Builder(
builder: (context) {
return IconButton(
icon: Icon(item['icon'],
color: Theme.of(context).iconTheme.color),
onPressed: () {
//TODO : 第三方登录方法
Scaffold.of(context).showSnackBar(new SnackBar(
content: new Text("${item['title']}登录"),
action: new SnackBarAction(
label: "取消",
onPressed: () {},
),
));
});
},
))
.toList(),
);
}
Align buildOtherLoginText() {
return Align(
alignment: Alignment.center,
child: Text(
'其他账号登录',
style: TextStyle(color: Colors.grey, fontSize: 14.0),
));
}
Align buildLoginButton(BuildContext context) {
return Align(
child: SizedBox(
height: 45.0,
width: 270.0,
child: RaisedButton(
child: Text(
'Login',
style: Theme.of(context).primaryTextTheme.headline,
),
color: Colors.white,
onPressed: () {
if (_formKey.currentState.validate()) {
///只有输入的内容符合要求通过才会到达此处
_formKey.currentState.save();
//TODO 执行登录方法
print('email:$_email , assword:$_password');
}
},
shape: StadiumBorder(side: BorderSide()),
),
),
);
}
Padding buildForgetPasswordText(BuildContext context) {
return Padding(
padding: const EdgeInsets.only(top: 8.0),
child: Align(
alignment: Alignment.centerRight,
child: FlatButton(
child: Text(
'忘记密码?',
style: TextStyle(fontSize: 14.0, color: Colors.grey),
),
onPressed: () {
Navigator.pop(context);
},
),
),
);
}
TextFormField buildPasswordTextField(BuildContext context) {
return TextFormField(
onSaved: (String value) => _password = value,
obscureText: _isObscure,
validator: (String value) {
if (value.isEmpty) {
return '请输入密码';
}
},
decoration: InputDecoration(
labelText: 'Password',
suffixIcon: IconButton(
icon: Icon(
Icons.remove_red_eye,
color: _eyeColor,
),
onPressed: () {
setState(() {
_isObscure = !_isObscure;
_eyeColor = _isObscure
? Colors.grey
: Theme.of(context).iconTheme.color;
});
})),
);
}
TextFormField buildEmailTextField() {
return TextFormField(
decoration: InputDecoration(
labelText: 'Emall Address',
),
validator: (String value) {
var emailReg = RegExp(
r"[\w!#$%&'*+/=?^_`{|}~-]+(?:\.[\w!#$%&'*+/=?^_`{|}~-]+)*@(?:[\w](?:[\w-]*[\w])?\.)+[\w](?:[\w-]*[\w])?");
if (!emailReg.hasMatch(value)) {
return '请输入正确的邮箱地址';
}
},
onSaved: (String value) => _email = value,
);
}
Padding buildTitleLine() {
return Padding(
padding: EdgeInsets.only(left: 12.0, top: 4.0),
child: Align(
alignment: Alignment.bottomLeft,
child: Container(
color: Colors.black,
width: 40.0,
height: 2.0,
),
),
);
}
Padding buildTitle() {
return Padding(
padding: EdgeInsets.all(8.0),
child: Text(
'Login',
style: TextStyle(fontSize: 42.0),
),
);
}
}
今天就分享到这里,希望给大家带来这个插件的学习