tn_bottom_sheet_navigator 1.0.0
tn_bottom_sheet_navigator: ^1.0.0 copied to clipboard
Modal bottom sheets and dialogs with their own internal navigation stack, animated transitions and a height that adapts to the visible page.
TnBottomSheetNavigator #
Modal bottom sheets (and dialogs) with their own internal navigation stack and a height that adapts to the visible page, with animated transitions. Zero dependencies besides Flutter.
Requirements #
- Flutter >=3.22.0 / Dart >=3.4.1
- Android and iOS
Usage #
1. Define your routes #
Routes are plain values: define them wherever you like (a top level final, a feature module...). No global setup in main.
final authRoutes = [
TnRoute(path: 'signin', builder: (context, _) => const SignInPage()),
TnRoute(path: 'signup', builder: (context, _) => const SignUpPage()),
TnRoute(
path: 'forgot-password',
builder: (context, email) => ForgotPasswordPage(email: email as String),
),
];
2. Open the sheet #
final token = await showTnBottomSheet<String>(
context: context,
routes: authRoutes,
initialRoute: 'signin',
);
The future completes with whatever the pages pass to context.tnClose(result) (or null if the sheet is dismissed). Several sheets can be open at once, each one with its own stack.
Options (all optional): initialArguments, isDismissible (default true), enableDrag, isScrollControlled, useRootNavigator, showDragHandle, backgroundColor, barrierColor, shape, clipBehavior, constraints (default: max 90% of the screen height) and transitionDuration (Duration.zero disables animations).
3. Navigate from the pages #
context.tnPush('signup'); // add a page
context.tnPush('forgot-password', arguments: email); // ...with arguments
context.tnReplace('signup'); // swap the visible page
context.tnGo('signin'); // reset the stack
context.tnPop(); // back one page
context.tnPopToRoot(); // back to the first page
context.tnClose('ok'); // close the whole sheet
// push returns a Future that completes when that page is popped
final picked = await context.tnPush<Color>('color-picker');
// ...and in the color picker: context.tnPop(Colors.red);
For anything else use the controller: context.tnNavigator (or TnNavigator.of(context)) exposes stack, canPop, currentPath, popUntil(...).
TnNavigator.of(context) rebuilds the caller when the stack changes; context.tnNavigator does not.
Previous pages stay alive (offstage) while others are on top, so their state (text fields, scroll position...) is preserved when going back. The Android back button / gesture goes back in the stack first, and only then closes the sheet.
TnBottomSheetScaffold & TnBottomSheetAppBar #
TnBottomSheetScaffold(
appBar: const TnBottomSheetAppBar(title: 'Sign in', showCloseIcon: true),
padding: const EdgeInsets.all(16),
child: SingleChildScrollView(child: Column(...)),
)
The app bar shows a back button automatically when the stack can pop. child gets the remaining space: wrap long content in a scrollable so it scrolls when the sheet reaches its max height.
TnBottomSheetAppBar props: title, titleWidget, showCloseIcon, theme (TnBottomSheetAppBarTheme(closeIconSize, titleTextStyle, padding)).
Dialogs and other places: TnNavigator #
TnNavigator is the widget behind the sheet; it can be used anywhere.
showDialog(
context: context,
builder: (context) => AlertDialog(
content: Material(
color: Colors.transparent,
child: TnNavigator(routes: authRoutes, initialRoute: 'signin'),
),
),
);
Testing #
TnNavigatorController has no dependency on widgets, so navigation logic can be unit tested directly:
final c = TnNavigatorController(routes: authRoutes, initialRoute: 'signin');
c.push('signup');
expect(c.currentPath, 'signup');
Migrating from 0.x #
| 0.x | 1.0 |
|---|---|
TnRouter().setRoutes([...]) in main |
pass routes: to showTnBottomSheet / TnNavigator |
TnBottomSheetRoute(path, builder: (ctx, params) ...) |
TnRoute(path, builder: (ctx, arguments) ...); arguments is an Object? instead of a Map |
context.showTnBottomSheetNav('x', settings: ..., params: ...) / showTnBottomSheetNavigator(...) |
showTnBottomSheet(context:, routes:, initialRoute:, initialArguments:, <options>) |
TnBottomSheetSettings |
options are plain named parameters; isDismisable -> isDismissible (now defaults to true) |
context.tnPush('x', params: {...}) |
context.tnPush('x', arguments: ...) (also tnReplace, tnGo, tnPop, tnPopToRoot, tnClose) |
TnRouter() singleton |
context.tnNavigator / TnNavigator.of(context) (a TnNavigatorController) |
TnNavigatorBuilder(initialPath:) |
TnNavigator(routes:, initialRoute:) |
TnBottomSheetNavigatorBuilder |
removed (use TnNavigator) |
TnBottomSheetAppBar(customTitle:) |
TnBottomSheetAppBar(titleWidget:) |
TnBottomSheetScaffold(appBar:, child:) |
same, appBar is now optional and padding was added; child is flexible |
| navigating to an unknown path silently did nothing | throws an ArgumentError listing the registered paths |