From 51585f9e55363d76d93e0dff5100b7736f448afe Mon Sep 17 00:00:00 2001 From: Rongjian Zhang Date: Wed, 25 Sep 2019 18:47:34 +0800 Subject: [PATCH] refactor: error loading wrapper --- lib/scaffolds/refresh_stateful.dart | 22 +++++----- lib/scaffolds/tab.dart | 33 ++++++--------- lib/scaffolds/tab_stateful.dart | 28 +++++-------- lib/scaffolds/utils.dart | 62 +++++++++++++++++++++++++++++ 4 files changed, 93 insertions(+), 52 deletions(-) diff --git a/lib/scaffolds/refresh_stateful.dart b/lib/scaffolds/refresh_stateful.dart index a9a36c0..dc04180 100644 --- a/lib/scaffolds/refresh_stateful.dart +++ b/lib/scaffolds/refresh_stateful.dart @@ -1,8 +1,6 @@ import 'package:flutter/material.dart'; import 'package:flutter/cupertino.dart'; import 'package:git_touch/scaffolds/utils.dart'; -import '../widgets/loading.dart'; -import '../widgets/error_reload.dart'; class RefreshStatefulScaffold extends StatefulWidget { final Widget title; @@ -58,21 +56,19 @@ class _RefreshStatefulScaffoldState return widget.trailingBuilder(_payload); } - Widget get _body { - if (_error.isNotEmpty) { - return ErrorReload(text: _error, onTap: _refresh); - } else if (_payload == null) { - return Loading(more: false); - } else { - return widget.bodyBuilder(_payload); - } - } - @override Widget build(BuildContext context) { return CommonScaffold( title: widget.title, - body: RefreshWrapper(onRefresh: _refresh, body: _body), + body: RefreshWrapper( + onRefresh: _refresh, + body: ErrorLoadingWrapper( + bodyBuilder: () => widget.bodyBuilder(_payload), + error: _error, + loading: _payload == null, + reload: _refresh, + ), + ), trailing: _trailing, ); } diff --git a/lib/scaffolds/tab.dart b/lib/scaffolds/tab.dart index 2af1e14..abb53d7 100644 --- a/lib/scaffolds/tab.dart +++ b/lib/scaffolds/tab.dart @@ -4,30 +4,23 @@ import 'package:git_touch/models/theme.dart'; import 'package:git_touch/scaffolds/utils.dart'; import 'package:provider/provider.dart'; -class CommonTabPayload { - final List tabs; - final int activeTab; - final Function(int active) onTabSwitch; - CommonTabPayload({ - @required this.tabs, - @required this.activeTab, - @required this.onTabSwitch, - }); -} - class TabScaffold extends StatelessWidget { final Widget title; final Widget body; final Widget trailing; final void Function() onRefresh; - final CommonTabPayload tabPayload; + final List tabs; + final int activeTab; + final Function(int index) onTabSwitch; TabScaffold({ @required this.title, @required this.body, this.trailing, @required this.onRefresh, - @required this.tabPayload, + @required this.tabs, + @required this.activeTab, + @required this.onTabSwitch, }); Widget _buildTitle(BuildContext context) { @@ -36,9 +29,9 @@ class TabScaffold extends StatelessWidget { return DefaultTextStyle( style: TextStyle(fontSize: 16), child: CupertinoSegmentedControl( - groupValue: tabPayload.activeTab, - onValueChanged: tabPayload.onTabSwitch, - children: tabPayload.tabs.asMap().map((key, text) => MapEntry( + groupValue: activeTab, + onValueChanged: onTabSwitch, + children: tabs.asMap().map((key, text) => MapEntry( key, Padding( padding: const EdgeInsets.symmetric(horizontal: 8), @@ -58,10 +51,8 @@ class TabScaffold extends StatelessWidget { body: RefreshWrapper(body: body, onRefresh: onRefresh), trailing: trailing, bottom: TabBar( - onTap: tabPayload.onTabSwitch, - tabs: tabPayload.tabs - .map((text) => Tab(text: text.toUpperCase())) - .toList(), + onTap: onTabSwitch, + tabs: tabs.map((text) => Tab(text: text.toUpperCase())).toList(), ), ); @@ -70,7 +61,7 @@ class TabScaffold extends StatelessWidget { return scaffold; default: return DefaultTabController( - length: tabPayload.tabs.length, + length: tabs.length, child: scaffold, ); } diff --git a/lib/scaffolds/tab_stateful.dart b/lib/scaffolds/tab_stateful.dart index fc5732e..920bed8 100644 --- a/lib/scaffolds/tab_stateful.dart +++ b/lib/scaffolds/tab_stateful.dart @@ -1,8 +1,7 @@ import 'package:flutter/material.dart'; import 'package:flutter/cupertino.dart'; import 'package:git_touch/scaffolds/tab.dart'; -import '../widgets/loading.dart'; -import '../widgets/error_reload.dart'; +import 'package:git_touch/scaffolds/utils.dart'; class TabStatefulScaffold extends StatefulWidget { final Widget title; @@ -66,16 +65,6 @@ class _TabStatefulScaffoldState extends State> { _refresh(); } - Widget _buildBody() { - if (_error.isNotEmpty) { - return ErrorReload(text: _error, onTap: _refresh); - } else if (_payload == null) { - return Loading(more: false); - } else { - return widget.bodyBuilder(_payload, _activeTab); - } - } - Future _refresh() async { try { setState(() { @@ -114,13 +103,16 @@ class _TabStatefulScaffoldState extends State> { return TabScaffold( title: widget.title, trailing: _buildTrailing(), - tabPayload: CommonTabPayload( - tabs: widget.tabs, - activeTab: _activeTab, - onTabSwitch: _switch, - ), + tabs: widget.tabs, + activeTab: _activeTab, + onTabSwitch: _switch, onRefresh: _refresh, - body: _buildBody(), + body: ErrorLoadingWrapper( + bodyBuilder: () => widget.bodyBuilder(_payload, _activeTab), + error: _error, + loading: _payload == null, + reload: _refresh, + ), ); } } diff --git a/lib/scaffolds/utils.dart b/lib/scaffolds/utils.dart index 4d38797..efd669c 100644 --- a/lib/scaffolds/utils.dart +++ b/lib/scaffolds/utils.dart @@ -1,6 +1,8 @@ import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'package:git_touch/models/theme.dart'; +import 'package:git_touch/widgets/link.dart'; +import 'package:git_touch/widgets/loading.dart'; import 'package:provider/provider.dart'; class CommonScaffold extends StatelessWidget { @@ -67,3 +69,63 @@ class RefreshWrapper extends StatelessWidget { } } } + +class ErrorLoadingWrapper extends StatelessWidget { + final String error; + final bool loading; + final void Function() reload; + final Widget Function() bodyBuilder; + + ErrorLoadingWrapper({ + @required this.error, + @required this.loading, + @required this.reload, + @required this.bodyBuilder, + }); + + @override + Widget build(BuildContext context) { + if (error.isNotEmpty) { + return Container( + padding: EdgeInsets.symmetric(vertical: 30, horizontal: 20), + child: Column( + children: [ + Text( + 'Woops, something bad happened. Error message:', + style: TextStyle(fontSize: 16), + ), + Padding(padding: EdgeInsets.only(top: 10)), + Text( + error, + style: TextStyle( + fontSize: 14, + fontWeight: FontWeight.w300, + color: Colors.redAccent, + ), + ), + Padding(padding: EdgeInsets.only(top: 10)), + Link( + child: Text( + 'Reload', + style: TextStyle(fontSize: 20, color: Colors.blueAccent), + ), + onTap: reload, + material: false, + ), + ], + ), + ); + } + + if (loading) { + return Loading(); + } + + return bodyBuilder(); + } +} + +Widget wrapBuilder(Widget Function() builder) { + if (builder == null) return null; + return builder(); +}