Skip to content

Commit 82a9b52

Browse files
committed
add onSlideAnimationChanged and onSlideIsOpenChanged callbacks in controller
1 parent 84bb9d1 commit 82a9b52

File tree

3 files changed

+171
-3
lines changed

3 files changed

+171
-3
lines changed

example/lib/main.dart

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class MyHomePage extends StatefulWidget {
2727
}
2828

2929
class _MyHomePageState extends State<MyHomePage> {
30-
final SlidableController slidableController = new SlidableController();
30+
SlidableController slidableController;
3131
final List<_HomeItem> items = List.generate(
3232
20,
3333
(i) => new _HomeItem(
@@ -38,6 +38,30 @@ class _MyHomePageState extends State<MyHomePage> {
3838
),
3939
);
4040

41+
@protected
42+
void initState() {
43+
slidableController = new SlidableController(
44+
onSlideAnimationChanged: handleSlideAnimationChanged,
45+
onSlideIsOpenChanged: handleSlideIsOpenChanged,
46+
);
47+
super.initState();
48+
}
49+
50+
Animation<double> _rotationAnimation;
51+
Color _fabColor = Colors.blue;
52+
53+
void handleSlideAnimationChanged(Animation<double> slideAnimation) {
54+
setState(() {
55+
_rotationAnimation = slideAnimation;
56+
});
57+
}
58+
59+
void handleSlideIsOpenChanged(bool isOpen) {
60+
setState(() {
61+
_fabColor = isOpen ? Colors.green : Colors.blue;
62+
});
63+
}
64+
4165
@override
4266
Widget build(BuildContext context) {
4367
return new Scaffold(
@@ -52,7 +76,17 @@ class _MyHomePageState extends State<MyHomePage> {
5276
? Axis.vertical
5377
: Axis.horizontal),
5478
),
55-
), // This trailing comma makes auto-formatting nicer for build methods.
79+
),
80+
floatingActionButton: FloatingActionButton(
81+
backgroundColor: _fabColor,
82+
onPressed: null,
83+
child: _rotationAnimation == null
84+
? Icon(Icons.add)
85+
: RotationTransition(
86+
turns: _rotationAnimation,
87+
child: Icon(Icons.add),
88+
),
89+
),
5690
);
5791
}
5892

example/lib/main_34.dart

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
import 'dart:async';
2+
3+
import 'package:flutter/material.dart';
4+
import 'package:flutter_slidable/flutter_slidable.dart';
5+
6+
class Data {
7+
final String url = 'https://via.placeholder.com/350x150';
8+
}
9+
10+
void main() => runApp(new MyApp());
11+
12+
Future<Data> getData() async {
13+
await Future.delayed(Duration(seconds: 1));
14+
return Data();
15+
}
16+
17+
class MyApp extends StatelessWidget {
18+
// This widget is the root of your application.
19+
@override
20+
Widget build(BuildContext context) {
21+
return new MaterialApp(
22+
title: 'Flutter Slidable Demo',
23+
theme: new ThemeData(
24+
primarySwatch: Colors.blue,
25+
),
26+
home: new MyHomePage(title: 'Flutter Slidable Demo'),
27+
);
28+
}
29+
}
30+
31+
class MyHomePage extends StatefulWidget {
32+
MyHomePage({Key key, this.title}) : super(key: key);
33+
34+
final String title;
35+
36+
@override
37+
_MyHomePageState createState() => new _MyHomePageState();
38+
}
39+
40+
class _MyHomePageState extends State<MyHomePage> {
41+
final SlidableController slidableController = new SlidableController();
42+
43+
@override
44+
Widget build(BuildContext context) {
45+
return new Scaffold(
46+
appBar: new AppBar(
47+
title: new Text(widget.title),
48+
),
49+
body: ListView.builder(
50+
itemCount: 20,
51+
itemBuilder: (context, index) {
52+
return FutureBuilder<Data>(
53+
future: getData(),
54+
builder: (context, snapshot) {
55+
if (snapshot.hasData) {
56+
return Slidable(
57+
delegate: SlidableScrollDelegate(),
58+
actionExtentRatio: 0.25,
59+
secondaryActions: <Widget>[
60+
IconSlideAction(
61+
caption: 'Delete',
62+
color: Colors.red,
63+
icon: Icons.delete,
64+
//onTap: () => removeLocation(location),
65+
),
66+
],
67+
child: ListTile(
68+
// onTap: () {
69+
// Navigator.pushNamed(context, Routes.closeUp);
70+
// },
71+
72+
leading: SizedBox(
73+
width: 64.0,
74+
height: 64.0,
75+
child: ClipRRect(
76+
borderRadius: BorderRadius.circular(64.0),
77+
child: RepaintBoundary(
78+
child: Image(
79+
image: NetworkImage(snapshot.data.url),
80+
),
81+
),
82+
),
83+
),
84+
),
85+
);
86+
}
87+
return CircularProgressIndicator();
88+
},
89+
);
90+
},
91+
),
92+
); // This trailing comma makes auto-formatting nicer for build methods.
93+
}
94+
}

lib/src/widgets/slidable.dart

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -590,11 +590,45 @@ class SlidableDrawerDelegate extends SlidableStackDelegate {
590590
/// A controller that keep tracks of the active [SlidableState] and close
591591
/// the previous one.
592592
class SlidableController {
593+
SlidableController({
594+
this.onSlideAnimationChanged,
595+
this.onSlideIsOpenChanged,
596+
});
597+
598+
final ValueChanged<Animation<double>> onSlideAnimationChanged;
599+
final ValueChanged<bool> onSlideIsOpenChanged;
600+
bool _isSlideOpen;
601+
602+
Animation<double> _slideAnimation;
603+
593604
SlidableState _activeState;
594605
SlidableState get activeState => _activeState;
595606
set activeState(SlidableState value) {
596-
_activeState?.close();
607+
_activeState?._flingAnimationControllers();
608+
597609
_activeState = value;
610+
if (onSlideAnimationChanged != null) {
611+
_slideAnimation?.removeListener(_handleSlideIsOpenChanged);
612+
if (onSlideIsOpenChanged != null) {
613+
_slideAnimation = value?.overallMoveAnimation;
614+
_slideAnimation?.addListener(_handleSlideIsOpenChanged);
615+
if (_slideAnimation == null) {
616+
_isSlideOpen = false;
617+
onSlideIsOpenChanged(_isSlideOpen);
618+
}
619+
}
620+
onSlideAnimationChanged(value?.overallMoveAnimation);
621+
}
622+
}
623+
624+
void _handleSlideIsOpenChanged() {
625+
if (onSlideIsOpenChanged != null && _slideAnimation != null) {
626+
final bool isOpen = _slideAnimation.value != 0.0;
627+
if (isOpen != _isSlideOpen) {
628+
_isSlideOpen = isOpen;
629+
onSlideIsOpenChanged(_isSlideOpen);
630+
}
631+
}
598632
}
599633
}
600634

@@ -903,6 +937,11 @@ class SlidableState extends State<Slidable>
903937
}
904938

905939
void close() {
940+
_flingAnimationControllers();
941+
widget.controller?.activeState = null;
942+
}
943+
944+
void _flingAnimationControllers() {
906945
if (!_dismissing) {
907946
_actionsMoveController.fling(velocity: -1.0);
908947
_overallMoveController.fling(velocity: -1.0);
@@ -1021,6 +1060,7 @@ class SlidableState extends State<Slidable>
10211060
}
10221061

10231062
void _handleDismiss() {
1063+
widget.controller?.activeState = null;
10241064
final SlideToDismissDelegate slideToDismissDelegate =
10251065
widget.slideToDismissDelegate;
10261066
if (slideToDismissDelegate.onDismissed != null) {

0 commit comments

Comments
 (0)