mirror of
https://github.com/git-touch/git-touch
synced 2025-01-18 18:29:59 +01:00
refactor: error loading wrapper
This commit is contained in:
parent
1226e8ff8d
commit
51585f9e55
@ -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<T> extends StatefulWidget {
|
||||
final Widget title;
|
||||
@ -58,21 +56,19 @@ class _RefreshStatefulScaffoldState<T>
|
||||
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,
|
||||
);
|
||||
}
|
||||
|
@ -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<String> 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<String> 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,
|
||||
);
|
||||
}
|
||||
|
@ -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<T> extends StatefulWidget {
|
||||
final Widget title;
|
||||
@ -66,16 +65,6 @@ class _TabStatefulScaffoldState<T> extends State<TabStatefulScaffold<T>> {
|
||||
_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<void> _refresh() async {
|
||||
try {
|
||||
setState(() {
|
||||
@ -114,13 +103,16 @@ class _TabStatefulScaffoldState<T> extends State<TabStatefulScaffold<T>> {
|
||||
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,
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@ -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: <Widget>[
|
||||
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();
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user