mirror of
https://github.com/git-touch/git-touch
synced 2025-01-31 08:04:51 +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/material.dart';
|
||||||
import 'package:flutter/cupertino.dart';
|
import 'package:flutter/cupertino.dart';
|
||||||
import 'package:git_touch/scaffolds/utils.dart';
|
import 'package:git_touch/scaffolds/utils.dart';
|
||||||
import '../widgets/loading.dart';
|
|
||||||
import '../widgets/error_reload.dart';
|
|
||||||
|
|
||||||
class RefreshStatefulScaffold<T> extends StatefulWidget {
|
class RefreshStatefulScaffold<T> extends StatefulWidget {
|
||||||
final Widget title;
|
final Widget title;
|
||||||
@ -58,21 +56,19 @@ class _RefreshStatefulScaffoldState<T>
|
|||||||
return widget.trailingBuilder(_payload);
|
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
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
return CommonScaffold(
|
return CommonScaffold(
|
||||||
title: widget.title,
|
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,
|
trailing: _trailing,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -4,30 +4,23 @@ import 'package:git_touch/models/theme.dart';
|
|||||||
import 'package:git_touch/scaffolds/utils.dart';
|
import 'package:git_touch/scaffolds/utils.dart';
|
||||||
import 'package:provider/provider.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 {
|
class TabScaffold extends StatelessWidget {
|
||||||
final Widget title;
|
final Widget title;
|
||||||
final Widget body;
|
final Widget body;
|
||||||
final Widget trailing;
|
final Widget trailing;
|
||||||
final void Function() onRefresh;
|
final void Function() onRefresh;
|
||||||
final CommonTabPayload tabPayload;
|
final List<String> tabs;
|
||||||
|
final int activeTab;
|
||||||
|
final Function(int index) onTabSwitch;
|
||||||
|
|
||||||
TabScaffold({
|
TabScaffold({
|
||||||
@required this.title,
|
@required this.title,
|
||||||
@required this.body,
|
@required this.body,
|
||||||
this.trailing,
|
this.trailing,
|
||||||
@required this.onRefresh,
|
@required this.onRefresh,
|
||||||
@required this.tabPayload,
|
@required this.tabs,
|
||||||
|
@required this.activeTab,
|
||||||
|
@required this.onTabSwitch,
|
||||||
});
|
});
|
||||||
|
|
||||||
Widget _buildTitle(BuildContext context) {
|
Widget _buildTitle(BuildContext context) {
|
||||||
@ -36,9 +29,9 @@ class TabScaffold extends StatelessWidget {
|
|||||||
return DefaultTextStyle(
|
return DefaultTextStyle(
|
||||||
style: TextStyle(fontSize: 16),
|
style: TextStyle(fontSize: 16),
|
||||||
child: CupertinoSegmentedControl(
|
child: CupertinoSegmentedControl(
|
||||||
groupValue: tabPayload.activeTab,
|
groupValue: activeTab,
|
||||||
onValueChanged: tabPayload.onTabSwitch,
|
onValueChanged: onTabSwitch,
|
||||||
children: tabPayload.tabs.asMap().map((key, text) => MapEntry(
|
children: tabs.asMap().map((key, text) => MapEntry(
|
||||||
key,
|
key,
|
||||||
Padding(
|
Padding(
|
||||||
padding: const EdgeInsets.symmetric(horizontal: 8),
|
padding: const EdgeInsets.symmetric(horizontal: 8),
|
||||||
@ -58,10 +51,8 @@ class TabScaffold extends StatelessWidget {
|
|||||||
body: RefreshWrapper(body: body, onRefresh: onRefresh),
|
body: RefreshWrapper(body: body, onRefresh: onRefresh),
|
||||||
trailing: trailing,
|
trailing: trailing,
|
||||||
bottom: TabBar(
|
bottom: TabBar(
|
||||||
onTap: tabPayload.onTabSwitch,
|
onTap: onTabSwitch,
|
||||||
tabs: tabPayload.tabs
|
tabs: tabs.map((text) => Tab(text: text.toUpperCase())).toList(),
|
||||||
.map((text) => Tab(text: text.toUpperCase()))
|
|
||||||
.toList(),
|
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
|
||||||
@ -70,7 +61,7 @@ class TabScaffold extends StatelessWidget {
|
|||||||
return scaffold;
|
return scaffold;
|
||||||
default:
|
default:
|
||||||
return DefaultTabController(
|
return DefaultTabController(
|
||||||
length: tabPayload.tabs.length,
|
length: tabs.length,
|
||||||
child: scaffold,
|
child: scaffold,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -1,8 +1,7 @@
|
|||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:flutter/cupertino.dart';
|
import 'package:flutter/cupertino.dart';
|
||||||
import 'package:git_touch/scaffolds/tab.dart';
|
import 'package:git_touch/scaffolds/tab.dart';
|
||||||
import '../widgets/loading.dart';
|
import 'package:git_touch/scaffolds/utils.dart';
|
||||||
import '../widgets/error_reload.dart';
|
|
||||||
|
|
||||||
class TabStatefulScaffold<T> extends StatefulWidget {
|
class TabStatefulScaffold<T> extends StatefulWidget {
|
||||||
final Widget title;
|
final Widget title;
|
||||||
@ -66,16 +65,6 @@ class _TabStatefulScaffoldState<T> extends State<TabStatefulScaffold<T>> {
|
|||||||
_refresh();
|
_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 {
|
Future<void> _refresh() async {
|
||||||
try {
|
try {
|
||||||
setState(() {
|
setState(() {
|
||||||
@ -114,13 +103,16 @@ class _TabStatefulScaffoldState<T> extends State<TabStatefulScaffold<T>> {
|
|||||||
return TabScaffold(
|
return TabScaffold(
|
||||||
title: widget.title,
|
title: widget.title,
|
||||||
trailing: _buildTrailing(),
|
trailing: _buildTrailing(),
|
||||||
tabPayload: CommonTabPayload(
|
tabs: widget.tabs,
|
||||||
tabs: widget.tabs,
|
activeTab: _activeTab,
|
||||||
activeTab: _activeTab,
|
onTabSwitch: _switch,
|
||||||
onTabSwitch: _switch,
|
|
||||||
),
|
|
||||||
onRefresh: _refresh,
|
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/cupertino.dart';
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:git_touch/models/theme.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';
|
import 'package:provider/provider.dart';
|
||||||
|
|
||||||
class CommonScaffold extends StatelessWidget {
|
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…
x
Reference in New Issue
Block a user