Skip to content

Commit

Permalink
Merge pull request #340 from woltapp/fix-modal-decorator
Browse files Browse the repository at this point in the history
Fix modalDecorator Context Wrapping in WoltModalSheet
  • Loading branch information
ulusoyca authored Nov 12, 2024
2 parents 374ac7f + f21b5e5 commit 7c46586
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 16 deletions.
2 changes: 1 addition & 1 deletion lib/src/modal_type/wolt_alert_dialog_type.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class WoltAlertDialogType extends WoltDialogType {

@override
BoxConstraints layoutModal(Size availableSize) {
late double width;
double width;
final availableWidth = availableSize.width;
final screenType = WoltBreakpoints.getScreenTypeForWidth(availableWidth);

Expand Down
2 changes: 1 addition & 1 deletion lib/src/modal_type/wolt_dialog_type.dart
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class WoltDialogType extends WoltModalType {
final double availableWidth = availableSize.width;
final double availableHeight = availableSize.height;

late double width;
double width;

if (availableWidth > horizontalBreakpoint) {
width = minModalWidth;
Expand Down
32 changes: 18 additions & 14 deletions lib/src/wolt_modal_sheet.dart
Original file line number Diff line number Diff line change
Expand Up @@ -311,19 +311,18 @@ class WoltModalSheetState extends State<WoltModalSheet> {
}

@override
void didChangeDependencies() {
super.didChangeDependencies();
if (_pages.isEmpty) {
// Get the initial page list from the initially provided pageListBuilder.
final initialPages = widget.pageListBuilderNotifier.value(context);
assert(initialPages.isNotEmpty,
'pageListBuilder must return a non-empty list.');
_pages = initialPages;
Widget build(BuildContext context) {
final modalDecorator = widget.modalDecorator;
if (modalDecorator != null) {
return modalDecorator(Builder(builder: (decoratedContext) {
return _buildModalContent(decoratedContext);
}));
}

return _buildModalContent(context);
}

@override
Widget build(BuildContext context) {
Widget _buildModalContent(BuildContext context) {
final themeData = Theme.of(context).extension<WoltModalSheetThemeData>();
final defaultThemeData = WoltModalSheetDefaultThemeData(context);
final modalType =
Expand All @@ -332,8 +331,17 @@ class WoltModalSheetState extends State<WoltModalSheet> {
Widget modalContent = ValueListenableBuilder(
valueListenable: widget.pageIndexNotifier,
builder: (context, currentPageIndex, __) {
if (_pages.isEmpty) {
final initialPages = widget.pageListBuilderNotifier.value(context);
assert(
initialPages.isNotEmpty,
'pageListBuilder must return a non-empty list.',
);
_pages = initialPages;
}
final pages = _pages;
final page = pages[currentPageIndex];

final enableDrag = page.enableDrag ??
widget.enableDrag ??
modalType.isDragToDismissEnabled ??
Expand Down Expand Up @@ -441,10 +449,6 @@ class WoltModalSheetState extends State<WoltModalSheet> {
},
);

if (widget.modalDecorator != null) {
modalContent = widget.modalDecorator!(modalContent);
}

return modalContent;
}

Expand Down
30 changes: 30 additions & 0 deletions playground/lib/home/home_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -223,8 +223,20 @@ class _HomeScreenState extends State<HomeScreen> {
Navigator.of(context).pop();
},
pageListBuilder: (BuildContext context) {
assert(
_MyModalDecorator.of(context) != null,
'This is added to test the modalDecorator. It will throw a '
'runtime error if the modalDecorator is broken.',
);

return [RootSheetPage.build(context)];
},
modalDecorator: (child) {
return _MyModalDecorator(
data: 'Hello from _MyModalDecorator',
child: child,
);
},
);
},
child: const Text('Show Modal Sheet'),
Expand Down Expand Up @@ -265,6 +277,24 @@ class _HomeScreenState extends State<HomeScreen> {
}
}

class _MyModalDecorator extends InheritedWidget {
final String data;

const _MyModalDecorator({
required this.data,
required Widget child,
}) : super(child: child);

static _MyModalDecorator? of(BuildContext context) {
return context.dependOnInheritedWidgetOfExactType<_MyModalDecorator>();
}

@override
bool updateShouldNotify(_MyModalDecorator oldWidget) {
return data != oldWidget.data;
}
}

enum _Responsiveness {
alwaysDialog('Always dialog'),
alwaysAlertDialog('Always alert dialog'),
Expand Down

0 comments on commit 7c46586

Please sign in to comment.