Skip to content

Commit

Permalink
Merge pull request #351 from gazialankus/show_page_with_type_and_sele…
Browse files Browse the repository at this point in the history
…ctor

Show page with type and selector
  • Loading branch information
ulusoyca authored Dec 16, 2024
2 parents 31ffa6f + 376c3b1 commit 78bf0d3
Show file tree
Hide file tree
Showing 5 changed files with 146 additions and 5 deletions.
15 changes: 12 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -928,9 +928,10 @@ This subsection details the methods for moving within the modal's navigation
stack, allowing users to navigate through pages effectively.

#### Direct navigation
Navigate within the modal stack using specific methods to move directly to a
desired page. You can go to the next or previous page, jump to a page at a
specific index in the page list, or navigate to a page by its unique identifier.
Navigate within the modal stack using specific methods to move directly to a
desired page. You can go to the next or previous page, jump to a page at a
specific index in the page list, navigate to a page by its unique identifier,
or even navigate based on the page type with optional filtering criteria.

```dart
// Move to the next page
Expand All @@ -944,6 +945,14 @@ bool navigatedByIndex = WoltModalSheet.of(context).showAtIndex(2);
// Navigate to a page by its unique identifier
bool navigatedById = WoltModalSheet.of(context).showPageWithId(pageId);
// Navigate to a page by its type
bool didShowPage = WoltModalSheet.of(context).showPage<MyCustomPage>();
// Navigate to a page of a certain type that meets additional criteria
bool didShowFilteredPage = WoltModalSheet.of(context).showPage<MyCustomPage>(
where: (page) => page.someProperty == "desiredValue",
);
```

#### Pushing Pages
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import 'package:demo_ui_components/demo_ui_components.dart';
import 'package:flutter/material.dart';
import 'package:wolt_modal_sheet/wolt_modal_sheet.dart';

class ProductWithSkuPage extends WoltModalSheetPage {
final int sku;

ProductWithSkuPage(this.sku)
: super(
heroImage: Stack(
fit: StackFit.passthrough,
children: [
const Image(
image: AssetImage('lib/assets/images/hero_image.jpg'),
fit: BoxFit.cover,
),
Positioned(
right: 30,
bottom: 30,
child: Center(child: ModalSheetTitle('SKU: $sku')),
),
],
),
stickyActionBar: const Padding(
padding: EdgeInsets.fromLTRB(16, 0, 16, 16),
child: WoltModalSheetCloseOrNextSab(),
),
pageTitle: ModalSheetTitle('Product with sku $sku'),
leadingNavBarWidget: const WoltModalSheetBackButton(),
trailingNavBarWidget: const WoltModalSheetCloseButton(),
child: const Padding(
padding: EdgeInsets.only(bottom: 80, top: 16, left: 16, right: 16),
child: ModalSheetContentText('''
Instead of relying on the order of pages or the id values provided to pages, we can also look pages up by type and any other arbitrary value that the page may have. This gives the user the power to address pages in ways that may not be covered by other options.
'''),
),
);
}
15 changes: 13 additions & 2 deletions examples/playground/lib/home/pages/root_sheet_page.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'package:demo_ui_components/demo_ui_components.dart';
import 'package:flutter/material.dart';
import 'package:playground/home/pages/custom_sheet_pages/product_with_sku_page.dart';
import 'package:playground/home/pages/modal_page_name.dart';
import 'package:playground/home/pages/sheet_page_with_custom_top_bar.dart';
import 'package:playground/home/pages/sheet_page_with_min_height.dart';
Expand Down Expand Up @@ -27,6 +28,9 @@ class RootSheetPage {
SheetPageWithTextField.build(),
SheetPageWithMinHeight.build(),
SheetPageWithInAppNavigation.build(),
ProductWithSkuPage(1),
ProductWithSkuPage(2),
ProductWithSkuPage(3),
SheetPageWithCustomTopBar.build(),
SheetPageWithNoPageTitleNoTopBar.build(),
SheetPageWithUpdatePage.build(context, isLastPage: true),
Expand Down Expand Up @@ -153,8 +157,15 @@ class RootSheetPage {
destinationPage = SheetPageWithNonScrollingLayout.build();
break;
case ModalPageName.inAppNavigation:
destinationPage = SheetPageWithInAppNavigation.build();
break;
WoltModalSheet.of(context).addOrReplacePages([
SheetPageWithInAppNavigation.build(),
ProductWithSkuPage(1),
ProductWithSkuPage(2),
ProductWithSkuPage(3),
]);
isButtonEnabledNotifier.value =
selectedItemData.isSelected;
return;
case null:
WoltModalSheet.of(context).addOrReplacePages(
_constructAllPagesAfterRootPage(context),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'package:demo_ui_components/demo_ui_components.dart';
import 'package:flutter/material.dart';
import 'package:playground/home/pages/custom_sheet_pages/product_with_sku_page.dart';
import 'package:playground/home/pages/modal_page_name.dart';
import 'package:playground/home/pages/root_sheet_page.dart';
import 'package:wolt_modal_sheet/wolt_modal_sheet.dart';
Expand Down Expand Up @@ -45,6 +46,8 @@ class SheetPageWithInAppNavigation {
SizedBox(height: 16),
_ButtonForShowPageWithId(),
SizedBox(height: 16),
_ButtonForShowProductWithSku(),
SizedBox(height: 16),
],
),
);
Expand Down Expand Up @@ -299,3 +302,34 @@ class _ButtonForShowPageWithId extends StatelessWidget {
);
}
}

class _ButtonForShowProductWithSku extends StatelessWidget {
const _ButtonForShowProductWithSku();

@override
Widget build(BuildContext context) {
const skus = [1, 2, 3];
return Row(
children: [
for (final sku in skus) ...[
Expanded(
child: WoltElevatedButton(
onPressed: () {
final didShowFilteredPage =
WoltModalSheet.of(context).showPage<ProductWithSkuPage>(
where: (page) => page.sku == sku,
);
String message = didShowFilteredPage
? 'Moved to product page with sku: $sku'
: 'Cannot navigate: No product page with sku $sku found.';
_showMessage(context, message);
},
child: Text('SKU $sku'),
),
),
if (sku != skus.last) const SizedBox(width: 8),
],
],
);
}
}
49 changes: 49 additions & 0 deletions lib/src/wolt_modal_sheet.dart
Original file line number Diff line number Diff line change
Expand Up @@ -903,6 +903,55 @@ class WoltModalSheetState extends State<WoltModalSheet> {
}
return false;
}

/// Navigates to a page identified by both type and an optional selector function parameter `where`.
///
/// This method allows specifying the page to navigate using multiple criteria:
/// - The type of the page specified by `T`.
/// - Arbitrary constraints on the page using the `where` parameter.
///
/// If no page that matches these criteria is found, no navigation will occur.
///
/// Parameters:
/// - [T]: The [SliverWoltModalSheetPage] descendant type of the page to find.
/// - [where]: An optional selector function to further constrain the search. For example,
/// `(page) => page.id == "desired_page_id"`.
///
/// Returns:
/// - [bool]: Returns `true` if navigation to the page was successful, `false` if no page
/// that matches the given criteria is found in the stack.
///
/// Usage Examples:
/// ```dart
/// // Example 1: Navigate to the first occurrence of a page by its type
/// bool didShowPage = WoltModalSheet.of(context).showPage<MyCustomPage>();
/// if (didShowPage) {
/// print("Successfully navigated to MyCustomPage.");
/// } else {
/// print("No MyCustomPage found in the stack.");
/// }
///
/// // Example 2: Navigate to a page of a certain type that meets additional criteria
/// bool didShowFilteredPage = WoltModalSheet.of(context).showPage<MyCustomPage>(
/// where: (page) => page.id == "some_unique_page_id",
/// );
/// if (didShowFilteredPage) {
/// print("Successfully navigated to the MyCustomPage with ID 'some_unique_page_id'.");
/// } else {
/// print("No MyCustomPage with the specified ID found in the stack.");
/// }
/// ```
bool showPage<T extends SliverWoltModalSheetPage>({
bool Function(T page)? where,
}) {
final index = _pages
.indexWhere(where == null ? (p) => p is T : (p) => p is T && where(p));
if (index != -1) {
_currentPageIndex = index;
return true;
}
return false;
}
}

class _WoltModalMultiChildLayoutDelegate extends MultiChildLayoutDelegate {
Expand Down

0 comments on commit 78bf0d3

Please sign in to comment.