-
Notifications
You must be signed in to change notification settings - Fork 5
/
main_tab.dart
144 lines (134 loc) · 5.25 KB
/
main_tab.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import 'package:exchange_app/logic/market/market_bloc.dart';
import 'package:exchange_app/logic/market/market_provider.dart';
import 'package:exchange_app/widgets/market/banner.dart';
import 'package:exchange_app/widgets/market/market_item.dart';
import 'package:exchange_app/widgets/market/sort_bar.dart';
import 'package:exchange_app/config/ui.dart';
import 'package:exchange_app/logic/market/market_model.dart';
import 'package:flutter/material.dart';
class MarketTab {
MarketTab({this.key, this.label});
final String key;
final String label;
}
final List<MarketTab> _allPages = PRICE_MARKETS.keys
.map((label) => MarketTab(key: label, label: label))
.toList();
class MarketTabPage extends StatefulWidget {
@override
_MarketTabPageState createState() => _MarketTabPageState();
}
class _MarketTabPageState extends State<MarketTabPage>
with SingleTickerProviderStateMixin {
TabController _tabController;
@override
void initState() {
super.initState();
_tabController = TabController(vsync: this, length: 4, initialIndex: 1);
_tabController.addListener(_handleTabSelection);
}
void _handleTabSelection() {
MarketProvider.of(context)
.marketBloc
.setCurrentMarket(_allPages[_tabController.index].key);
}
Widget buildAppBar() => AppBar(
title: Text('Markets', style: UiConfig.appBarTextStyle),
backgroundColor: Colors.transparent,
elevation: 0,
centerTitle: false,
actions: [
IconButton(icon: Icon(Icons.search), iconSize: 25, onPressed: () {})
],
);
Widget buildSliverContent() => SliverFixedExtentList(
// Here we need to avoid that the content goes under the Tab/Sort Bar,
// so we need to controll the height of the remaining space
itemExtent: MediaQuery.of(context).size.height -
MediaQuery.of(context).padding.top -
// TODO: maybe is a Flutter bug, on iPhoneX padding.bottom is 0, I think it should be 34px
MediaQuery.of(context).padding.bottom -
40 - // Market TabBar
40 - // Market SortBar
20 -
(-4) - // Add Border Bottom/Shadow
kToolbarHeight - // AppBar
kBottomNavigationBarHeight, // Bottom TabBar
delegate: SliverChildBuilderDelegate(
(BuildContext context, int index) {
return TabBarView(
controller: _tabController,
children: _allPages.map<Widget>(buildTabView).toList());
},
childCount: 1,
),
);
Widget buildTabView(MarketTab marketTab) => Container(
color: Colors.white,
child: StreamBuilder<List<MarketItem>>(
stream: MarketProvider.of(context).marketBloc.markets[marketTab.key],
builder: (context, snapshot) {
if (snapshot.hasError) {
return Text(snapshot.error);
}
if (!snapshot.hasData) {
return Center(child: CircularProgressIndicator());
}
if (snapshot.data.length == 0) {
return Container(
margin: EdgeInsets.symmetric(vertical: 30),
height: 50,
alignment: Alignment.topCenter,
child: Text('No records'));
}
return ListView.builder(
shrinkWrap: true,
physics: ClampingScrollPhysics(),
itemCount: snapshot.data.length,
itemBuilder: (BuildContext ctxt, int index) {
return MarketItemWidget(snapshot.data[index]);
});
}));
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.transparent,
appBar: buildAppBar(),
body: RefreshIndicator(
onRefresh: () =>
MarketProvider.of(context).marketBloc.updateMarketData(),
child: CustomScrollView(
primary: true,
physics: ClampingScrollPhysics(),
slivers: <Widget>[
SliverAppBar(
floating: false,
pinned: false,
backgroundColor: Colors.transparent,
elevation: 0,
expandedHeight: 140.0,
flexibleSpace: MarketBanner()),
SliverAppBar(
pinned: true,
floating: false,
backgroundColor: Colors.transparent,
elevation: 0,
bottom: PreferredSize(
preferredSize: Size.fromHeight(40),
child: MarketSortBar()),
flexibleSpace: PreferredSize(
preferredSize: Size.fromHeight(40),
child: TabBar(
indicatorColor: Colors.white,
indicatorWeight: 1.00,
controller: _tabController,
tabs: _allPages
.map<Widget>((MarketTab page) =>
Tab(text: page.label.toUpperCase()))
.toList(),
)),
),
buildSliverContent()
])));
}
}