Skip to content

Commit

Permalink
Merge pull request #21 from letsar/feature/controller
Browse files Browse the repository at this point in the history
Add SlidableController
  • Loading branch information
letsar committed Aug 8, 2018
2 parents b0584c1 + 39cb6da commit 435ed7e
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 5 deletions.
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
## 0.4.1
### Added
* The `SlidableController` class.
* The `controller` argument on `Slidable` constructors to enable keeping only one `Slidable` open.

## 0.4.0
### Added
* The `SlidableRenderingMode` enum.
* The `SlideActionType` enum.
* The `SlideToDismissDelegate` classes.

### Modified
* Added a renderingMode parameter in the `SlideActionBuilder` signature .
* Added a renderingMode parameter in the `SlideActionBuilder` signature.

## 0.3.2
### Added
Expand Down
20 changes: 18 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ A Flutter implementation of slidable list item with directional slide actions th

## Features

* Accepts left and right widget lists as slide actions.
* Accepts primary (left/top) and secondary (right/bottom) widget lists as slide actions.
* Can be dismissed.
* 4 built-in layouts.
* 2 built-in slide action widgets.
Expand All @@ -27,7 +27,7 @@ In the `pubspec.yaml` of your flutter project, add the following dependency:
```yaml
dependencies:
...
flutter_slidable: "^0.4.0"
flutter_slidable: "^0.4.1"
```

In your library add the following import:
Expand Down Expand Up @@ -137,6 +137,8 @@ The slide actions stretch while the item is sliding:

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

### FAQ

#### How to prevent my slide action to close after it has been tapped?

By default, `SlideAction` and `IconSlideAction` close on tap.
Expand Down Expand Up @@ -217,6 +219,20 @@ slideToDismissDelegate: new SlideToDismissDrawerDelegate(
),
```

#### How to let keep only one `Slidable` open?

You have to set the `controller` argument of the `Slidable` constructors to a `SlidableController` instance:

```dart
final SlidableController slidableController = new SlidableController();
...
new Slidable(
key: new Key(item.title),
controller: slidableController,
...
);
```

## Changelog

Please see the [Changelog](https://github.com/letsar/flutter_slidable/blob/master/CHANGELOG.md) page to know what's recently changed.
Expand Down
11 changes: 10 additions & 1 deletion example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class MyHomePage extends StatefulWidget {
}

class _MyHomePageState extends State<MyHomePage> {
final SlidableController slidableController = new SlidableController();
final List<_HomeItem> items = List.generate(
20,
(i) => new _HomeItem(
Expand All @@ -44,7 +45,13 @@ class _MyHomePageState extends State<MyHomePage> {
title: new Text(widget.title),
),
body: new Center(
child: _buildList(context, Axis.vertical),
child: new OrientationBuilder(
builder: (context, orientation) => _buildList(
context,
orientation == Orientation.portrait
? Axis.vertical
: Axis.horizontal),
),
), // This trailing comma makes auto-formatting nicer for build methods.
);
}
Expand Down Expand Up @@ -115,6 +122,7 @@ class _MyHomePageState extends State<MyHomePage> {
//final int t = index;
return new Slidable(
key: new Key(item.title),
controller: slidableController,
direction: direction,
slideToDismissDelegate: new SlideToDismissDrawerDelegate(
onDismissed: (actionType) {
Expand Down Expand Up @@ -171,6 +179,7 @@ class _MyHomePageState extends State<MyHomePage> {

return new Slidable.builder(
key: new Key(item.title),
controller: slidableController,
direction: direction,
slideToDismissDelegate: new SlideToDismissDrawerDelegate(
onWillDismiss: (item.index != 10)
Expand Down
27 changes: 27 additions & 0 deletions lib/src/widgets/slidable.dart
Original file line number Diff line number Diff line change
Expand Up @@ -584,6 +584,17 @@ class SlidableDrawerDelegate extends SlidableStackDelegate {
}
}

/// A controller that keep tracks of the active [SlidableState] and close
/// the previous one.
class SlidableController {
SlidableState _activeState;
SlidableState get activeState => _activeState;
set activeState(SlidableState value) {
_activeState?.close();
_activeState = value;
}
}

/// A widget that can be slid in both direction of the specified axis.
///
/// If the direction is [Axis.horizontal], this widget can be slid to the left or to the right,
Expand Down Expand Up @@ -619,6 +630,7 @@ class Slidable extends StatefulWidget {
bool closeOnScroll = true,
bool enabled = true,
SlideToDismissDelegate slideToDismissDelegate,
SlidableController controller,
}) : this.builder(
key: key,
child: child,
Expand All @@ -633,6 +645,7 @@ class Slidable extends StatefulWidget {
closeOnScroll: closeOnScroll,
enabled: enabled,
slideToDismissDelegate: slideToDismissDelegate,
controller: controller,
);

/// Creates a widget that can be slid.
Expand Down Expand Up @@ -663,6 +676,7 @@ class Slidable extends StatefulWidget {
this.closeOnScroll = true,
this.enabled = true,
this.slideToDismissDelegate,
this.controller,
}) : assert(delegate != null),
assert(direction != null),
assert(
Expand All @@ -684,6 +698,9 @@ class Slidable extends StatefulWidget {
/// The widget below this widget in the tree.
final Widget child;

/// The controller that tracks the active [Slidable] and keep only one open.
final SlidableController controller;

/// A delegate that builds slide actions that appears when the child has been dragged
/// down or to the right.
final SlideActionDelegate actionDelegate;
Expand Down Expand Up @@ -869,6 +886,7 @@ class SlidableState extends State<Slidable>
_actionsMoveController.dispose();
_resizeController?.dispose();
_removeScrollingNotifierListener();
widget.controller?.activeState = null;
super.dispose();
}

Expand Down Expand Up @@ -910,6 +928,7 @@ class SlidableState extends State<Slidable>

void _handleDragStart(DragStartDetails details) {
_dragUnderway = true;
widget.controller?.activeState = this;
_dragExtent = _actionsMoveController.value *
_actionsDragAxisExtent *
_dragExtent.sign;
Expand All @@ -923,6 +942,10 @@ class SlidableState extends State<Slidable>
}

void _handleDragUpdate(DragUpdateDetails details) {
if (widget.controller != null && widget.controller.activeState != this) {
return;
}

final double delta = details.primaryDelta;
_dragExtent += delta;
setState(() {
Expand All @@ -935,6 +958,10 @@ class SlidableState extends State<Slidable>
}

void _handleDragEnd(DragEndDetails details) {
if (widget.controller != null && widget.controller.activeState != this) {
return;
}

_dragUnderway = false;
final double velocity = details.primaryVelocity;
final bool shouldOpen = velocity.sign == _dragExtent.sign;
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: flutter_slidable
description: A Flutter implementation of slidable list item with directional slide actions that can be dismissed.
version: 0.4.0
version: 0.4.1
author: Romain Rastel <lets4r@gmail.com>
homepage: https://github.com/letsar/flutter_slidable

Expand Down

0 comments on commit 435ed7e

Please sign in to comment.