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

Attach default controller to sheet if not explicitly specified #102

Merged
merged 1 commit into from
Apr 22, 2024
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
5 changes: 3 additions & 2 deletions package/lib/smooth_sheets.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/// Comprehensive bottom sheet library supporting imperative and declarative
/// navigation APIs, nested navigation, persistent and modal styles (including
/// the iOS flavor), and more.
/// the iOS flavor), and more.
library smooth_sheets;

export 'src/draggable/draggable_sheet.dart';
Expand All @@ -12,7 +12,8 @@ export 'src/foundation/keyboard_dismissible.dart';
export 'src/foundation/notifications.dart';
export 'src/foundation/physics.dart';
export 'src/foundation/sheet_content_scaffold.dart';
export 'src/foundation/sheet_controller.dart' hide SheetControllerScope;
export 'src/foundation/sheet_controller.dart'
hide ImplicitSheetControllerScope, SheetControllerScope;
export 'src/foundation/sheet_extent.dart';
export 'src/foundation/theme.dart';
export 'src/modal/cupertino.dart';
Expand Down
27 changes: 16 additions & 11 deletions package/lib/src/draggable/draggable_sheet.dart
Original file line number Diff line number Diff line change
Expand Up @@ -71,18 +71,23 @@ class DraggableSheet extends StatelessWidget {
final keyboardDismissBehavior =
this.keyboardDismissBehavior ?? theme?.keyboardDismissBehavior;

Widget result = SheetContainer(
Widget result = ImplicitSheetControllerScope(
controller: controller,
config: DraggableSheetExtentConfig(
initialExtent: initialExtent,
minExtent: minExtent,
maxExtent: maxExtent,
physics: physics,
),
child: SheetDraggable(
behavior: hitTestBehavior,
child: child,
),
builder: (context, controller) {
return SheetContainer(
controller: controller,
config: DraggableSheetExtentConfig(
initialExtent: initialExtent,
minExtent: minExtent,
maxExtent: maxExtent,
physics: physics,
),
child: SheetDraggable(
behavior: hitTestBehavior,
child: child,
),
);
},
);

if (keyboardDismissBehavior != null) {
Expand Down
8 changes: 5 additions & 3 deletions package/lib/src/foundation/framework.dart
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter/widgets.dart';

import 'sheet_controller.dart';
import 'sheet_extent.dart';

class SheetContainer extends StatelessWidget {
const SheetContainer({
super.key,
this.controller,
this.onExtentChanged,
required this.controller,
required this.config,
required this.child,
});

final SheetController? controller;
final SheetController controller;
final ValueChanged<SheetExtent?>? onExtentChanged;
final SheetExtentConfig config;
final Widget child;
Expand All @@ -21,7 +23,7 @@ class SheetContainer extends StatelessWidget {
Widget build(BuildContext context) {
return SheetExtentScope(
config: config,
controller: controller ?? SheetControllerScope.maybeOf(context),
controller: controller,
onExtentChanged: onExtentChanged,
child: Builder(
builder: (context) {
Expand Down
35 changes: 35 additions & 0 deletions package/lib/src/foundation/sheet_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'package:flutter/foundation.dart';
import 'package:flutter/scheduler.dart';
import 'package:flutter/widgets.dart';
import 'package:meta/meta.dart';

import 'sheet_extent.dart';

class SheetController extends ChangeNotifier
Expand Down Expand Up @@ -113,6 +114,40 @@ class SheetControllerScope extends InheritedWidget {
}
}

/// A widget that ensures that a [SheetController] is available in the subtree.
///
/// The [builder] callback will be called with the [controller] if it is
/// explicitly provided and is not null, or a [SheetController] that is hosted
/// in the nearest ancestor [SheetControllerScope]. If neither is found, a newly
/// created [SheetController] hosted in a [DefaultSheetController] will be
/// used as a fallback.
@internal
class ImplicitSheetControllerScope extends StatelessWidget {
const ImplicitSheetControllerScope({
super.key,
this.controller,
required this.builder,
});

final SheetController? controller;
final Widget Function(BuildContext, SheetController) builder;

@override
Widget build(BuildContext context) {
return switch (controller ?? DefaultSheetController.maybeOf(context)) {
final controller? => builder(context, controller),
null => DefaultSheetController(
child: Builder(
builder: (context) {
final controller = DefaultSheetController.of(context);
return builder(context, controller);
},
),
),
};
}
}

class DefaultSheetController extends StatefulWidget {
const DefaultSheetController({
super.key,
Expand Down
15 changes: 10 additions & 5 deletions package/lib/src/navigation/navigation_sheet.dart
Original file line number Diff line number Diff line change
Expand Up @@ -101,13 +101,18 @@ class NavigationSheetState extends State<NavigationSheet>
final keyboardDismissBehavior =
widget.keyboardDismissBehavior ?? theme?.keyboardDismissBehavior;

Widget result = SheetContainer(
config: const _NavigationSheetExtentConfig(),
Widget result = ImplicitSheetControllerScope(
controller: widget.controller,
onExtentChanged: (extent) {
_extent = extent as _NavigationSheetExtent?;
builder: (context, controller) {
return SheetContainer(
config: const _NavigationSheetExtentConfig(),
controller: widget.controller ?? DefaultSheetController.of(context),
onExtentChanged: (extent) {
_extent = extent as _NavigationSheetExtent?;
},
child: widget.child,
);
},
child: widget.child,
);

if (keyboardDismissBehavior != null) {
Expand Down
21 changes: 13 additions & 8 deletions package/lib/src/scrollable/scrollable_sheet.dart
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,20 @@ class ScrollableSheet extends StatelessWidget {
final keyboardDismissBehavior =
this.keyboardDismissBehavior ?? theme?.keyboardDismissBehavior;

Widget result = SheetContainer(
Widget result = ImplicitSheetControllerScope(
controller: controller,
config: ScrollableSheetExtentConfig(
initialExtent: initialExtent,
minExtent: minExtent,
maxExtent: maxExtent,
physics: physics,
),
child: PrimarySheetContentScrollController(child: child),
builder: (context, controller) {
return SheetContainer(
controller: controller,
config: ScrollableSheetExtentConfig(
initialExtent: initialExtent,
minExtent: minExtent,
maxExtent: maxExtent,
physics: physics,
),
child: PrimarySheetContentScrollController(child: child),
);
},
);

if (keyboardDismissBehavior != null) {
Expand Down
Loading