Flutter实战一Flutter聊天应用(十二)
由于当前项目的账号是直接使用Google账户,iOS系统问题不大,但是Android系统如果没有Google框架,则无法使用我们的应用程序。因此,我们需要创建自己的账户数据。在这篇文章中,我们会创建一个登陆屏幕和注册屏幕,两个屏幕的UI如下图所示: 关于UI布局的内容不是这篇文章的重点,所以不会具体描述,只会展示代码并陈述布局思路。有关UI布局的内容可以查看《Flutter进阶—构建布局实例》、《Flutter进阶—布局方法演示》、《Flutter进阶—布局一个控件》、《Flutter进阶—垂直和水平布局》等文章。 实现登陆屏幕首先添加三个图像资源,分别为登陆屏幕的背景、注册屏幕的背景和应用程序的Logo,具体的添加方法可以查看《Flutter基础—常用控件之图片》。 我们在项目的 import 'package:flutter/material.dart';
class SignIn extends StatefulWidget {
@override
State createState() => new SignInState();
}
class SignInState extends State<SignIn> {
@override
Widget build(BuildContext context) {
return new Scaffold(
body: new Stack(children: <Widget>[
new Opacity(
opacity: 0.3,child: new Container(
decoration: new BoxDecoration(
image: new DecorationImage(
image: new ExactAssetImage('images/sign_in_background.jpg'),fit: BoxFit.cover,),)),new Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,crossAxisAlignment: CrossAxisAlignment.start,children: <Widget>[
],)
]));
}
}
为了实现透明背景,我们使用 class SignInState extends State<SignIn> {
@override
Widget build(BuildContext context) {
return new Scaffold(
body: new Stack(children: <Widget>[
//...
new Column(
//...
children: <Widget>[
new Center(
child: new Image.asset(
'images/talk_casually.png',width: MediaQuery.of(context).size.width * 0.4,],)
]));
}
}
第一个子控件是 class SignInState extends State<SignIn> {
final TextEditingController _usernameController = new TextEditingController();
final TextEditingController _passwordController = new TextEditingController();
@override
Widget build(BuildContext context) {
return new Scaffold(
body: new Stack(children: <Widget>[
//...
new Column(
//...
children: <Widget>[
//...
new Container(
width: MediaQuery.of(context).size.width * 0.96,child: new Column(
mainAxisAlignment: MainAxisAlignment.center,children: <Widget>[
new TextField(
controller: _usernameController,decoration: new InputDecoration(
hintText: 'Username',icon: new Icon(
Icons.account_circle,new TextField(
controller: _passwordController,obscureText: true,keyboardType: TextInputType.number,decoration: new InputDecoration(
hintText: 'Password',icon: new Icon(
Icons.lock_outline,])),)
]));
}
}
第二个控件是 上图中,输入框的输入区域一直到屏幕的边缘,反之,如果设置成96%的宽度,效果如下所示: 为了使屏幕整体效果更和谐,这里使输入框的输入区域与屏幕边缘保持一定距离。当然,具体使用怎样的效果还是看个人的审美吧! class SignInState extends State<SignIn> {
//...
@override
Widget build(BuildContext context) {
return new Scaffold(
body: new Stack(children: <Widget>[
//...
new Column(
//...
children: <Widget>[
//...
new FlatButton(
child: new Container(
decoration: new BoxDecoration(
color: Theme.of(context).accentColor,child: new Center(
child: new Text("Sign In",style: new TextStyle(
color: const Color(0xff000000),))),onPressed: () {
print('Sign In');
},)
]));
}
}
第三个子控件是 class SignInState extends State<SignIn> {
//...
@override
Widget build(BuildContext context) {
return new Scaffold(
body: new Stack(children: <Widget>[
//...
new Column(
//...
children: <Widget>[
//...
new Center(
child: new FlatButton(
child: new Text("Don't have an account ? Sign Up",style: new TextStyle(
color: const Color(0xff000000),onPressed: null,))
],)
]));
}
}
第四个子控件也是 显示登陆屏幕我们在项目的 import 'package:flutter/material.dart';
import 'sign_in.dart';
void main() {
runApp(new TalkcasuallyApp());
}
class TalkcasuallyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new MaterialApp(
title: '谈天说地',home: new SignIn()
);
}
}
重新运行应用程序,就能显示我们的登陆屏幕。 实现注册屏幕我们在项目的 import 'package:flutter/material.dart';
class SignUp extends StatefulWidget {
@override
State createState() => new SignUpState();
}
class SignUpState extends State<SignUp> {
@override
Widget build(BuildContext context) {
return new Scaffold(
body: new Stack(children: <Widget>[
new Opacity(
opacity: 0.3,child: new Container(
decoration: new BoxDecoration(
image: new DecorationImage(
image: new ExactAssetImage('images/sign_up_background.jpg'),new Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,children: <Widget>[
])
]));
}
}
上面的代码与登陆屏幕相似,所以我们直接往 class SignUpState extends State<SignUp> {
@override
Widget build(BuildContext context) {
return new Scaffold(
body: new Stack(children: <Widget>[
//...
new Column(
//...
children: <Widget>[
new BackButton(),new Text(" Sign Up",textScaleFactor: 2.0,style: new TextStyle(
color: const Color(0xff000000),])
]));
}
}
第一个子控件是 class SignUpState extends State<SignUp> {
final TextEditingController _usernameController = new TextEditingController();
final TextEditingController _passwordController = new TextEditingController();
final TextEditingController _emailController = new TextEditingController();
final TextEditingController _phoneController = new TextEditingController();
@override
Widget build(BuildContext context) {
return new Scaffold(
body: new Stack(children: <Widget>[
//...
new Column(
//...
children: <Widget>[
//...
new Container(
width: MediaQuery.of(context).size.width * 0.96,child: new Column(
mainAxisAlignment: MainAxisAlignment.center,children: <Widget>[
new TextField(
controller: _usernameController,decoration: new InputDecoration(
hintText: 'Username',icon: new Icon(
Icons.account_circle,new TextField(
controller: _passwordController,decoration: new InputDecoration(
hintText: 'Password',icon: new Icon(
Icons.lock_outline,new TextField(
controller: _emailController,decoration: new InputDecoration(
hintText: 'E-mail',icon: new Icon(
Icons.email,new TextField(
controller: _phoneController,decoration: new InputDecoration(
hintText: 'Phone',icon: new Icon(
Icons.phone,]));
}
}
第三个控件是 class SignUpState extends State<SignUp> {
//...
@override
Widget build(BuildContext context) {
return new Scaffold(
body: new Stack(children: <Widget>[
//...
new Column(
//...
children: <Widget>[
//...
new FlatButton(
child: new Container(
width: MediaQuery.of(context).size.width,decoration: new BoxDecoration(
color: Theme.of(context).accentColor,child: new Center(
child: new Text("Join",style: new TextStyle(
color: const Color(0xff000000),onPressed: () {
print('Sign In');
},])
]));
}
}
第四个子控件是 class SignUpState extends State<SignUp> {
//...
@override
Widget build(BuildContext context) {
return new Scaffold(
body: new Stack(children: <Widget>[
//...
new Column(
//...
children: <Widget>[
//...
new Center(
child: new FlatButton(
child: new Text("Already have an account ? Sign In",style: new TextStyle(
color: const Color(0xff000000),onPressed: () {
Navigator.of(context).pop();
},))
])
]));
}
}
第五个子控件是包装在 从登陆屏幕导航到注册屏幕我们在 class SignInState extends State<SignIn> {
//...
void _openSignUp() {
setState(() {
Navigator.of(context).push(new MaterialPageRoute<Null>(
builder: (BuildContext context) {
return new SignUp();
},));
});
}
//...
}
然后我们将 import 'package:flutter/material.dart';
import 'sign_up.dart';
//...
class SignInState extends State<SignIn> {
//...
@override
Widget build(BuildContext context) {
return new Scaffold(
body: new Stack(children: <Widget>[
//...
new Column(
//...
children: <Widget>[
//...
new Center(
child: new FlatButton(
child: new Text("Don't have an account ? Sign Up",onPressed: _openSignUp,)
]));
}
}
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- cocos2d-js 的 cc.callFunc 参数
- How to call an Oracle Stored Procedure that r
- c# – 如何使HttpWebRequest异步
- Quick-Cocos2d-x初学者游戏教程(二)
- ruby-on-rails – ruby?? rails – 重定向到原始
- #Flex开发# 找?不?到?所?需?A?d?o?b?e? ?F?l?a?s
- oracle没有create or replace table
- c# – 具有多个NIC的UDP多播仅在一个接口处于活动
- postgresql 序列自增 & Mybatis添加功能
- 如何一步一步用DDD设计一个电商网站(十二)——