Skip to content

Commit 29d4a5f

Browse files
committed
feat: 接入star
1 parent f75e1a3 commit 29d4a5f

File tree

7 files changed

+656
-21
lines changed

7 files changed

+656
-21
lines changed

lib/config/index.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@ import 'development.dart' as Development;
22
import 'production.dart' as Production;
33

44
const bool isPro = false;
5+
const String owner_repo = 'efoxTeam/flutter-ui';
56

67
Object env = isPro ? Production.Config() : Development.Config();

lib/controller/index.dart

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,16 @@ import 'package:efox_flutter/store/index.dart'
33

44
import 'package:efox_flutter/utils/appVersion.dart' show AppVersion;
55

6-
void initState() {
6+
void initState() async {
77
// 获取版本号
88
Store.valueNotCtx<ConfigModel>().$getAppVersion();
99
// 登录
10-
//Store.valueNotCtx<UserModel>().getLocalUserInfo();
10+
Store.valueNotCtx<UserModel>().getLocalUserInfo().then((res) {
11+
if (res) {
12+
Store.valueNotCtx<UserModel>().getUserStar();
13+
}
14+
});
15+
Store.valueNotCtx<UserModel>().getFlutterUIStar();
1116
Future.delayed(Duration(seconds: 3), () {
1217
AppVersion().check(Store.widgetCtx);
1318
});

lib/http/index.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,3 +82,8 @@ Future post({url, data = const {}, options, loading}) async {
8282
return getDio(options: options, loading: loading ?? Map())
8383
.post(url, data: data);
8484
}
85+
86+
Future put({url, data = const {}, options, loading}) async {
87+
return getDio(options: options, loading: loading ?? Map())
88+
.put(url, data: data);
89+
}

lib/page/app_login/index.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ class _IndexState extends State<Index> {
137137
'name': nameCtl.text.trim(),
138138
'pwd': pwdCtl.text.trim()
139139
});
140+
await Store.value<UserModel>(context).getUserStar();
140141
}
141142
},
142143
),

lib/page/home.dart

Lines changed: 62 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -30,20 +30,24 @@ class _IndexState extends State<Index> {
3030
}
3131

3232
Widget _bottomNavigationBar() {
33-
return BottomNavigationBar(
34-
items: <BottomNavigationBarItem>[
35-
BottomNavigationBarItem(
36-
title: Text(AppLocalizations.$t('title_component')),
37-
icon: Icon(Icons.dashboard)),
38-
BottomNavigationBarItem(
39-
title: Text(AppLocalizations.$t('title_my')),
40-
icon: Icon(Icons.person_outline)),
41-
],
42-
type: BottomNavigationBarType.fixed,
43-
currentIndex: _currentIndex,
44-
onTap: (int index) {
45-
_pageController.jumpToPage(index);
46-
},
33+
return BottomAppBar(
34+
shape: CircularNotchedRectangle(),
35+
clipBehavior: Clip.antiAlias,
36+
child: BottomNavigationBar(
37+
items: <BottomNavigationBarItem>[
38+
BottomNavigationBarItem(
39+
title: Text(AppLocalizations.$t('title_component')),
40+
icon: Icon(Icons.dashboard)),
41+
BottomNavigationBarItem(
42+
title: Text(AppLocalizations.$t('title_my')),
43+
icon: Icon(Icons.person_outline)),
44+
],
45+
// type: BottomNavigationBarType.fixed,
46+
currentIndex: _currentIndex,
47+
onTap: (int index) {
48+
_pageController.jumpToPage(index);
49+
},
50+
),
4751
);
4852
}
4953

@@ -55,6 +59,7 @@ class _IndexState extends State<Index> {
5559
title: Text(AppLocalizations.$t('common.logout')),
5660
onTap: () {
5761
Store.value<UserModel>(context).clearUserInfo();
62+
Store.value<UserModel>(context).changeIsStar(false);
5863
},
5964
),
6065
];
@@ -122,12 +127,55 @@ class _IndexState extends State<Index> {
122127
);
123128
}
124129

130+
Widget _floatingActionButton() {
131+
return Store.connect<UserModel>(
132+
builder: (context, child, model) {
133+
return FloatingActionButton(
134+
backgroundColor: Theme.of(context).primaryColor,
135+
onPressed: () {
136+
if(!model.isStar&&model.user.id != null) {
137+
print('进行star');
138+
model.setStarFlutterUI();
139+
} else {
140+
print('不满足进行star条件');
141+
if(model.user.id == null) {
142+
Navigator.of(context).push(
143+
MaterialPageRoute(
144+
builder: (BuildContext context) {
145+
return LoginIndex.Index();
146+
}
147+
)
148+
);
149+
}
150+
}
151+
},
152+
child: Container(
153+
child: Column(
154+
mainAxisAlignment: MainAxisAlignment.center,
155+
children: <Widget>[
156+
model.isStar
157+
?Icon(Icons.star,size: 20, color: Colors.white)
158+
:Icon(Icons.star_border, size: 20, color: Colors.white),
159+
Text(
160+
'${model.flutter_ui_info.stargazersCount.toString()}',
161+
style: TextStyle(color: Colors.white)
162+
)
163+
],
164+
),
165+
),
166+
);
167+
}
168+
);
169+
}
170+
125171
@override
126172
Widget build(BuildContext context) {
127173
Store.setWidgetCtx(context); // 初始化scaffold的上下文作为全局上下文,提供弹窗等使用。
128174
return Scaffold(
129175
drawer: renderDrawer(),
130176
bottomNavigationBar: _bottomNavigationBar(),
177+
floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
178+
floatingActionButton: _floatingActionButton(),
131179
body: PageView(
132180
controller: _pageController,
133181
physics: NeverScrollableScrollPhysics(),

lib/store/models/user_model.dart

Lines changed: 88 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,15 @@ import '../objects/user_info.dart' show UserInfo;
66
import '../objects/github_resp_info.dart' show GitHubRespInfo;
77
import 'package:efox_flutter/http/index.dart' as Http;
88
import 'package:efox_flutter/utils/localStorage.dart' show LocalStorage;
9+
import 'package:efox_flutter/config/index.dart' show owner_repo;
10+
import '../objects/flutter_ui_info.dart' show FlutterUiInfo;
911

10-
class UserModel with ChangeNotifier {
12+
class UserModelInfo {
13+
bool isStar = false; // 用户是否star了flutter ui项目
14+
FlutterUiInfo flutter_ui_info = FlutterUiInfo(); // flutter ui项目信息
15+
}
16+
17+
class UserModel extends UserModelInfo with ChangeNotifier {
1118
UserInfo user = UserInfo();
1219
Future testLogin() async {
1320
return await Http.post(url: 'http://www.baidu.com').then((res) {
@@ -91,12 +98,14 @@ class UserModel with ChangeNotifier {
9198
getLocalUserInfo() async {
9299
String data = await LocalStorage.get('githubUserInfo');
93100
print("本地数据 $data");
101+
if (data != null) {
102+
UserInfo user = UserInfo.fromJson(json.decode(data));
103+
setUserInfo(user);
104+
return true;
105+
}
94106
if (data == null) {
95-
getUserInfo();
96-
return;
107+
return await getUserInfo();
97108
}
98-
UserInfo user = UserInfo.fromJson(json.decode(data));
99-
setUserInfo(user);
100109
}
101110

102111
/**
@@ -120,4 +129,78 @@ class UserModel with ChangeNotifier {
120129
LocalStorage.remove('githubRespLoginToken');
121130
notifyListeners();
122131
}
132+
133+
/**
134+
* 修改star显示
135+
*/
136+
changeIsStar(isShow){
137+
isStar = isShow;
138+
notifyListeners();
139+
}
140+
141+
/**
142+
* 获取flutter ui star数量
143+
*/
144+
getFlutterUIStar() {
145+
var response = Http.get(
146+
url: 'https://api.github.com/repos/$owner_repo'
147+
);
148+
response.then((resp) {
149+
print('获取flutter ui信息:$resp');
150+
flutter_ui_info= FlutterUiInfo.fromJson(resp.data);
151+
notifyListeners();
152+
}).catchError((error) {
153+
print('获取flutter ui信息出错:$error');
154+
});
155+
}
156+
157+
/**
158+
* 获取用户的star flutter ui信息
159+
*/
160+
getUserStar() async {
161+
var response = Http.get(
162+
url: 'https://api.github.com/user/starred/$owner_repo'
163+
);
164+
response.then((resp) {
165+
print('用户的star信息状态码:${resp.statusCode}');
166+
if (resp.statusCode == 204) {
167+
// TODO user have focused the repository
168+
isStar = true;
169+
notifyListeners();
170+
return ;
171+
}
172+
if (resp.statusCode == 404) {
173+
// TODO user have not focused the repository
174+
isStar = false;
175+
notifyListeners();
176+
return;
177+
}
178+
}).catchError((error) {
179+
print('获取用户star信息出错$error');
180+
if (error.statusCode == 404) {
181+
// TODO user have not focused the repository
182+
isStar = false;
183+
notifyListeners();
184+
}
185+
});
186+
}
187+
188+
/**
189+
* 用户star flutter ui项目
190+
*/
191+
setStarFlutterUI() {
192+
var response = Http.put(
193+
url: 'https://api.github.com/user/starred/$owner_repo'
194+
);
195+
response.then((resp) {
196+
print('用户star flutter ui项目状态码: ${resp.statusCode}');
197+
if (resp.statusCode == 204) {
198+
// star success
199+
getFlutterUIStar();
200+
getUserStar();
201+
}
202+
}).catchError((error) {
203+
print('用户star flutter ui项目错误: $error');
204+
});
205+
}
123206
}

0 commit comments

Comments
 (0)