mirror of
https://github.com/alibaba/flutter-go.git
synced 2025-05-17 21:05:56 +08:00
Login 登陆界面
This commit is contained in:
5
lib/api/api.dart
Normal file
5
lib/api/api.dart
Normal file
@ -0,0 +1,5 @@
|
||||
class Api{
|
||||
static const String BASE_URL = 'http://127.0.0.1:6001/';
|
||||
|
||||
static const String DO_LOGIN = BASE_URL+'doLogin';
|
||||
}
|
24
lib/model/user_info.dart
Normal file
24
lib/model/user_info.dart
Normal file
@ -0,0 +1,24 @@
|
||||
class UserInfo {
|
||||
String username;
|
||||
int id;
|
||||
String avatarPic;
|
||||
String themeColor;
|
||||
String urlName;
|
||||
|
||||
UserInfo({
|
||||
this.avatarPic,
|
||||
this.id,
|
||||
this.themeColor,
|
||||
this.urlName,
|
||||
this.username,
|
||||
});
|
||||
|
||||
factory UserInfo.fromJson(Map<String, dynamic> json) {
|
||||
return UserInfo(
|
||||
avatarPic: json['avatar_pic'],
|
||||
id: json['id'],
|
||||
username: json['username'],
|
||||
themeColor: json['theme_color'],
|
||||
urlName: json['url_name']);
|
||||
}
|
||||
}
|
16
lib/utils/data_utils.dart
Normal file
16
lib/utils/data_utils.dart
Normal file
@ -0,0 +1,16 @@
|
||||
import 'dart:async' show Future;
|
||||
|
||||
import './net_utils.dart';
|
||||
import '../model/user_info.dart';
|
||||
import 'package:flutter_go/api/api.dart';
|
||||
|
||||
|
||||
class DataUtils{
|
||||
// 登陆获取用户信息
|
||||
static Future<UserInfo> doLogin(Map<String,String> params) async{
|
||||
var response = await NetUtils.post(Api.DO_LOGIN, params);
|
||||
print('url:${Api.DO_LOGIN} $response');
|
||||
UserInfo userInfo = UserInfo.fromJson(response['data']);
|
||||
return userInfo;
|
||||
}
|
||||
}
|
@ -1,5 +1,6 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_spinkit/flutter_spinkit.dart';
|
||||
import 'package:flutter_go/utils/data_utils.dart';
|
||||
|
||||
class LoginPage extends StatefulWidget {
|
||||
@override
|
||||
@ -17,6 +18,7 @@ class _LoginPageState extends State<LoginPage> {
|
||||
bool isShowPassWord = false;
|
||||
String username = '';
|
||||
String password = '';
|
||||
bool isLoading = false;
|
||||
|
||||
// 创建登录界面的TextForm
|
||||
Widget buildSignInTextForm() {
|
||||
@ -136,15 +138,34 @@ class _LoginPageState extends State<LoginPage> {
|
||||
// 利用key来获取widget的状态FormState,可以用过FormState对Form的子孙FromField进行统一的操作
|
||||
if (_signInFormKey.currentState.validate()) {
|
||||
// 如果输入都检验通过,则进行登录操作
|
||||
Scaffold.of(context)
|
||||
.showSnackBar(new SnackBar(content: new Text("执行登录操作")));
|
||||
// Scaffold.of(context)
|
||||
// .showSnackBar(new SnackBar(content: new Text("执行登录操作")));
|
||||
//调用所有自孩子的save回调,保存表单内容
|
||||
_signInFormKey.currentState.save();
|
||||
doLogin();
|
||||
}
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
// 登陆操作
|
||||
doLogin() {
|
||||
_signInFormKey.currentState.save();
|
||||
setState(() {
|
||||
isLoading = true;
|
||||
});
|
||||
DataUtils.doLogin({'username':username,'password':password}).then((result){
|
||||
print(result.username);
|
||||
setState(() {
|
||||
isLoading = false;
|
||||
});
|
||||
}).catchError((onError){
|
||||
print(onError);
|
||||
setState(() {
|
||||
isLoading = false;
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
// 点击控制密码是否显示
|
||||
void showPassWord() {
|
||||
setState(() {
|
||||
@ -152,6 +173,23 @@ class _LoginPageState extends State<LoginPage> {
|
||||
});
|
||||
}
|
||||
|
||||
Widget buildLoading() {
|
||||
if (isLoading) {
|
||||
return Opacity(
|
||||
opacity: .5,
|
||||
child: Container(
|
||||
width: MediaQuery.of(context).size.width * 0.85,
|
||||
decoration: BoxDecoration(
|
||||
borderRadius: BorderRadius.all(Radius.circular(8.0)),
|
||||
color: Colors.black,
|
||||
),
|
||||
child: SpinKitPouringHourglass(color: Colors.white),
|
||||
),
|
||||
);
|
||||
}
|
||||
return Container();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
@ -195,17 +233,7 @@ class _LoginPageState extends State<LoginPage> {
|
||||
top: 0,
|
||||
left: 0,
|
||||
bottom: 0,
|
||||
child: Opacity(
|
||||
opacity: .5,
|
||||
child: Container(
|
||||
width: MediaQuery.of(context).size.width * 0.85,
|
||||
decoration: BoxDecoration(
|
||||
borderRadius: BorderRadius.all(Radius.circular(8.0)),
|
||||
color: Colors.black,
|
||||
),
|
||||
child: SpinKitPouringHourglass(color: Colors.white),
|
||||
),
|
||||
),
|
||||
child: buildLoading(),
|
||||
)
|
||||
],
|
||||
),
|
||||
|
Reference in New Issue
Block a user