Skip to content

Commit

Permalink
Add SheetTheme (#93)
Browse files Browse the repository at this point in the history
Closes #91.
  • Loading branch information
fujidaiti authored Apr 15, 2024
1 parent 021489c commit 1170461
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 4 deletions.
1 change: 1 addition & 0 deletions package/lib/smooth_sheets.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export 'src/foundation/sheet_content_scaffold.dart';
export 'src/foundation/sheet_controller.dart' hide SheetControllerScope;
export 'src/foundation/sheet_extent.dart';
export 'src/foundation/sheet_physics.dart';
export 'src/foundation/theme.dart';
export 'src/modal/cupertino.dart';
export 'src/modal/modal_sheet.dart';
export 'src/navigation/navigation_route.dart';
Expand Down
9 changes: 7 additions & 2 deletions package/lib/src/foundation/sized_content_sheet.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import 'package:smooth_sheets/src/foundation/keyboard_dismissible.dart';
import 'package:smooth_sheets/src/foundation/sheet_controller.dart';
import 'package:smooth_sheets/src/foundation/sheet_extent.dart';
import 'package:smooth_sheets/src/foundation/sheet_physics.dart';
import 'package:smooth_sheets/src/foundation/theme.dart';

/// [SheetExtent] for a [SizedContentSheet].
abstract class SizedContentSheetExtent extends SheetExtent {
Expand Down Expand Up @@ -118,15 +119,19 @@ abstract class SizedContentSheetState<T extends SizedContentSheet>

@override
Widget build(BuildContext context) {
final theme = SheetTheme.maybeOf(context);
final keyboardDismissBehavior =
widget.keyboardDismissBehavior ?? theme?.keyboardDismissBehavior;

Widget result = SheetContainer(
factory: factory,
controller: widget.controller,
child: buildContent(context),
);

if (widget.keyboardDismissBehavior != null) {
if (keyboardDismissBehavior != null) {
result = SheetKeyboardDismissible(
dismissBehavior: widget.keyboardDismissBehavior!,
dismissBehavior: keyboardDismissBehavior,
child: result,
);
}
Expand Down
78 changes: 78 additions & 0 deletions package/lib/src/foundation/theme.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import 'package:flutter/widgets.dart';
import 'package:smooth_sheets/src/foundation/keyboard_dismissible.dart';

/// A theme for descendant sheets.
///
/// The [SheetTheme] is used to configure the default appearance and behavior of
/// descendant sheets. The current theme's [SheetThemeData] object can be
/// obtained by calling [SheetTheme.maybeOf] or [SheetTheme.of].
class SheetTheme extends InheritedWidget {
/// Creates a heme for descendant sheets.
const SheetTheme({
super.key,
required this.data,
required super.child,
});

/// The default appearance and behavior for descendant sheets.
final SheetThemeData data;

@override
bool updateShouldNotify(SheetTheme oldWidget) => data != oldWidget.data;

/// Obtains the closest [SheetThemeData] object from the given [context]
/// if it exists.
static SheetThemeData? maybeOf(BuildContext context) {
return context.dependOnInheritedWidgetOfExactType<SheetTheme>()?.data;
}

/// Obtains the closest [SheetThemeData] object from the given [context].
static SheetThemeData of(BuildContext context) {
final themeData = maybeOf(context);
assert(
themeData != null,
'SheetTheme.of() called with a context that'
'does not contain a SheetTheme.',
);

return themeData!;
}
}

/// A set of properties that describe the appearance and behavior of a sheet.
///
/// Typically, this object is exposed by a [SheetTheme] to
/// the descendant widgets.
class SheetThemeData {
/// Creates a set of properties that describe the appearance and
/// behavior of a sheet.
const SheetThemeData({
this.keyboardDismissBehavior,
});

/// Determines when the on-screen keyboard should be dismissed.
final SheetKeyboardDismissBehavior? keyboardDismissBehavior;

/// Creates a copy of this object but with the given fields replaced with
/// the new values.
SheetThemeData copyWith({
SheetKeyboardDismissBehavior? keyboardDismissBehavior,
}) =>
SheetThemeData(
keyboardDismissBehavior:
keyboardDismissBehavior ?? this.keyboardDismissBehavior,
);

@override
bool operator ==(Object other) =>
identical(this, other) ||
other is SheetThemeData &&
runtimeType == other.runtimeType &&
keyboardDismissBehavior == other.keyboardDismissBehavior;

@override
int get hashCode => Object.hash(
runtimeType,
keyboardDismissBehavior,
);
}
9 changes: 7 additions & 2 deletions package/lib/src/navigation/navigation_sheet.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import 'package:smooth_sheets/src/foundation/sheet_controller.dart';
import 'package:smooth_sheets/src/foundation/sheet_extent.dart';
import 'package:smooth_sheets/src/foundation/sheet_physics.dart';
import 'package:smooth_sheets/src/foundation/sheet_status.dart';
import 'package:smooth_sheets/src/foundation/theme.dart';
import 'package:smooth_sheets/src/internal/transition_observer.dart';

typedef NavigationSheetTransitionObserver = TransitionObserver;
Expand Down Expand Up @@ -95,6 +96,10 @@ class NavigationSheetState extends State<NavigationSheet>

@override
Widget build(BuildContext context) {
final theme = SheetTheme.maybeOf(context);
final keyboardDismissBehavior =
widget.keyboardDismissBehavior ?? theme?.keyboardDismissBehavior;

Widget result = SheetContainer(
factory: const _NavigationSheetExtentFactory(),
controller: widget.controller,
Expand All @@ -104,9 +109,9 @@ class NavigationSheetState extends State<NavigationSheet>
child: widget.child,
);

if (widget.keyboardDismissBehavior != null) {
if (keyboardDismissBehavior != null) {
result = SheetKeyboardDismissible(
dismissBehavior: widget.keyboardDismissBehavior!,
dismissBehavior: keyboardDismissBehavior,
child: result,
);
}
Expand Down

0 comments on commit 1170461

Please sign in to comment.