Skip to content
Open
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
3 changes: 2 additions & 1 deletion lib/src/draggable_menu/menu/controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import 'package:flutter/material.dart';

class DraggableMenuController extends ChangeNotifier {
late void Function(int level) animateTo;

///You can use `DraggableMenuController` to control the `DraggableMenu` widget.
///
/// Provide the `DraggableMenuController` to the `controller` parameter of the `DraggableMenu`.
Expand All @@ -27,4 +26,6 @@ class DraggableMenuController extends ChangeNotifier {
init(void Function(int level) animateTo) {
this.animateTo = (level) => animateTo(level);
}

bool isOpen = false;
}
24 changes: 18 additions & 6 deletions lib/src/draggable_menu/menu/menu.dart
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ class DraggableMenu extends StatefulWidget {
///
/// Throws if the level doesn't exist.
final int startLevel;

/// Creates a Draggable Menu widget.
///
/// To push the Draggable Menu to the screen, you can use the `DraggableMenu`'s `open` and `openReplacement` methods.
Expand Down Expand Up @@ -239,8 +239,10 @@ class DraggableMenu extends StatefulWidget {
Curve? popCurve,
bool? barrier,
Color? barrierColor,
}) =>
Navigator.of(context).push<T>(
DraggableMenuController? controller,
}) {
controller?.isOpen = true;
return Navigator.of(context).push<T>(
MenuRoute<T>(
child: draggableMenu,
duration: animationDuration,
Expand All @@ -250,6 +252,7 @@ class DraggableMenu extends StatefulWidget {
barrierColor: barrierColor,
),
);
}

/// Opens the given Draggable Menu using `Navigator`'s `pushReplacement` method.
///
Expand All @@ -262,8 +265,10 @@ class DraggableMenu extends StatefulWidget {
Curve? popCurve,
bool? barrier,
Color? barrierColor,
}) =>
Navigator.of(context).pushReplacement(
DraggableMenuController? controller,
}) {
controller?.isOpen = true;
return Navigator.of(context).pushReplacement(
MenuRoute(
child: draggableMenu,
duration: animationDuration,
Expand All @@ -273,6 +278,7 @@ class DraggableMenu extends StatefulWidget {
barrierColor: barrierColor,
),
);
}

@override
State<DraggableMenu> createState() => _DraggableMenuState();
Expand Down Expand Up @@ -344,9 +350,14 @@ class _DraggableMenuState extends State<DraggableMenu>
void _controllerInit() {
// Initilizes the controller (Passes the _animeteTo method)
widget.controller?.init((int level) => _animateTo(level));
widget.controller?.isOpen = true;

// Adds itself as listener to the controller
widget.controller?.addListener(() => setState(() {}));
widget.controller?.addListener(() {
if (mounted) { // Check if the state is still mounted
setState(() {});
}
});
}

/// Initilizes the `DraggableMenu` widget's levels.
Expand Down Expand Up @@ -889,6 +900,7 @@ class _DraggableMenuState extends State<DraggableMenu>

@override
void dispose() {
widget.controller?.isOpen = false;
_ticker.dispose();
_animationController.dispose();
super.dispose();
Expand Down