Skip to content

Commit 435ed7e

Browse files
authored
Merge pull request #21 from letsar/feature/controller
Add SlidableController
2 parents b0584c1 + 39cb6da commit 435ed7e

File tree

5 files changed

+62
-5
lines changed

5 files changed

+62
-5
lines changed

CHANGELOG.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
1+
## 0.4.1
2+
### Added
3+
* The `SlidableController` class.
4+
* The `controller` argument on `Slidable` constructors to enable keeping only one `Slidable` open.
5+
16
## 0.4.0
27
### Added
38
* The `SlidableRenderingMode` enum.
49
* The `SlideActionType` enum.
510
* The `SlideToDismissDelegate` classes.
611

712
### Modified
8-
* Added a renderingMode parameter in the `SlideActionBuilder` signature .
13+
* Added a renderingMode parameter in the `SlideActionBuilder` signature.
914

1015
## 0.3.2
1116
### Added

README.md

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ A Flutter implementation of slidable list item with directional slide actions th
99

1010
## Features
1111

12-
* Accepts left and right widget lists as slide actions.
12+
* Accepts primary (left/top) and secondary (right/bottom) widget lists as slide actions.
1313
* Can be dismissed.
1414
* 4 built-in layouts.
1515
* 2 built-in slide action widgets.
@@ -27,7 +27,7 @@ In the `pubspec.yaml` of your flutter project, add the following dependency:
2727
```yaml
2828
dependencies:
2929
...
30-
flutter_slidable: "^0.4.0"
30+
flutter_slidable: "^0.4.1"
3131
```
3232
3333
In your library add the following import:
@@ -137,6 +137,8 @@ The slide actions stretch while the item is sliding:
137137

138138
![Overview](https://raw.githubusercontent.com/letsar/flutter_slidable/master/doc/images/slidable_stretch.gif)
139139

140+
### FAQ
141+
140142
#### How to prevent my slide action to close after it has been tapped?
141143

142144
By default, `SlideAction` and `IconSlideAction` close on tap.
@@ -217,6 +219,20 @@ slideToDismissDelegate: new SlideToDismissDrawerDelegate(
217219
),
218220
```
219221

222+
#### How to let keep only one `Slidable` open?
223+
224+
You have to set the `controller` argument of the `Slidable` constructors to a `SlidableController` instance:
225+
226+
```dart
227+
final SlidableController slidableController = new SlidableController();
228+
...
229+
new Slidable(
230+
key: new Key(item.title),
231+
controller: slidableController,
232+
...
233+
);
234+
```
235+
220236
## Changelog
221237

222238
Please see the [Changelog](https://github.com/letsar/flutter_slidable/blob/master/CHANGELOG.md) page to know what's recently changed.

example/lib/main.dart

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ class MyHomePage extends StatefulWidget {
2727
}
2828

2929
class _MyHomePageState extends State<MyHomePage> {
30+
final SlidableController slidableController = new SlidableController();
3031
final List<_HomeItem> items = List.generate(
3132
20,
3233
(i) => new _HomeItem(
@@ -44,7 +45,13 @@ class _MyHomePageState extends State<MyHomePage> {
4445
title: new Text(widget.title),
4546
),
4647
body: new Center(
47-
child: _buildList(context, Axis.vertical),
48+
child: new OrientationBuilder(
49+
builder: (context, orientation) => _buildList(
50+
context,
51+
orientation == Orientation.portrait
52+
? Axis.vertical
53+
: Axis.horizontal),
54+
),
4855
), // This trailing comma makes auto-formatting nicer for build methods.
4956
);
5057
}
@@ -115,6 +122,7 @@ class _MyHomePageState extends State<MyHomePage> {
115122
//final int t = index;
116123
return new Slidable(
117124
key: new Key(item.title),
125+
controller: slidableController,
118126
direction: direction,
119127
slideToDismissDelegate: new SlideToDismissDrawerDelegate(
120128
onDismissed: (actionType) {
@@ -171,6 +179,7 @@ class _MyHomePageState extends State<MyHomePage> {
171179

172180
return new Slidable.builder(
173181
key: new Key(item.title),
182+
controller: slidableController,
174183
direction: direction,
175184
slideToDismissDelegate: new SlideToDismissDrawerDelegate(
176185
onWillDismiss: (item.index != 10)

lib/src/widgets/slidable.dart

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -584,6 +584,17 @@ class SlidableDrawerDelegate extends SlidableStackDelegate {
584584
}
585585
}
586586

587+
/// A controller that keep tracks of the active [SlidableState] and close
588+
/// the previous one.
589+
class SlidableController {
590+
SlidableState _activeState;
591+
SlidableState get activeState => _activeState;
592+
set activeState(SlidableState value) {
593+
_activeState?.close();
594+
_activeState = value;
595+
}
596+
}
597+
587598
/// A widget that can be slid in both direction of the specified axis.
588599
///
589600
/// If the direction is [Axis.horizontal], this widget can be slid to the left or to the right,
@@ -619,6 +630,7 @@ class Slidable extends StatefulWidget {
619630
bool closeOnScroll = true,
620631
bool enabled = true,
621632
SlideToDismissDelegate slideToDismissDelegate,
633+
SlidableController controller,
622634
}) : this.builder(
623635
key: key,
624636
child: child,
@@ -633,6 +645,7 @@ class Slidable extends StatefulWidget {
633645
closeOnScroll: closeOnScroll,
634646
enabled: enabled,
635647
slideToDismissDelegate: slideToDismissDelegate,
648+
controller: controller,
636649
);
637650

638651
/// Creates a widget that can be slid.
@@ -663,6 +676,7 @@ class Slidable extends StatefulWidget {
663676
this.closeOnScroll = true,
664677
this.enabled = true,
665678
this.slideToDismissDelegate,
679+
this.controller,
666680
}) : assert(delegate != null),
667681
assert(direction != null),
668682
assert(
@@ -684,6 +698,9 @@ class Slidable extends StatefulWidget {
684698
/// The widget below this widget in the tree.
685699
final Widget child;
686700

701+
/// The controller that tracks the active [Slidable] and keep only one open.
702+
final SlidableController controller;
703+
687704
/// A delegate that builds slide actions that appears when the child has been dragged
688705
/// down or to the right.
689706
final SlideActionDelegate actionDelegate;
@@ -869,6 +886,7 @@ class SlidableState extends State<Slidable>
869886
_actionsMoveController.dispose();
870887
_resizeController?.dispose();
871888
_removeScrollingNotifierListener();
889+
widget.controller?.activeState = null;
872890
super.dispose();
873891
}
874892

@@ -910,6 +928,7 @@ class SlidableState extends State<Slidable>
910928

911929
void _handleDragStart(DragStartDetails details) {
912930
_dragUnderway = true;
931+
widget.controller?.activeState = this;
913932
_dragExtent = _actionsMoveController.value *
914933
_actionsDragAxisExtent *
915934
_dragExtent.sign;
@@ -923,6 +942,10 @@ class SlidableState extends State<Slidable>
923942
}
924943

925944
void _handleDragUpdate(DragUpdateDetails details) {
945+
if (widget.controller != null && widget.controller.activeState != this) {
946+
return;
947+
}
948+
926949
final double delta = details.primaryDelta;
927950
_dragExtent += delta;
928951
setState(() {
@@ -935,6 +958,10 @@ class SlidableState extends State<Slidable>
935958
}
936959

937960
void _handleDragEnd(DragEndDetails details) {
961+
if (widget.controller != null && widget.controller.activeState != this) {
962+
return;
963+
}
964+
938965
_dragUnderway = false;
939966
final double velocity = details.primaryVelocity;
940967
final bool shouldOpen = velocity.sign == _dragExtent.sign;

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: flutter_slidable
22
description: A Flutter implementation of slidable list item with directional slide actions that can be dismissed.
3-
version: 0.4.0
3+
version: 0.4.1
44
author: Romain Rastel <[email protected]>
55
homepage: https://github.com/letsar/flutter_slidable
66

0 commit comments

Comments
 (0)