Skip to content

Commit 22de05f

Browse files
committed
sth
1 parent 7a2731a commit 22de05f

16 files changed

+530
-1
lines changed

lib/api/Api.dart

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class Api {
2+
static final String HOME_BANNER = "http://www.";
3+
static final String HOME_LIST = "http://www.";
4+
static final String HOME_LOGIN = "http://www.";
5+
static final String REGISTER = "http://www.";
6+
7+
8+
}

lib/api/CommonService.dart

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import 'dart:async';
2+
3+
import 'package:coderiver/common/User.dart';
4+
import 'package:dio/dio.dart';
5+
import 'Api.dart';
6+
7+
class CommonService {
8+
9+
Future<Response> getWeChatListData(String url) async {
10+
return await Dio().get(url, options: _getOptions());
11+
}
12+
13+
Future<Response> getTreeItemList(String url) async {
14+
return await Dio().get(url, options: _getOptions());
15+
}
16+
17+
Future<Response> getArticleListData(int page) async {
18+
return await Dio()
19+
.get("${Api.HOME_LIST}$page/json", options: _getOptions());
20+
}
21+
22+
23+
Future<Response> getProjectListData(String url) async {
24+
return await Dio().get(url, options: _getOptions());
25+
}
26+
27+
28+
Future<Response> login(String username, String password) async {
29+
FormData formData = new FormData.from({
30+
"username": "$username",
31+
"password": "$password",
32+
});
33+
return await Dio().post(Api.HOME_LOGIN, data: formData);
34+
}
35+
36+
Future<Response> register(String username, String password) async {
37+
FormData formData = new FormData.from({
38+
"username": "$username",
39+
"password": "$password",
40+
});
41+
return await Dio().post(Api.REGISTER, data: formData);
42+
}
43+
44+
Options _getOptions() {
45+
return Options(headers: User().getHeader());
46+
}
47+
}

lib/common/GlobalConfig.dart

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import 'package:flutter/material.dart';
2+
3+
class GlobalConfig {
4+
///颜色
5+
static Color colorPrimary = Colors.blue;
6+
static Color color_tags = Color(0xFF009a61);
7+
static Color color_black = Color(0xFF000000);
8+
static Color color_dark_gray = Color(0xFF6b6b6b);
9+
static Color color_white_a80 = Color(0xccffffff);
10+
static Color color_blue = Color(0xcc0000ff);
11+
12+
///导航
13+
static String homeTab = "首页";
14+
static String secondTab = "二页";
15+
static String thiredTab = "三页";
16+
static String fourthTab = "四页";
17+
static String fifthTab = "五页";
18+
}

lib/common/Router.dart

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import 'package:flutter/cupertino.dart';
2+
import 'package:flutter/material.dart';
3+
4+
class Router {
5+
/*openWeb(BuildContext context, String url, String title) {
6+
Navigator.of(context)
7+
.push(MaterialPageRoute(builder: (BuildContext context) {
8+
return WebViewPage(url: url, title: title);
9+
}));
10+
}*/
11+
12+
back(BuildContext context) {
13+
Navigator.of(context).pop();
14+
}
15+
16+
Widget _transitionsBuilder(BuildContext context, Animation<double> animation,
17+
Animation<double> secondaryAnimation, Widget child) {
18+
return FadeTransition(
19+
opacity: animation,
20+
child: FadeTransition(
21+
opacity: Tween<double>(begin: 0.5, end: 1.0).animate(animation),
22+
child: child,
23+
),
24+
);
25+
}
26+
}

lib/common/Snack.dart

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import 'package:flutter/material.dart';
2+
3+
class Snack {
4+
static void show(BuildContext context, String str) {
5+
Scaffold.of(context).showSnackBar(SnackBar(
6+
content: Text(null == str ? "" : str),
7+
));
8+
}
9+
}

lib/common/Sp.dart

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import 'package:shared_preferences/shared_preferences.dart';
2+
3+
class Sp {
4+
static put(String key, String value) async {
5+
var prefs = await SharedPreferences.getInstance();
6+
await prefs.setString(key, value);
7+
}
8+
9+
static getS(String key, Function callback) async {
10+
SharedPreferences.getInstance().then((prefs) {
11+
callback(prefs.getString(key));
12+
});
13+
}
14+
15+
static putUserName(String value) {
16+
put("username", value);
17+
}
18+
19+
static putPassword(String value) {
20+
put("password", value);
21+
}
22+
23+
static putCookie(String value) {
24+
put("cookie", value);
25+
}
26+
27+
static putCookieExpires(String value) {
28+
put("expires", value);
29+
}
30+
31+
static getUserName(Function callback) {
32+
getS("username", callback);
33+
}
34+
35+
static getPassword(Function callback) {
36+
getS("password", callback);
37+
}
38+
39+
static getCookie(Function callback) {
40+
getS("cookie", callback);
41+
}
42+
43+
static getCookieExpires(Function callback) {
44+
getS("expires", callback);
45+
}
46+
}

lib/common/User.dart

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
import 'dart:async';
2+
import 'dart:convert';
3+
4+
import 'package:coderiver/api/CommonService.dart';
5+
import 'package:coderiver/utils/DateUtil.dart';
6+
import 'package:dio/dio.dart';
7+
import 'package:coderiver/common/Sp.dart';
8+
9+
class User {
10+
String userName;
11+
String password;
12+
String cookie;
13+
DateTime cookieExpiresTime;
14+
Map<String, String> _headerMap;
15+
16+
static final User _singleton = User._internal();
17+
18+
factory User() {
19+
return _singleton;
20+
}
21+
22+
User._internal();
23+
24+
bool isLogin() {
25+
return null != userName &&
26+
userName.length >= 6 &&
27+
null != password &&
28+
password.length >= 6;
29+
}
30+
31+
void logout() {
32+
Sp.putUserName(null);
33+
Sp.putPassword(null);
34+
userName = null;
35+
password = null;
36+
_headerMap = null;
37+
}
38+
39+
void refreshUserData({Function callback}) {
40+
Sp.getPassword((pw) {
41+
this.password = pw;
42+
});
43+
Sp.getUserName((str) {
44+
this.userName = str;
45+
if (null != callback) {
46+
callback();
47+
}
48+
});
49+
Sp.getCookie((str) {
50+
this.cookie = str;
51+
_headerMap = null;
52+
});
53+
Sp.getCookieExpires((str) {
54+
if (null != str && str.length > 0) {
55+
this.cookieExpiresTime = DateTime.parse(str);
56+
//提前3天请求新的cookie
57+
if (cookieExpiresTime.isAfter(DateUtil.getDaysAgo(3))) {
58+
Timer(Duration(milliseconds: 100), () {
59+
autoLogin();
60+
});
61+
}
62+
}
63+
});
64+
}
65+
66+
void login({Function callback}) {
67+
_saveUserInfo(CommonService().login(userName, password), userName, password,
68+
callback: callback);
69+
}
70+
71+
void register({Function callback}) {
72+
_saveUserInfo(
73+
CommonService().register(userName, password), userName, password,
74+
callback: callback);
75+
}
76+
77+
void _saveUserInfo(
78+
Future<Response> responseF, String userName, String password,
79+
{Function callback}) {
80+
responseF.then((response) {
81+
//实体处理
82+
/*var userModel = UserModel.fromJson(response.data);
83+
if (userModel.errorCode == 0) {
84+
Sp.putUserName(userName);
85+
Sp.putPassword(password);
86+
String cookie = "";
87+
DateTime expires;
88+
response.headers.forEach((String name, List<String> values) {
89+
if (name == "set-cookie") {
90+
cookie = json
91+
.encode(values)
92+
.replaceAll("\[\"", "")
93+
.replaceAll("\"\]", "")
94+
.replaceAll("\",\"", "; ");
95+
try {
96+
expires = DateUtil.formatExpiresTime(cookie);
97+
} catch (e) {
98+
expires = DateTime.now();
99+
}
100+
}
101+
});
102+
Sp.putCookie(cookie);
103+
Sp.putCookieExpires(expires.toIso8601String());
104+
if (null != callback) callback(true, null);
105+
} else {
106+
if (null != callback) callback(false, userModel.errorMsg);
107+
}*/
108+
});
109+
}
110+
111+
void autoLogin() {
112+
if (isLogin()) {
113+
login();
114+
}
115+
}
116+
117+
Map<String, String> getHeader() {
118+
if (null == _headerMap) {
119+
_headerMap = Map();
120+
_headerMap["Cookie"] = cookie;
121+
}
122+
return _headerMap;
123+
}
124+
}

lib/main.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import 'package:coderiver/ui/Application.dart';
12
import 'package:flutter/material.dart';
23

34
void main() => runApp(MyApp());
@@ -16,7 +17,7 @@ class MyApp extends StatelessWidget {
1617
appBar: AppBar(
1718
title: Text("CodeRiver"),
1819
),
19-
body: Text("Hello World"),
20+
body: ApplicationPage(),
2021
)
2122
);
2223
}

lib/ui/Application.dart

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
import 'package:coderiver/common/GlobalConfig.dart';
2+
import 'package:coderiver/common/User.dart';
3+
import 'package:coderiver/ui/home/HomePage.dart';
4+
import 'package:coderiver/ui/home/MinePage.dart';
5+
import 'package:coderiver/ui/home/SeconedPage.dart';
6+
import 'package:flutter/material.dart';
7+
8+
class ApplicationPage extends StatefulWidget {
9+
@override
10+
State<StatefulWidget> createState() {
11+
return _ApplicationPageState();
12+
}
13+
}
14+
15+
class _ApplicationPageState extends State<ApplicationPage>
16+
with SingleTickerProviderStateMixin {
17+
int _page = 0;
18+
PageController _pageController;
19+
20+
final List<BottomNavigationBarItem> _bottomTabs = <BottomNavigationBarItem>[
21+
BottomNavigationBarItem(
22+
icon: new Icon(Icons.assignment),
23+
title: Text(GlobalConfig.homeTab),
24+
backgroundColor: GlobalConfig.colorPrimary),
25+
BottomNavigationBarItem(
26+
icon: new Icon(Icons.assignment),
27+
title: Text(GlobalConfig.secondTab),
28+
backgroundColor: GlobalConfig.colorPrimary),
29+
BottomNavigationBarItem(
30+
icon: new Icon(Icons.assignment),
31+
title: Text(GlobalConfig.thiredTab),
32+
backgroundColor: GlobalConfig.colorPrimary),
33+
];
34+
35+
@override
36+
void initState() {
37+
super.initState();
38+
_pageController = PageController(initialPage: this._page);
39+
}
40+
41+
@override
42+
void dispose() {
43+
_pageController.dispose();
44+
super.dispose();
45+
}
46+
47+
@override
48+
Widget build(BuildContext context) {
49+
User().refreshUserData();
50+
return Scaffold(
51+
body: PageView(
52+
physics: NeverScrollableScrollPhysics(),
53+
children: <Widget>[HomePage(), SeconedPage(), MinePage()],
54+
onPageChanged: onPageChanged,
55+
controller: _pageController,
56+
),
57+
bottomNavigationBar: BottomNavigationBar(
58+
items: _bottomTabs,
59+
currentIndex: _page,
60+
fixedColor: GlobalConfig.colorPrimary,
61+
type: BottomNavigationBarType.fixed,
62+
onTap: onTap,
63+
),
64+
);
65+
}
66+
67+
void onTap(int index) {
68+
_pageController.animateToPage(index,
69+
duration: const Duration(milliseconds: 300), curve: Curves.ease);
70+
}
71+
72+
void onPageChanged(int page) {
73+
setState(() {
74+
this._page = page;
75+
});
76+
}
77+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy