Skip to content

Commit 67af394

Browse files
committed
Hide fab button when listview scroller
1 parent ab3a64d commit 67af394

File tree

1 file changed

+66
-43
lines changed

1 file changed

+66
-43
lines changed

lib/screens/watch_list.dart

Lines changed: 66 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import 'package:flutter/material.dart';
2+
import 'package:flutter/rendering.dart';
23
import 'package:flutter/services.dart';
34
import 'package:provider/provider.dart';
45
import 'package:qr_flutter/qr_flutter.dart';
@@ -15,10 +16,31 @@ class WatchList extends StatefulWidget {
1516
_WatchListState createState() => _WatchListState();
1617
}
1718

18-
class _WatchListState extends State<WatchList> {
19+
class _WatchListState extends State<WatchList> with SingleTickerProviderStateMixin {
1920
int index = 0;
2021
GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey<ScaffoldState>();
2122
String label = "";
23+
ScrollController scrollController;
24+
Animation<double> fabSlideAnimation;
25+
AnimationController fabSlideAnimationController;
26+
27+
@override
28+
void initState() {
29+
super.initState();
30+
scrollController = ScrollController();
31+
fabSlideAnimationController = new AnimationController(duration: Duration(milliseconds: 100), vsync: this)
32+
..addListener(() => setState(() {}));
33+
34+
fabSlideAnimation = Tween(begin: 0.0, end: 100.0).animate(fabSlideAnimationController);
35+
scrollController.addListener(() {
36+
bool isVisible = scrollController.position.userScrollDirection == ScrollDirection.forward;
37+
if (isVisible) {
38+
fabSlideAnimationController.reverse();
39+
} else {
40+
fabSlideAnimationController.forward();
41+
}
42+
});
43+
}
2244

2345
@override
2446
Widget build(BuildContext context) {
@@ -39,7 +61,9 @@ class _WatchListState extends State<WatchList> {
3961
mainAxisSize: MainAxisSize.max,
4062
mainAxisAlignment: MainAxisAlignment.center,
4163
children: <Widget>[
42-
Padding(padding: EdgeInsets.all(12),),
64+
Padding(
65+
padding: EdgeInsets.all(12),
66+
),
4367
FloatingActionButton.extended(
4468
label: Text("Create new watch list"),
4569
icon: Icon(Icons.add),
@@ -54,10 +78,11 @@ class _WatchListState extends State<WatchList> {
5478
);
5579
}
5680
return ListView.builder(
57-
itemCount: model.xpubs.length,
81+
controller: scrollController,
82+
itemCount: 12,
5883
itemBuilder: (context, index) {
5984
return ChangeNotifierProvider.value(
60-
value: model.xpubs[index],
85+
value: model.xpubs[0],
6186
child: SlideUpWrapper(
6287
Card(
6388
elevation: 4,
@@ -90,28 +115,18 @@ class _WatchListState extends State<WatchList> {
90115
context: context,
91116
builder: (con) {
92117
return Card(
93-
color: Theme
94-
.of(context)
95-
.backgroundColor,
118+
color: Theme.of(context).backgroundColor,
96119
elevation: 12,
97120
child: Container(
98-
margin: EdgeInsets.symmetric(
99-
vertical: 22,
100-
horizontal: 12),
121+
margin: EdgeInsets.symmetric(vertical: 22, horizontal: 12),
101122
child: TextField(
102-
controller:
103-
TextEditingController()
104-
..text = model
105-
.xpubs[index]
106-
.label,
123+
controller: TextEditingController()..text = model.xpubs[index].label,
107124
onSubmitted: (str) {
108-
_update(
109-
str, index, context);
125+
_update(str, index, context);
110126
Navigator.pop(context);
111127
},
112128
autofocus: true,
113-
keyboardType:
114-
TextInputType.text,
129+
keyboardType: TextInputType.text,
115130
decoration: InputDecoration(
116131
labelText: 'Label',
117132
)),
@@ -141,18 +156,21 @@ class _WatchListState extends State<WatchList> {
141156
);
142157
}),
143158
),
144-
floatingActionButton: Consumer<Wallet>(builder: (context, model, child) {
145-
if (model.xpubs.length == 0) {
146-
return SizedBox.shrink();
147-
}
148-
return FloatingActionButton(
149-
child: Icon(Icons.add),
150-
heroTag: "actionbtn",
151-
onPressed: () {
152-
_navigate(context);
153-
},
154-
);
155-
}),
159+
floatingActionButton: Transform.translate(
160+
offset: Offset(0, fabSlideAnimation.value),
161+
child: Consumer<Wallet>(builder: (context, model, child) {
162+
if (model.xpubs.length == 0) {
163+
return SizedBox.shrink();
164+
}
165+
return FloatingActionButton(
166+
child: Icon(Icons.add),
167+
heroTag: "actionbtn",
168+
onPressed: () {
169+
_navigate(context);
170+
},
171+
);
172+
}),
173+
),
156174
);
157175
}
158176

@@ -171,8 +189,7 @@ class _WatchListState extends State<WatchList> {
171189
_delete(BuildContext context, int index) async {
172190
bool confirm = await showConfirmModel(
173191
context: context,
174-
title: Text("Are you sure want to Remove?",
175-
style: Theme.of(context).textTheme.subhead),
192+
title: Text("Are you sure want to Remove?", style: Theme.of(context).textTheme.subhead),
176193
iconPositive: new Icon(
177194
Icons.check_circle,
178195
color: Colors.greenAccent[200],
@@ -195,10 +212,7 @@ class _WatchListState extends State<WatchList> {
195212
context: context,
196213
builder: (context) {
197214
return Container(
198-
height: MediaQuery
199-
.of(context)
200-
.size
201-
.height,
215+
height: MediaQuery.of(context).size.height,
202216
child: Card(
203217
margin: EdgeInsets.symmetric(vertical: 2),
204218
child: Center(
@@ -220,8 +234,7 @@ class _WatchListState extends State<WatchList> {
220234
// return Track();
221235
// }));
222236

223-
int result = await Navigator.of(context)
224-
.push(new MaterialPageRoute<int>(builder: (BuildContext context) {
237+
int result = await Navigator.of(context).push(new MaterialPageRoute<int>(builder: (BuildContext context) {
225238
return Track();
226239
}));
227240
if (result != null) {
@@ -243,6 +256,12 @@ class _WatchListState extends State<WatchList> {
243256
}
244257
}
245258
}
259+
260+
@override
261+
void dispose() {
262+
scrollController.dispose();
263+
super.dispose();
264+
}
246265
}
247266

248267
class SlideUpWrapper extends StatefulWidget {
@@ -254,8 +273,7 @@ class SlideUpWrapper extends StatefulWidget {
254273
_SlideUpWrapperState createState() => _SlideUpWrapperState();
255274
}
256275

257-
class _SlideUpWrapperState extends State<SlideUpWrapper>
258-
with SingleTickerProviderStateMixin {
276+
class _SlideUpWrapperState extends State<SlideUpWrapper> with SingleTickerProviderStateMixin {
259277
AnimationController controller;
260278
Animation<double> animation;
261279

@@ -269,8 +287,7 @@ class _SlideUpWrapperState extends State<SlideUpWrapper>
269287
..addListener(() => setState(() {}))
270288
..addStatusListener((state) {});
271289

272-
final Animation curve =
273-
CurvedAnimation(parent: controller, curve: Curves.fastOutSlowIn);
290+
final Animation curve = CurvedAnimation(parent: controller, curve: Curves.fastOutSlowIn);
274291
animation = Tween(begin: 200.0, end: 260.0).animate(curve);
275292
}
276293

@@ -298,4 +315,10 @@ class _SlideUpWrapperState extends State<SlideUpWrapper>
298315
),
299316
);
300317
}
318+
319+
@override
320+
void dispose() {
321+
controller.dispose();
322+
super.dispose();
323+
}
301324
}

0 commit comments

Comments
 (0)