refactor: extract link tap screen builder

This commit is contained in:
Rongjian Zhang 2019-02-07 19:17:29 +08:00
parent 374a39c420
commit 74aa4a7ea5
11 changed files with 92 additions and 100 deletions

View File

@ -61,48 +61,51 @@ class NotificationScreenState extends State<NotificationScreen> {
_groupMap[repo].items.add(item); _groupMap[repo].items.add(item);
}); });
var schema = '{'; if (_groupMap.isNotEmpty) {
_groupMap.forEach((repo, group) { // query state of issues and pull requests
var repoKey = getRepoKey(group); var schema = '{';
schema += _groupMap.forEach((repo, group) {
'$repoKey: repository(owner: "${group.owner}", name: "${group.name}") {'; var repoKey = getRepoKey(group);
schema +=
'$repoKey: repository(owner: "${group.owner}", name: "${group.name}") {';
group.items.forEach((item) { group.items.forEach((item) {
var key = getItemKey(item); var key = getItemKey(item);
switch (item.type) { switch (item.type) {
case 'Issue': case 'Issue':
schema += ''' schema += '''
$key: issue(number: ${item.number}) { $key: issue(number: ${item.number}) {
state state
} }
'''; ''';
break; break;
case 'PullRequest': case 'PullRequest':
schema += ''' schema += '''
$key: pullRequest(number: ${item.number}) { $key: pullRequest(number: ${item.number}) {
state state
} }
'''; ''';
break; break;
} }
}); });
schema += '}';
});
schema += '}'; schema += '}';
});
schema += '}';
// print(schema); // print(schema);
var data = await SettingsProvider.of(context).query(schema); var data = await SettingsProvider.of(context).query(schema);
_groupMap.forEach((repo, group) { _groupMap.forEach((repo, group) {
group.items.forEach((item) { group.items.forEach((item) {
var itemData = data[getRepoKey(group)][getItemKey(item)]; var itemData = data[getRepoKey(group)][getItemKey(item)];
if (itemData != null) { if (itemData != null) {
item.state = itemData['state']; item.state = itemData['state'];
} }
});
}); });
}); // print(data);
// print(data); }
return _groupMap; return _groupMap;
} }
@ -120,10 +123,10 @@ $key: pullRequest(number: ${item.number}) {
style: TextStyle(color: Colors.black, fontSize: 15), style: TextStyle(color: Colors.black, fontSize: 15),
), ),
Link( Link(
onTap: () async { beforeRedirect: () async {
// await SettingsProvider.of(context) await SettingsProvider.of(context)
// .putWithCredentials('/repos/$repo/notifications'); .putWithCredentials('/repos/$repo/notifications');
// await _refresh(); await _refresh();
}, },
child: Icon( child: Icon(
Octicons.check, Octicons.check,

View File

@ -113,16 +113,12 @@ class _RepoScreenState extends State<RepoScreen> {
EntryItem( EntryItem(
count: payload['issues']['totalCount'], count: payload['issues']['totalCount'],
text: 'Issues', text: 'Issues',
route: CupertinoPageRoute( screenBuilder: (context) => IssuesScreen(),
builder: (context) => IssuesScreen(),
),
), ),
EntryItem( EntryItem(
count: payload['pullRequests']['totalCount'], count: payload['pullRequests']['totalCount'],
text: 'Pull Requests', text: 'Pull Requests',
route: CupertinoPageRoute( screenBuilder: (context) => PullRequestsScreen(),
builder: (context) => PullRequestsScreen(),
),
), ),
], ],
), ),

View File

@ -175,26 +175,22 @@ class _UserScreenState extends State<UserScreen> {
EntryItem( EntryItem(
count: payload['repositories']['totalCount'], count: payload['repositories']['totalCount'],
text: 'Repositories', text: 'Repositories',
route: screenBuilder: (context) => ReposScreen(),
CupertinoPageRoute(builder: (context) => ReposScreen()),
), ),
EntryItem( EntryItem(
count: payload['starredRepositories']['totalCount'], count: payload['starredRepositories']['totalCount'],
text: 'Stars', text: 'Stars',
route: screenBuilder: (context) => ReposScreen(),
CupertinoPageRoute(builder: (context) => ReposScreen()),
), ),
EntryItem( EntryItem(
count: payload['followers']['totalCount'], count: payload['followers']['totalCount'],
text: 'Followers', text: 'Followers',
route: screenBuilder: (context) => UsersScreen(),
CupertinoPageRoute(builder: (context) => UsersScreen()),
), ),
EntryItem( EntryItem(
count: payload['following']['totalCount'], count: payload['following']['totalCount'],
text: 'Following', text: 'Following',
route: screenBuilder: (context) => UsersScreen(),
CupertinoPageRoute(builder: (context) => UsersScreen()),
), ),
], ],
), ),

View File

@ -17,10 +17,7 @@ class Avatar extends StatelessWidget {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return Link( return Link(
onTap: () { screenBuilder: (_) => UserScreen(login),
Navigator.of(context)
.push(CupertinoPageRoute(builder: (_) => UserScreen(login)));
},
child: CircleAvatar( child: CircleAvatar(
backgroundColor: Colors.transparent, backgroundColor: Colors.transparent,
backgroundImage: NetworkImage(url), backgroundImage: NetworkImage(url),

View File

@ -4,9 +4,9 @@ import 'link.dart';
class EntryItem extends StatelessWidget { class EntryItem extends StatelessWidget {
final int count; final int count;
final String text; final String text;
final CupertinoPageRoute route; final WidgetBuilder screenBuilder;
EntryItem({this.count, this.text, this.route}); EntryItem({this.count, this.text, this.screenBuilder});
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
@ -17,14 +17,12 @@ class EntryItem extends StatelessWidget {
padding: EdgeInsets.symmetric(vertical: 10), padding: EdgeInsets.symmetric(vertical: 10),
child: Column( child: Column(
children: <Widget>[ children: <Widget>[
Text(count.toString()), Text(count.toString(), style: TextStyle(fontSize: 18)),
Text(text, style: TextStyle(fontSize: 13)) Text(text, style: TextStyle(fontSize: 13))
], ],
), ),
), ),
onTap: () { screenBuilder: screenBuilder,
Navigator.of(context).push(route);
},
), ),
); );
} }

View File

@ -3,8 +3,9 @@ import 'package:flutter/cupertino.dart';
import '../screens/issue.dart'; import '../screens/issue.dart';
import '../screens/pull_request.dart'; import '../screens/pull_request.dart';
import '../screens/user.dart'; import '../screens/user.dart';
import '../utils/utils.dart'; import 'link.dart';
import 'avatar.dart'; import 'avatar.dart';
import '../utils/utils.dart';
class EventItem extends StatelessWidget { class EventItem extends StatelessWidget {
final Event event; final Event event;
@ -39,10 +40,7 @@ class EventItem extends StatelessWidget {
}) { }) {
var _spans = [ var _spans = [
createLinkSpan( createLinkSpan(
context, context, event.actor.login, () => UserScreen(event.actor.login))
event.actor.login,
() => UserScreen(event.actor.login),
)
]; ];
_spans.addAll(spans); _spans.addAll(spans);

View File

@ -1,24 +1,46 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
import '../providers/settings.dart'; import '../providers/settings.dart';
class Link extends StatelessWidget { class Link extends StatelessWidget {
final Widget child; final Widget child;
final GestureTapCallback onTap; final WidgetBuilder screenBuilder;
final Function beforeRedirect;
final Color bgColor; final Color bgColor;
Link({@required this.child, @required this.onTap, this.bgColor}); Link({
@required this.child,
this.screenBuilder,
this.beforeRedirect,
this.bgColor,
});
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
var theme = SettingsProvider.of(context).theme;
return Material( return Material(
child: Ink( child: Ink(
color: bgColor ?? Colors.white, color: bgColor ?? Colors.white,
child: InkWell( child: InkWell(
splashColor: SettingsProvider.of(context).theme == ThemeMap.cupertino
? Colors.transparent
: null,
onTap: onTap,
child: child, child: child,
splashColor: theme == ThemeMap.cupertino ? Colors.transparent : null,
onTap: () {
if (beforeRedirect != null) {
beforeRedirect();
}
if (screenBuilder != null) {
switch (theme) {
case ThemeMap.cupertino:
Navigator.of(context)
.push(CupertinoPageRoute(builder: screenBuilder));
break;
default:
Navigator.of(context)
.push(MaterialPageRoute(builder: screenBuilder));
}
}
},
), ),
), ),
); );

View File

@ -139,15 +139,15 @@ class _ListScaffoldState extends State<ListScaffold> {
return CupertinoPageScaffold( return CupertinoPageScaffold(
navigationBar: CupertinoNavigationBar( navigationBar: CupertinoNavigationBar(
middle: widget.title, middle: widget.title,
trailing: Link( // trailing: Link(
child: Icon( // child: Icon(
widget.trailingIconData, // widget.trailingIconData,
size: 24, // size: 24,
color: Colors.blueAccent, // color: Colors.blueAccent,
), // ),
onTap: widget.trailingOnTap, // beforeRedirect: widget.trailingOnTap,
bgColor: Colors.transparent, // bgColor: Colors.transparent,
), // ),
), ),
child: SafeArea( child: SafeArea(
child: CustomScrollView( child: CustomScrollView(

View File

@ -52,7 +52,7 @@ class _NotificationItemState extends State<NotificationItem> {
NotificationPayload get payload => widget.payload; NotificationPayload get payload => widget.payload;
bool loading = false; bool loading = false;
Widget _buildRoute() { Widget _buildRoute(BuildContext context) {
switch (payload.type) { switch (payload.type) {
case 'Issue': case 'Issue':
return IssueScreen(payload.number, payload.owner, payload.name); return IssueScreen(payload.number, payload.owner, payload.name);
@ -132,12 +132,8 @@ class _NotificationItemState extends State<NotificationItem> {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return Link( return Link(
onTap: () { screenBuilder: _buildRoute,
_markAsRead(); beforeRedirect: _markAsRead,
Navigator.of(context).push(
CupertinoPageRoute(builder: (context) => _buildRoute()),
);
},
child: Opacity( child: Opacity(
opacity: payload.unread ? 1 : 0.5, opacity: payload.unread ? 1 : 0.5,
child: Container( child: Container(
@ -154,10 +150,7 @@ class _NotificationItemState extends State<NotificationItem> {
overflow: TextOverflow.ellipsis, overflow: TextOverflow.ellipsis,
), ),
), ),
Link( Link(child: _buildCheckIcon(), beforeRedirect: _markAsRead),
child: _buildCheckIcon(),
onTap: _markAsRead,
),
], ],
), ),
), ),

View File

@ -22,14 +22,7 @@ class RepoItem extends StatelessWidget {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return Link( return Link(
onTap: () { screenBuilder: (_) => RepoScreen(item['owner']['login'], item['name']),
Navigator.of(context).push(
CupertinoPageRoute(
builder: (context) =>
RepoScreen(item['owner']['login'], item['name']),
),
);
},
child: Padding( child: Padding(
padding: EdgeInsets.all(10), padding: EdgeInsets.all(10),
child: Row( child: Row(

View File

@ -17,11 +17,7 @@ class UserName extends StatelessWidget {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return Link( return Link(
onTap: () { screenBuilder: (_) => UserScreen(login),
Navigator.of(context).push(
CupertinoPageRoute(builder: (_) => UserScreen(login)),
);
},
child: Container( child: Container(
padding: EdgeInsets.all(2), padding: EdgeInsets.all(2),
decoration: BoxDecoration( decoration: BoxDecoration(