diff --git a/CHANGELOG.md b/CHANGELOG.md index 9c87a209..fe11f0c8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,8 @@ +## 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. @@ -5,7 +10,7 @@ * The `SlideToDismissDelegate` classes. ### Modified -* Added a renderingMode parameter in the `SlideActionBuilder` signature . +* Added a renderingMode parameter in the `SlideActionBuilder` signature. ## 0.3.2 ### Added diff --git a/README.md b/README.md index dc78348e..6a8ccb2b 100644 --- a/README.md +++ b/README.md @@ -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. @@ -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: @@ -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. @@ -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. diff --git a/example/lib/main.dart b/example/lib/main.dart index 9c6599b5..874ed301 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -27,6 +27,7 @@ class MyHomePage extends StatefulWidget { } class _MyHomePageState extends State { + final SlidableController slidableController = new SlidableController(); final List<_HomeItem> items = List.generate( 20, (i) => new _HomeItem( @@ -44,7 +45,13 @@ class _MyHomePageState extends State { 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. ); } @@ -115,6 +122,7 @@ class _MyHomePageState extends State { //final int t = index; return new Slidable( key: new Key(item.title), + controller: slidableController, direction: direction, slideToDismissDelegate: new SlideToDismissDrawerDelegate( onDismissed: (actionType) { @@ -171,6 +179,7 @@ class _MyHomePageState extends State { return new Slidable.builder( key: new Key(item.title), + controller: slidableController, direction: direction, slideToDismissDelegate: new SlideToDismissDrawerDelegate( onWillDismiss: (item.index != 10) diff --git a/lib/src/widgets/slidable.dart b/lib/src/widgets/slidable.dart index 6ddb5902..689c7b33 100644 --- a/lib/src/widgets/slidable.dart +++ b/lib/src/widgets/slidable.dart @@ -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, @@ -619,6 +630,7 @@ class Slidable extends StatefulWidget { bool closeOnScroll = true, bool enabled = true, SlideToDismissDelegate slideToDismissDelegate, + SlidableController controller, }) : this.builder( key: key, child: child, @@ -633,6 +645,7 @@ class Slidable extends StatefulWidget { closeOnScroll: closeOnScroll, enabled: enabled, slideToDismissDelegate: slideToDismissDelegate, + controller: controller, ); /// Creates a widget that can be slid. @@ -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( @@ -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; @@ -869,6 +886,7 @@ class SlidableState extends State _actionsMoveController.dispose(); _resizeController?.dispose(); _removeScrollingNotifierListener(); + widget.controller?.activeState = null; super.dispose(); } @@ -910,6 +928,7 @@ class SlidableState extends State void _handleDragStart(DragStartDetails details) { _dragUnderway = true; + widget.controller?.activeState = this; _dragExtent = _actionsMoveController.value * _actionsDragAxisExtent * _dragExtent.sign; @@ -923,6 +942,10 @@ class SlidableState extends State } void _handleDragUpdate(DragUpdateDetails details) { + if (widget.controller != null && widget.controller.activeState != this) { + return; + } + final double delta = details.primaryDelta; _dragExtent += delta; setState(() { @@ -935,6 +958,10 @@ class SlidableState extends State } 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; diff --git a/pubspec.yaml b/pubspec.yaml index 11c7af7e..bc1f08af 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -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 homepage: https://github.com/letsar/flutter_slidable