diff --git a/assets/images/arrow.png b/assets/images/arrow.png new file mode 100644 index 00000000..7ac21edc Binary files /dev/null and b/assets/images/arrow.png differ diff --git a/lib/api/api.dart b/lib/api/api.dart index 1effc4bd..691932fa 100644 --- a/lib/api/api.dart +++ b/lib/api/api.dart @@ -1,5 +1,5 @@ class Api{ - static const String BASE_URL = 'http://30.10.29.190:6001/'; + static const String BASE_URL = 'http://30.10.25.17:6001/'; // static const String BASE_URL = 'http://flutter-go.alibaba.net/'; static const String DO_LOGIN = BASE_URL+'doLogin';//登陆 diff --git a/lib/main.dart b/lib/main.dart index fdcee399..f877a1f3 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -11,6 +11,7 @@ import 'package:flutter_go/model/search_history.dart'; import 'package:flutter_go/utils/analytics.dart' as Analytics; import 'package:flutter_go/views/login_page/login_page.dart'; import 'package:flutter_go/utils/data_utils.dart'; +import 'package:flutter_go/model/user_info.dart'; //import 'views/welcome_page/index.dart'; @@ -34,18 +35,27 @@ class MyApp extends StatefulWidget { class _MyAppState extends State { bool _hasLogin = false; bool _isLoading = true; + UserInformation _userInfo; @override void initState() { super.initState(); DataUtils.checkLogin().then((hasLogin) { + if (hasLogin.runtimeType == UserInformation) { + setState(() { + _hasLogin = true; + _isLoading = false; + _userInfo = hasLogin; + }); + } else { + setState(() { + _hasLogin = hasLogin; + _isLoading = false; + }); + } + }).catchError((onError) { setState(() { - _hasLogin = hasLogin; - _isLoading = false; - }); - }).catchError((onError){ - setState(() { - _hasLogin = true; + _hasLogin = false; _isLoading = false; }); print('身份信息验证失败:$onError'); @@ -63,7 +73,7 @@ class _MyAppState extends State { } else { // 判断是否已经登录 if (_hasLogin) { - return AppPage(); + return AppPage(_userInfo); } else { return LoginPage(); } diff --git a/lib/model/user_info.dart b/lib/model/user_info.dart index b47b08a3..e5bc6b37 100644 --- a/lib/model/user_info.dart +++ b/lib/model/user_info.dart @@ -1,24 +1,25 @@ -class UserInfo { +class UserInformation { String username; int id; String avatarPic; String themeColor; - String urlName; - UserInfo({ + UserInformation({ this.avatarPic, this.id, this.themeColor, - this.urlName, this.username, }); - factory UserInfo.fromJson(Map json) { - return UserInfo( + factory UserInformation.fromJson(Map json) { + String name = json['name']; + if(json['name'] == null){ + name = json['url_name']; + } + return UserInformation( avatarPic: json['avatar_pic'], id: int.parse(json['id']), - username: json['name'], - themeColor: json['theme_color'], - urlName: json['url_name']); + username: name, + themeColor: json['theme_color']); } } diff --git a/lib/routers/router_handler.dart b/lib/routers/router_handler.dart index 4491ebcb..ce68b813 100644 --- a/lib/routers/router_handler.dart +++ b/lib/routers/router_handler.dart @@ -7,11 +7,12 @@ import 'package:flutter_go/components/full_screen_code_dialog.dart'; import 'package:flutter_go/views/web_page/web_view_page.dart'; import 'package:flutter_go/views/home.dart'; import 'package:flutter_go/views/login_page/login_page.dart'; +import 'package:flutter_go/model/user_info.dart'; // app的首页 var homeHandler = new Handler( handlerFunc: (BuildContext context, Map> params) { - return new AppPage(); + return new AppPage(UserInformation(id: 0)); }, ); diff --git a/lib/utils/data_utils.dart b/lib/utils/data_utils.dart index 35bc7c22..010f9252 100644 --- a/lib/utils/data_utils.dart +++ b/lib/utils/data_utils.dart @@ -8,29 +8,38 @@ class DataUtils { // 登陆获取用户信息 static Future doLogin(Map params) async { var response = await NetUtils.post(Api.DO_LOGIN, params); - print(response); try { - UserInfo userInfo = UserInfo.fromJson(response['data']); + UserInformation userInfo = UserInformation.fromJson(response['data']); return userInfo; } catch (err) { - return response['data']; + return response['message']; } } // 获取用户信息 - static Future getUserInfo(Map params) async { + static Future getUserInfo(Map params) async { var response = await NetUtils.get(Api.GET_USER_INFO, params); - print(response); - UserInfo userInfo = UserInfo.fromJson(response['data']); - return userInfo; + try { + UserInformation userInfo = UserInformation.fromJson(response['data']); + return userInfo; + } catch (err) { + return response['message']; + } } // 验证登陆 - - static Future checkLogin() async { + static Future checkLogin() async { var response = await NetUtils.get(Api.CHECK_LOGIN); - print('验证登陆:$response'); - return response['success']; + try { + if (response['success']) { + UserInformation userInfo = UserInformation.fromJson(response['data']); + return userInfo; + } else { + return response['success']; + } + } catch (err) { + return response['message']; + } } // 退出登陆 diff --git a/lib/utils/net_utils.dart b/lib/utils/net_utils.dart index 5a981138..927fa78e 100644 --- a/lib/utils/net_utils.dart +++ b/lib/utils/net_utils.dart @@ -17,12 +17,12 @@ class NetUtils { var response; // 设置代理 便于本地 charles 抓包 - (dio.httpClientAdapter as DefaultHttpClientAdapter).onHttpClientCreate = - (HttpClient client) { - client.findProxy = (uri) { - return "PROXY 30.10.29.190:8888"; - }; - }; + // (dio.httpClientAdapter as DefaultHttpClientAdapter).onHttpClientCreate = + // (HttpClient client) { + // client.findProxy = (uri) { + // return "PROXY 30.10.25.17:8888"; + // }; + // }; Directory documentsDir = await getApplicationDocumentsDirectory(); String documentsPath = documentsDir.path; diff --git a/lib/views/first_page/drawer_page.dart b/lib/views/first_page/drawer_page.dart new file mode 100644 index 00000000..dfe2332c --- /dev/null +++ b/lib/views/first_page/drawer_page.dart @@ -0,0 +1,130 @@ +import 'package:flutter/material.dart'; +import 'package:flutter_go/model/user_info.dart'; +import 'package:share/share.dart'; + +class DrawerPage extends StatefulWidget { + final UserInformation userInfo; + + DrawerPage({Key key, this.userInfo}) : super(key: key); + + @override + _DrawerPageState createState() => _DrawerPageState(); +} + +class _DrawerPageState extends State { + final TextStyle textStyle = + TextStyle(fontSize: 16, fontWeight: FontWeight.w300); + + @override + Widget build(BuildContext context) { + return ListView( + padding: EdgeInsets.zero, + children: [ + UserAccountsDrawerHeader( + accountName: Text(''), + accountEmail: Container( + padding: const EdgeInsets.only(bottom: 20.0), + child: Text( + widget.userInfo.username, + style: TextStyle(fontSize: 28), + ), + ), + decoration: BoxDecoration( + image: new DecorationImage( + fit: BoxFit.cover, + image: new NetworkImage(widget.userInfo.avatarPic), + ), + ), + ), + ListTile( + leading: Icon( + Icons.search, + size: 27.0, + color: Theme.of(context).primaryColor, + ), + title: Text( + '搜索', + style: textStyle, + ), + onTap: () {}, + ), + // new Divider(), + ListTile( + leading: Icon( + Icons.favorite, + size: 27.0, + ), + title: Text( + '我的收藏', + style: textStyle, + ), + onTap: () {}, + ), + // new Divider(), + ListTile( + leading: Icon( + Icons.settings, + size: 27.0, + ), + title: Text( + '更多设置', + style: textStyle, + ), + onTap: () {}, + ), + new Divider(), + + ListTile( + leading: Icon( + Icons.email, + size: 27.0, + ), + title: Text( + '反馈/建议', + style: textStyle, + ), + onTap: () {}, + ), + ListTile( + leading: Icon( + Icons.info, + size: 27.0, + ), + title: Text( + '关于 App', + style: textStyle, + ), + onTap: () {}, + ), + ListTile( + leading: Icon( + Icons.share, + size: 27.0, + ), + title: Text( + '分享 App', + style: textStyle, + ), + onTap: () { + Share.share('https://github.com/alibaba/flutter-go'); + }, + ), + new Divider(), + ListTile( + leading: Icon( + Icons.exit_to_app, + size: 27.0, + ), + title: Text( + '登出', + style: textStyle, + ), + onTap: () { + Share.share('check out my website https://example.com'); + }, + ), + + ], + ); + } +} diff --git a/lib/views/first_page/main_page.dart b/lib/views/first_page/main_page.dart index 0dd66de9..1822f623 100644 --- a/lib/views/first_page/main_page.dart +++ b/lib/views/first_page/main_page.dart @@ -1,9 +1,11 @@ import 'package:flutter/material.dart'; import 'package:flutter/cupertino.dart'; +import 'package:flutter_go/views/first_page/drawer_page.dart'; import './first_page.dart'; import './sub_page.dart'; import './main_app_bar.dart'; import './search_page.dart'; +import 'package:flutter_go/model/user_info.dart'; class _Page { final String labelId; @@ -19,6 +21,10 @@ final List<_Page> _allPages = <_Page>[ ]; class MainPage extends StatelessWidget { + final UserInformation userInfo; + + MainPage({Key key, this.userInfo}) : super(key: key); + @override Widget build(BuildContext context) { print("MainPagess build......"); @@ -29,7 +35,7 @@ class MainPage extends StatelessWidget { leading: Container( child: new ClipOval( child: Image.network( - 'https://hbimg.huabanimg.com/9bfa0fad3b1284d652d370fa0a8155e1222c62c0bf9d-YjG0Vt_fw658', + userInfo.avatarPic, scale: 15.0, ), )), @@ -44,17 +50,8 @@ class MainPage extends StatelessWidget { ], ), drawer: Drawer( - child: new ListView( - children: [ - new ListTile( - title: new Text("欢迎"), - ), - new Divider(), - new ListTile( - title: new Text("设置"), - trailing: new Icon(Icons.settings), - onTap: () {}), - ], + child: DrawerPage( + userInfo: userInfo, ), ), body: TabBarViewLayout(), diff --git a/lib/views/home.dart b/lib/views/home.dart index c0f63681..58aca74a 100644 --- a/lib/views/home.dart +++ b/lib/views/home.dart @@ -20,10 +20,16 @@ import 'package:flutter_go/widgets/index.dart'; import 'package:flutter_go/components/search_input.dart'; import 'package:flutter_go/model/search_history.dart'; import 'package:flutter_go/resources/widget_name_to_icon.dart'; +import 'package:flutter_go/model/user_info.dart'; const int ThemeColor = 0xFFC91B3A; class AppPage extends StatefulWidget { + final UserInformation userInfo; + + AppPage(this.userInfo); + + @override State createState() { return _MyHomePageState(); @@ -32,6 +38,8 @@ class AppPage extends StatefulWidget { class _MyHomePageState extends State with SingleTickerProviderStateMixin { + + SpUtil sp; WidgetControlModel widgetControl = new WidgetControlModel(); SearchHistoryList searchHistoryList; @@ -62,7 +70,7 @@ class _MyHomePageState extends State } list // ..add(FirstPage()) - ..add(MainPage()) + ..add(MainPage(userInfo:widget.userInfo)) ..add(WidgetPage(Provider.db)) ..add(CollectionPage()) ..add(FourthPage()); @@ -98,7 +106,7 @@ class _MyHomePageState extends State } Widget buildSearchInput(BuildContext context) { - return new SearchInput((value) async { + return new SearchInput((value) async { if (value != '') { List list = await widgetControl.search(value); return list @@ -107,7 +115,7 @@ class _MyHomePageState extends State icon: WidgetName2Icon.icons[item.name] ?? null, text: 'widget', onTap: () { - onWidgetTap(item, context); + onWidgetTap(item, context); }, )) .toList(); @@ -117,9 +125,9 @@ class _MyHomePageState extends State }, (value) {}, () {}); } - renderAppBar(BuildContext context,Widget widget,int index) { + renderAppBar(BuildContext context, Widget widget, int index) { print('renderAppBar=====>>>>>>${index}'); - if(index == 0) { + if (index == 0) { return null; } return AppBar(title: buildSearchInput(context)); @@ -128,7 +136,7 @@ class _MyHomePageState extends State @override Widget build(BuildContext context) { return new Scaffold( - appBar: renderAppBar(context,widget,_currentIndex), + appBar: renderAppBar(context, widget, _currentIndex), body: list[_currentIndex], bottomNavigationBar: BottomNavigationBar( items: myTabs, diff --git a/lib/views/login_page/login_page.dart b/lib/views/login_page/login_page.dart index 52febd19..61232bc0 100644 --- a/lib/views/login_page/login_page.dart +++ b/lib/views/login_page/login_page.dart @@ -11,6 +11,7 @@ import 'package:flutter_go/event/event_model.dart'; import 'package:flutter_go/model/user_info_cache.dart'; import 'package:flutter_go/routers/application.dart'; import 'package:flutter_go/routers/routers.dart'; +import 'package:flutter_go/model/user_info.dart'; class LoginPage extends StatefulWidget { @override @@ -47,7 +48,6 @@ class _LoginPageState extends State { _userInfoControlModel.getAllInfo().then((list) { if (list.length > 0) { UserInfo _userInfo = list[0]; - print('获取的数据:${_userInfo.username} ${_userInfo.password}'); setState(() { _userNameEditingController.text = _userInfo.username; _passwordEditingController.text = _userInfo.password; @@ -62,7 +62,6 @@ class _LoginPageState extends State { ApplicationEvent.event.on().listen((event) { if (event.isSuccess == true) { - print('请求接口'); // oAuth 认证成功 setState(() { isLoading = true; @@ -73,7 +72,7 @@ class _LoginPageState extends State { isLoading = false; }); Navigator.of(context).pushAndRemoveUntil( - MaterialPageRoute(builder: (context) => AppPage()), + MaterialPageRoute(builder: (context) => AppPage(result)), (route) => route == null); }).catchError((onError) { print('获取身份信息 error:::$onError'); @@ -81,9 +80,16 @@ class _LoginPageState extends State { isLoading = false; }); }); + } else { + Fluttertoast.showToast( + msg: '验证失败', + toastLength: Toast.LENGTH_SHORT, + gravity: ToastGravity.CENTER, + timeInSecForIos: 1, + backgroundColor: Theme.of(context).primaryColor, + textColor: Colors.white, + fontSize: 16.0); } - print('这是接受到的 event ${event.isSuccess} ${event.loginName}'); - // _getList(); }); } @@ -223,10 +229,10 @@ class _LoginPageState extends State { isLoading = true; }); DataUtils.doLogin({'username': username, 'password': password}) - .then((result) { - if(result.runtimeType == String){ - throw result; - } + .then((userResult) { + if (userResult.runtimeType == String) { + throw userResult; + } setState(() { isLoading = false; }); @@ -236,15 +242,15 @@ class _LoginPageState extends State { _userInfoControlModel .insert(UserInfo(password: password, username: username)) .then((value) { - // print('存储成功:$value'); + print('存储成功:$value'); Navigator.of(context).pushAndRemoveUntil( - MaterialPageRoute(builder: (context) => AppPage()), + MaterialPageRoute(builder: (context) => AppPage(userResult)), (route) => route == null); }); }); } catch (err) { Navigator.of(context).pushAndRemoveUntil( - MaterialPageRoute(builder: (context) => AppPage()), + MaterialPageRoute(builder: (context) => AppPage(userResult)), (route) => route == null); } }).catchError((errorMsg) { @@ -252,14 +258,13 @@ class _LoginPageState extends State { isLoading = false; }); Fluttertoast.showToast( - msg: errorMsg.toString(), - toastLength: Toast.LENGTH_SHORT, - gravity: ToastGravity.CENTER, - timeInSecForIos: 1, - backgroundColor: Theme.of(context).primaryColor, - textColor: Colors.white, - fontSize: 16.0 - ); + msg: errorMsg.toString(), + toastLength: Toast.LENGTH_SHORT, + gravity: ToastGravity.CENTER, + timeInSecForIos: 1, + backgroundColor: Theme.of(context).primaryColor, + textColor: Colors.white, + fontSize: 16.0); }); } @@ -315,11 +320,28 @@ class _LoginPageState extends State { mainAxisSize: MainAxisSize.min, children: [ SizedBox(height: 35.0), - Image.asset( - 'assets/images/FlutterGo.png', - fit: BoxFit.contain, - width: 100.0, - height: 100.0, + Row( + mainAxisAlignment: MainAxisAlignment.center, + children: [ + Image.asset( + 'assets/images/gitHub.png', + fit: BoxFit.contain, + width: 60.0, + height: 60.0, + ), + Image.asset( + 'assets/images/arrow.png', + fit: BoxFit.contain, + width: 40.0, + height: 30.0, + ), + Image.asset( + 'assets/images/FlutterGo.png', + fit: BoxFit.contain, + width: 60.0, + height: 60.0, + ), + ], ), buildSignInTextForm(), buildSignInButton(), @@ -330,19 +352,36 @@ class _LoginPageState extends State { color: Colors.grey[400], margin: const EdgeInsets.only(bottom: 10.0), ), - FlatButton( - child: Text( - 'Github OAuth 认证', - style: TextStyle( - color: Theme.of(context).primaryColor, - decoration: TextDecoration.underline), - ), - onPressed: () { - // _launchURL('https://github.com/login/oauth/authorize?client_id=cfe4795e76382ae8a5bd&scope=user,public_repo'); - - Application.router.navigateTo(context, - '${Routes.webViewPage}?title=Github&url=${Uri.encodeComponent("https://github.com/login/oauth/authorize?client_id=cfe4795e76382ae8a5bd&scope=user,public_repo")}'); - }, + Row( + mainAxisAlignment: MainAxisAlignment.center, + children: [ + FlatButton( + child: Text( + 'Github OAuth 认证', + style: TextStyle( + color: Theme.of(context).primaryColor, + decoration: TextDecoration.underline), + ), + onPressed: () { + Application.router.navigateTo(context, + '${Routes.webViewPage}?title=Github&url=${Uri.encodeComponent("https://github.com/login/oauth/authorize?client_id=cfe4795e76382ae8a5bd&scope=user,public_repo")}'); + }, + ), + FlatButton( + child: Text( + '游客登录', + style: TextStyle( + color: Theme.of(context).primaryColor, + decoration: TextDecoration.underline), + ), + onPressed: () { + Navigator.of(context).pushAndRemoveUntil( + MaterialPageRoute( + builder: (context) => AppPage(UserInformation(id: 0,avatarPic: 'https://hbimg.huabanimg.com/9bfa0fad3b1284d652d370fa0a8155e1222c62c0bf9d-YjG0Vt_fw658'))), + (route) => route == null); + }, + ) + ], ) ], ), diff --git a/pubspec.lock b/pubspec.lock index 02c0f882..2dc6c452 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -235,6 +235,13 @@ packages: url: "https://pub.flutter-io.cn" source: hosted version: "0.21.0" + share: + dependency: "direct main" + description: + name: share + url: "https://pub.flutter-io.cn" + source: hosted + version: "0.6.1+1" shared_preferences: dependency: "direct main" description: diff --git a/pubspec.yaml b/pubspec.yaml index 7a8c87ea..300cd3b2 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -27,6 +27,7 @@ dependencies: url_launcher: ^5.0.2 # 本地存储、收藏功能 shared_preferences: ^0.4.3 + share: ^0.6.1+1 flutter_spinkit: "^3.1.0" path_provider: ^1.0.0 fluttertoast: ^3.1.0