Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

controller open to manage the state. #442

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,34 @@ Animate the actions as if they were streched while the `Slidable` is moving:

![Stretch Motion][stretch_motion]

### Controller

You can use ```SlidableController``` to open or close the actions programmatically:

```dart
final controller = SlidableController();
// ...
Slidable(
controller: controller,
// ...
);
// ...
// Open the actions
void _handleOpen() {
controller.openEndActionPane();
// OR
//controller.openStartActionPane();
}
void _handleOpen() {
controller.close();
}
```

## FAQ

You can read the FAQ here: https://github.com/letsar/flutter_slidable/wiki/FAQ
Expand Down
22 changes: 15 additions & 7 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,18 @@ import 'package:flutter_slidable/flutter_slidable.dart';

void main() => runApp(const MyApp());

class MyApp extends StatelessWidget {
class MyApp extends StatefulWidget {
const MyApp({
Key? key,
}) : super(key: key);

@override
State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> with SingleTickerProviderStateMixin {
late final controller = SlidableController(this);

@override
Widget build(BuildContext context) {
return MaterialApp(
Expand Down Expand Up @@ -48,21 +55,21 @@ class MyApp extends StatelessWidget {
),

// The end action pane is the one at the right or the bottom side.
endActionPane: const ActionPane(
motion: ScrollMotion(),
endActionPane: ActionPane(
motion: const ScrollMotion(),
children: [
SlidableAction(
// An action can be bigger than the others.
flex: 2,
onPressed: doNothing,
backgroundColor: Color(0xFF7BC043),
onPressed: (_) => controller.openEndActionPane(),
backgroundColor: const Color(0xFF7BC043),
foregroundColor: Colors.white,
icon: Icons.archive,
label: 'Archive',
),
SlidableAction(
onPressed: doNothing,
backgroundColor: Color(0xFF0392CF),
onPressed: (_) => controller.close(),
backgroundColor: const Color(0xFF0392CF),
foregroundColor: Colors.white,
icon: Icons.save,
label: 'Save',
Expand All @@ -75,6 +82,7 @@ class MyApp extends StatelessWidget {
child: const ListTile(title: Text('Slide me')),
),
Slidable(
controller: controller,
// Specify a key if the Slidable is dismissible.
key: const ValueKey(1),

Expand Down
20 changes: 18 additions & 2 deletions lib/src/slidable.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class Slidable extends StatefulWidget {
/// [useTextDirection] and [child] arguments must not be null.
const Slidable({
Key? key,
this.controller,
this.groupTag,
this.enabled = true,
this.closeOnScroll = true,
Expand All @@ -30,6 +31,9 @@ class Slidable extends StatefulWidget {
required this.child,
}) : super(key: key);

/// The Slidable widget controller.
final SlidableController? controller;

/// Whether this slidable is interactive.
///
/// If false, the child will not slid to show actions.
Expand Down Expand Up @@ -134,7 +138,7 @@ class _SlidableState extends State<Slidable>
@override
void initState() {
super.initState();
controller = SlidableController(this)
controller = (widget.controller ?? SlidableController(this))
..actionPaneType.addListener(handleActionPanelTypeChanged);
}

Expand All @@ -149,14 +153,25 @@ class _SlidableState extends State<Slidable>
@override
void didUpdateWidget(covariant Slidable oldWidget) {
super.didUpdateWidget(oldWidget);

if (oldWidget.controller != widget.controller) {
controller.actionPaneType.removeListener(handleActionPanelTypeChanged);

controller = (widget.controller ?? SlidableController(this))
..actionPaneType.addListener(handleActionPanelTypeChanged);
}

updateIsLeftToRight();
updateController();
}

@override
void dispose() {
controller.actionPaneType.removeListener(handleActionPanelTypeChanged);
controller.dispose();

if (controller != widget.controller) {
controller.dispose();
}
super.dispose();
}

Expand Down Expand Up @@ -213,6 +228,7 @@ class _SlidableState extends State<Slidable>
}

ActionPane? get startActionPane => widget.startActionPane;

ActionPane? get endActionPane => widget.endActionPane;

Alignment get actionPaneAlignment {
Expand Down