1
0
mirror of https://github.com/git-touch/git-touch synced 2024-12-16 10:20:55 +01:00

feat: disable refresh on tab switch

This commit is contained in:
Rongjian Zhang 2019-09-24 20:35:37 +08:00
parent d5363f613e
commit 09c1ded405

View File

@ -9,15 +9,15 @@ class TabScaffold<T> extends StatefulWidget {
final Widget title; final Widget title;
final Widget Function(T payload) bodyBuilder; final Widget Function(T payload) bodyBuilder;
final Future<T> Function(int activeTab) onRefresh; final Future<T> Function(int activeTab) onRefresh;
final Widget Function(T payload) trailingBuilder;
final List<String> tabs; final List<String> tabs;
final Widget Function(T payload) trailingBuilder;
TabScaffold({ TabScaffold({
@required this.title, @required this.title,
@required this.bodyBuilder, @required this.bodyBuilder,
@required this.onRefresh, @required this.onRefresh,
@required this.tabs,
this.trailingBuilder, this.trailingBuilder,
this.tabs,
}); });
@override @override
@ -26,10 +26,41 @@ class TabScaffold<T> extends StatefulWidget {
class _TabScaffoldState<T> extends State<TabScaffold<T>> { class _TabScaffoldState<T> extends State<TabScaffold<T>> {
bool _loading; bool _loading;
T _payload; T _payload0;
T _payload1;
T _payload2;
String _error = ''; String _error = '';
int _activeTab = 0; int _activeTab = 0;
T _getPayload(int selected) {
switch (selected) {
case 0:
return _payload0;
case 1:
return _payload1;
case 2:
return _payload2;
default:
throw '';
}
}
T get _payload => _getPayload(_activeTab);
set _payload(T v) {
switch (_activeTab) {
case 0:
_payload0 = v;
break;
case 1:
_payload1 = v;
break;
case 2:
_payload2 = v;
break;
}
}
@override @override
void initState() { void initState() {
super.initState(); super.initState();
@ -46,16 +77,13 @@ class _TabScaffoldState<T> extends State<TabScaffold<T>> {
} }
} }
Future<void> _refresh([int activeTab]) async { Future<void> _refresh() async {
try { try {
setState(() { setState(() {
_error = ''; _error = '';
_loading = true; _loading = true;
if (activeTab != null) {
_activeTab = activeTab;
}
}); });
_payload = await widget.onRefresh(activeTab); _payload = await widget.onRefresh(_activeTab);
} catch (err) { } catch (err) {
_error = err.toString(); _error = err.toString();
throw err; throw err;
@ -68,6 +96,15 @@ class _TabScaffoldState<T> extends State<TabScaffold<T>> {
} }
} }
Future<void> _switch(int selected)async {
setState(() {
_activeTab = selected;
});
if (_getPayload(selected) == null) {
await _refresh();
}
}
Widget _buildTrailing() { Widget _buildTrailing() {
if (_payload == null || widget.trailingBuilder == null) return null; if (_payload == null || widget.trailingBuilder == null) return null;
@ -87,14 +124,14 @@ class _TabScaffoldState<T> extends State<TabScaffold<T>> {
return CupertinoPageScaffold( return CupertinoPageScaffold(
navigationBar: CupertinoNavigationBar( navigationBar: CupertinoNavigationBar(
middle: DefaultTextStyle( middle: DefaultTextStyle(
style: TextStyle(), style: TextStyle(fontSize: 16),
child: CupertinoSegmentedControl( child: CupertinoSegmentedControl(
groupValue: _activeTab, groupValue: _activeTab,
onValueChanged: _refresh, onValueChanged: _switch,
children: widget.tabs.asMap().map((key, text) => MapEntry( children: widget.tabs.asMap().map((key, text) => MapEntry(
key, key,
Padding( Padding(
padding: const EdgeInsets.symmetric(horizontal: 4), padding: const EdgeInsets.symmetric(horizontal: 8),
child: Text(text), child: Text(text),
))), ))),
), ),
@ -118,7 +155,7 @@ class _TabScaffoldState<T> extends State<TabScaffold<T>> {
title: widget.title, title: widget.title,
actions: _buildActions(), actions: _buildActions(),
bottom: TabBar( bottom: TabBar(
onTap: _refresh, onTap: _switch,
tabs: widget.tabs tabs: widget.tabs
.map((text) => Tab(text: text.toUpperCase())) .map((text) => Tab(text: text.toUpperCase()))
.toList(), .toList(),