diff --git a/lib/api/api.dart b/lib/api/api.dart new file mode 100644 index 00000000..62f4fb6c --- /dev/null +++ b/lib/api/api.dart @@ -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'; +} \ No newline at end of file diff --git a/lib/model/user_info.dart b/lib/model/user_info.dart new file mode 100644 index 00000000..b96485a0 --- /dev/null +++ b/lib/model/user_info.dart @@ -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 json) { + return UserInfo( + avatarPic: json['avatar_pic'], + id: json['id'], + username: json['username'], + themeColor: json['theme_color'], + urlName: json['url_name']); + } +} diff --git a/lib/utils/data_utils.dart b/lib/utils/data_utils.dart new file mode 100644 index 00000000..533848b1 --- /dev/null +++ b/lib/utils/data_utils.dart @@ -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 doLogin(Map 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; + } +} \ No newline at end of file diff --git a/lib/views/login_page/login_page.dart b/lib/views/login_page/login_page.dart index 2b24b476..b8933087 100644 --- a/lib/views/login_page/login_page.dart +++ b/lib/views/login_page/login_page.dart @@ -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 { bool isShowPassWord = false; String username = ''; String password = ''; + bool isLoading = false; // 创建登录界面的TextForm Widget buildSignInTextForm() { @@ -136,15 +138,34 @@ class _LoginPageState extends State { // 利用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 { }); } + 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 { 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(), ) ], ),