Skip to content

Commit

Permalink
First release
Browse files Browse the repository at this point in the history
  • Loading branch information
esDotDev committed Apr 14, 2021
1 parent 5f1586d commit 63e4f8e
Show file tree
Hide file tree
Showing 15 changed files with 503 additions and 225 deletions.
3 changes: 1 addition & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
## 0.0.1

* TODO: Describe initial release.
* First release
10 changes: 9 additions & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1 +1,9 @@
TODO: Add your license here.
MIT License

Copyright (c) 2019 gskinner.com

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
140 changes: 131 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,136 @@
<img src="http://screens.gskinner.com/shawn/example_g9GiSnHDVp.png" alt="" />

# nav_stack

A new Flutter package.
A simple but powerful path-based navigation router with full web-browser and deeplink support.

`NavStack` maintains a stateful list of routes which you define declaratively while providing a powerful imperitive API for controlling your navigation history stack.

## 🔨 Installation
```yaml
dependencies:
nav_stack: ^0.0.1
```

### ⚙ Import

```dart
import 'package:nav_stack/nav_stack.dart';
```

## 🕹️ Usage
When it comes to declaring your routes, `NavStack` uses `PathStack` under the hood. There is a wide variety of routing configutations you can create, and they are explained in some detail here: https://pub.dev/packages/path_stack.

`NavStack` builds on top of `PathStack` by connecting it's `.path` property it to `MaterialApp.router` and supplying a strong imperitive controller. This creates a turn-key navigation router that is very simple and expressive, but also powerful and flexible.

### Hello NavStack
In it's simplest form, it might look something like:
```dart
return NavStack(
stackBuilder: (context, controller) => PathStack(
path: controller.path,
// Use scaffold builder to wrap all our pages in a tab-menu
scaffoldBuilder: (_, stack) => _TabScaffold(["/home", "/profile"], child: stack),
routes: {
// Alias "/" will catch the default path and send it to /home
["/home", "/"]: LoginScreen().buildStackRoute(),
["/profile"]: ProfileScreen().buildStackRoute(),
},
),
);
```

This might not look like much, but there is a lot going on here.
* This is fully bound to the browser path,
* It will also receive deeplink start up values on any platform,
* It provides a `controller` which you can use to easily change the global path at any time,
* All routes are persistent, maintaining their state as you navigate between them (optional)
* A persistent scaffold wraps all children, and it is also stateful

Note: String literals are used here for brevity. In real usage, it is recommended you give each page it's own path property like `HomePage.path` or `LoginScreen.path`. This makes it much easier to construct and share links from other sections in your app.

### Nested Routes and Guards
Other features like nested routes, and route guards are also supported. In this example there is a protected section, that requires the user to be logged in. Otherwise they are redirected to `/home`:
```dart
return NavStack(
stackBuilder: (context, controller) {
return PathStack(
path: controller.path,
scaffoldBuilder: (_, stack) => _MyScaffold(stack),
routes: {
[LoginScreen.path, "/"]: LoginScreen().buildStackRoute(),
["/in/"]: PathStack(
path: controller.path,
routes: {
["/in/${ProfileScreen.path}:id"]: ProfileScreen().buildStackRoute(),
["/in/${SettingsScreen.path}"]: SettingsScreen().buildStackRoute(),
},
).buildStackRoute(onBeforeEnter: (_) {
if (!isConnected) controller.redirect("/login", () => showAuthWarning(context));
return isConnected; // returning false here will stop the page from changing
}),
},
);
},
);
```

There are many other options you can provide to the `PathStack`, including `unknownPathBuilder`, `transitionBuilder` and, `basePath`. For an exhaustive list, check out this example:
* https://github.com/gskinnerTeam/flutter_path_stack/blob/master/example/lib/full_api_example.dart

### Defining paths and arguments
Both path-based or query-string args are supported by `PathStack` under the hood. For more information on the routing rules and options check out the docs in the `PathStack` package: https://pub.dev/packages/path_stack#defining-paths

As a quick refresher, consuming path-based args (`/billing/88/99`) looks like:
```
["billing/:foo/:bar"]:
StackRouteBuilder(builder: (_, args) => BillingPage(id: "${args["foo"]}_${args["bar"]}")),
```

Consuming query-string args (`/billing/?foo=88&bar=99`) looks like:
```
["billing/"]:
StackRouteBuilder(builder: (_, args) => BillingPage(id: "${args["foo"]}_${args["bar"]}")),
```


For a some more complex code examples of path structure you can check here:
* https://github.com/gskinnerTeam/flutter_path_stack/blob/master/example/lib/simple_tab_example.dart
* https://github.com/gskinnerTeam/flutter_path_stack/blob/master/example/lib/advanced_tab_example.dart

### Imperitive API

`NavStack` offers a strong imperitive API for interacting with your navigation state.

* `NavStackController` can be looked up at anytime with `NavStack.of(context)`
* `navStack.path` to change the global routing path
* `navStack.history` to access the history of path entries so far, you can modify and re-assign this list as needed
* `navStack.goBack()` to go back one level in the history
* `navStack.popUntil()`, `navStack.popMatching()`, `navStack.replacePath()` etc

Additionally, you can still make full use of the old `Navigator.push()` API, and `showDialog`, `showBottomSheet` etc, just be aware that none of these things will be reflected in the navigation path. For example, you can not deeplink directly to a dialog or a bottom sheet.

**Important:** Any calls to `Navigator.of(context).pop()` from within the `NavStack` children will be ignored. However, you can still use them from within Dialogs, BottomSheets or full-screen PageRoutes triggered with `Navigator.push()`. If you'd like to `pop()` something that is a descendant of `NavStack` just use `NavStack.of(context).goBack()`.

### MaterialApp.router()

`NavStack` creates a default `MaterialApp.router` internally, but you can provide a custom one if you need to modify the settings. Just use the `appBuilder` and pass along the provided `router` and `delegate` instances:
```
return NavStack(
appBuilder: (router, delegate) => MaterialApp.router(
routeInformationParser: delegate,
routerDelegate: router,
debugShowCheckedModeBanner: false,
),
entries: { ... }
```

**Note:** Do not wrap a second `MaterialApp` around `NavStack` or you will break all browser support and deeplinking.

## 🐞 Bugs/Requests

## Getting Started
If you encounter any problems please open an issue. If you feel the library is missing a feature, please raise a ticket on Github and we'll look into it. Pull request are welcome.

This project is a starting point for a Dart
[package](https://flutter.dev/developing-packages/),
a library module containing code that can be shared easily across
multiple Flutter or Dart projects.
## 📃 License

For help getting started with Flutter, view our
[online documentation](https://flutter.dev/docs), which offers tutorials,
samples, guidance on mobile development, and a full API reference.
MIT License
16 changes: 0 additions & 16 deletions example/README.md
Original file line number Diff line number Diff line change
@@ -1,16 +0,0 @@
# example

A new Flutter project.

## Getting Started

This project is a starting point for a Flutter application.

A few resources to get you started if this is your first Flutter project:

- [Lab: Write your first Flutter app](https://flutter.dev/docs/get-started/codelab)
- [Cookbook: Useful Flutter samples](https://flutter.dev/docs/cookbook)

For help getting started with Flutter, view our
[online documentation](https://flutter.dev/docs), which offers tutorials,
samples, guidance on mobile development, and a full API reference.
84 changes: 84 additions & 0 deletions example/lib/basic_demo.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import 'dart:async';

import 'package:flutter/material.dart';
import 'package:nav_stack/nav_stack.dart';

class BasicDemo extends StatelessWidget {
@override
Widget build(BuildContext _) {
/// Change this value to test the route guard. When false, you should not be able to access the /in routes.
bool isConnected = false;
return NavStack(
stackBuilder: (context, controller) {
return PathStack(
path: controller.path,
scaffoldBuilder: (_, stack) => _MyScaffold(stack),
routes: {
["/login", "/"]: LoginScreen().buildStackRoute(),
["/in/"]: PathStack(
path: controller.path,
basePath: "/in/",
routes: {
["profile/:id"]: ProfileScreen().buildStackRoute(),
["settings"]: SettingsScreen().buildStackRoute(),
},
).buildStackRoute(onBeforeEnter: (_) {
if (!isConnected) controller.redirect("/login", () => showAuthWarning(context));
return isConnected; // If we return false, the route will not be entered.
}),
},
);
},
);
}
}

void showAuthWarning(BuildContext context) {
showDialog(context: context, builder: (_) => AuthErrorDialog());
}

class AuthErrorDialog extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Center(
child: GestureDetector(
onTap: () => Navigator.of(context).pop(), // Pop still works for dialogs!
child: Container(width: 400, height: 300, color: Colors.purple, child: buildText("No soup for you!!"))));
}
}

class _MyScaffold extends StatelessWidget {
_MyScaffold(this.child);
final Widget child;
@override
Widget build(BuildContext context) {
final navStack = NavStack.of(context);
Widget buildBtn(String value) =>
Expanded(child: TextButton(child: Text(value), onPressed: () => navStack.path = value));
return Scaffold(
body: Column(
children: [
Row(children: [buildBtn("/in/profile/99"), buildBtn("/in/settings")]),
Flexible(child: child),
],
),
);
}
}

class LoginScreen extends StatelessWidget {
@override
Widget build(BuildContext context) => Center(child: Text("LoginScreen"));
}

class ProfileScreen extends StatelessWidget {
@override
Widget build(BuildContext context) => Center(child: Text("ProfileScreen"));
}

class SettingsScreen extends StatelessWidget {
@override
Widget build(BuildContext context) => Center(child: Text("SettingsScreen"));
}

Text buildText(String value) => Text(value, style: TextStyle(fontSize: 32));
60 changes: 60 additions & 0 deletions example/lib/hello_world_demo.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import 'package:example/basic_demo.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:nav_stack/nav_stack.dart';

class HelloWorldDemo extends StatefulWidget {
@override
_HelloWorldDemoState createState() => _HelloWorldDemoState();
}

class _HelloWorldDemoState extends State<HelloWorldDemo> {
GlobalKey<NavStackController> navKey = GlobalKey();
NavStackController get navStack => navKey.currentState!;
@override
void initState() {
super.initState();
RawKeyboard.instance.addListener((value) {
if (value is RawKeyDownEvent) {
if (value.logicalKey == LogicalKeyboardKey.digit1) NavStack.of(context).path = "/home";
if (value.logicalKey == LogicalKeyboardKey.digit2) NavStack.of(context).path = "/profile";
}
});
}

@override
Widget build(BuildContext context) {
return NavStack(
appBuilder: (router, delegate) => MaterialApp.router(
routeInformationParser: delegate,
routerDelegate: router,
debugShowCheckedModeBanner: false,
),
stackBuilder: (context, controller) => PathStack(
path: controller.path,
// Use scaffold builder to wrap all our pages in a tab-menu
scaffoldBuilder: (_, stack) => _TabScaffold(["/home", "/profile"], child: stack),
routes: {
// Alias "/" will catch the default path and send it to /home
["/home", "/"]: LoginScreen().buildStackRoute(),
["/profile"]: ProfileScreen().buildStackRoute(),
},
),
);
}
}

class _TabScaffold extends StatelessWidget {
const _TabScaffold(this.labels, {Key? key, required this.child}) : super(key: key);
final List<String> labels;
final Widget child;
@override
Widget build(BuildContext context) {
return Row(
children: [
Expanded(child: TextButton(child: Text("home"), onPressed: () => NavStack.of(context).path = "/home")),
Expanded(child: TextButton(child: Text("profile"), onPressed: () => NavStack.of(context).path = "/profile")),
],
);
}
}

0 comments on commit 63e4f8e

Please sign in to comment.