From bc068c20eebc45128dc9a3fe090ec40289677e91 Mon Sep 17 00:00:00 2001 From: Rongjian Zhang Date: Tue, 24 Sep 2019 21:04:30 +0800 Subject: [PATCH] refactor: link widget --- lib/screens/login.dart | 4 +-- lib/widgets/link.dart | 30 ++++------------ lib/widgets/notification_item.dart | 55 ++++++++++++++---------------- lib/widgets/table_view.dart | 2 +- 4 files changed, 35 insertions(+), 56 deletions(-) diff --git a/lib/screens/login.dart b/lib/screens/login.dart index c962f8b..d1a84b6 100644 --- a/lib/screens/login.dart +++ b/lib/screens/login.dart @@ -52,8 +52,7 @@ class _LoginScreenState extends State { ); } - Widget _buildAddItem( - {String text, Function onTap, WidgetBuilder screenBuilder}) { + Widget _buildAddItem({String text, Function onTap}) { return Link( child: Container( padding: EdgeInsets.symmetric(vertical: 20), @@ -69,7 +68,6 @@ class _LoginScreenState extends State { ), ), onTap: onTap, - screenBuilder: screenBuilder, ); } diff --git a/lib/widgets/link.dart b/lib/widgets/link.dart index 8ed8ea8..9f8616e 100644 --- a/lib/widgets/link.dart +++ b/lib/widgets/link.dart @@ -9,33 +9,26 @@ class Link extends StatelessWidget { final String url; final WidgetBuilder screenBuilder; final Function onTap; - final Color bgColor; final bool material; final bool fullscreenDialog; - final Icon iconButton; Link({ this.child, this.url, this.screenBuilder, this.onTap, - this.bgColor, this.material = true, this.fullscreenDialog = false, - this.iconButton, - }) : assert(child != null || iconButton != null), - assert(screenBuilder == null || url == null); + }) : assert(screenBuilder == null || url == null); - void _onTap(BuildContext context, int theme) { + void _onTap(BuildContext context) { if (onTap != null) { - onTap(); + return onTap(); } - if (screenBuilder != null) { - Provider.of(context).pushRoute(context, screenBuilder, + return Provider.of(context).pushRoute(context, screenBuilder, fullscreenDialog: fullscreenDialog); } - if (url != null) { launch(url); } @@ -43,30 +36,21 @@ class Link extends StatelessWidget { @override Widget build(BuildContext context) { - var theme = Provider.of(context).theme; - - if (iconButton != null) { - return IconButton( - icon: iconButton, - onPressed: () => _onTap(context, theme), - ); - } - if (!material) { return GestureDetector( child: child, - onTap: () => _onTap(context, theme), + onTap: () => _onTap(context), ); } return Material( child: Ink( - color: bgColor ?? Colors.white, + color: Colors.white, child: InkWell( child: child, // splashColor: // theme == AppThemeType.cupertino ? Colors.transparent : null, - onTap: () => _onTap(context, theme), + onTap: () => _onTap(context), ), ), ); diff --git a/lib/widgets/notification_item.dart b/lib/widgets/notification_item.dart index 607bdfd..7380dc7 100644 --- a/lib/widgets/notification_item.dart +++ b/lib/widgets/notification_item.dart @@ -1,6 +1,7 @@ import 'package:flutter/material.dart'; import 'package:flutter/cupertino.dart'; import 'package:git_touch/models/notification.dart'; +import 'package:git_touch/models/theme.dart'; import 'package:primer/primer.dart'; import 'package:url_launcher/url_launcher.dart'; import '../utils/utils.dart'; @@ -96,38 +97,34 @@ class _NotificationItemState extends State { @override Widget build(BuildContext context) { - WidgetBuilder screenBuilder; - Function onTap; - - switch (payload.type) { - case 'Issue': - case 'PullRequest': - screenBuilder = (_) => IssueScreen( - number: payload.number, - owner: payload.owner, - name: payload.name, - isPullRequest: payload.type == 'PullRequest', - ); - break; - case 'Release': - onTap = () { - launch( - 'https://github.com/${payload.owner}/${payload.name}/releases/tag/${payload.title}'); - }; - break; - case 'Commit': - // TODO: - // onTap = () { - // launch('urlString'); - // }; - break; - } - return Link( - screenBuilder: screenBuilder, onTap: () { _markAsRead(); - if (onTap != null) onTap(); + + switch (payload.type) { + case 'Issue': + case 'PullRequest': + Provider.of(context).pushRoute( + context, + (_) => IssueScreen( + number: payload.number, + owner: payload.owner, + name: payload.name, + isPullRequest: payload.type == 'PullRequest', + )); + + break; + case 'Release': + launch( + 'https://github.com/${payload.owner}/${payload.name}/releases/tag/${payload.title}'); + break; + case 'Commit': + // TODO: + // onTap = () { + // launch('urlString'); + // }; + break; + } }, child: Opacity( opacity: payload.unread ? 1 : 0.5, diff --git a/lib/widgets/table_view.dart b/lib/widgets/table_view.dart index 7faea85..fe859ad 100644 --- a/lib/widgets/table_view.dart +++ b/lib/widgets/table_view.dart @@ -103,7 +103,7 @@ class TableView extends StatelessWidget { ); if (item.onTap == null && item.screenBuilder == null && item.url == null) { - return Ink(color: PrimerColors.white, child: widget); + return widget; } return Link( onTap: item.onTap,