Skip to content

Commit

Permalink
Add simple_nav
Browse files Browse the repository at this point in the history
  • Loading branch information
pedrox-hs committed Jan 15, 2023
1 parent 3a3475e commit b423ca1
Show file tree
Hide file tree
Showing 10 changed files with 874 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Flutter packages candidate to be published and frequently used in projects maint
- [flutter_toolkit](flutter_toolkit)
- [http_client_plus](http_client_plus)
- [logger_plus](logger_plus)
- [simple_nav](simple_nav)
- [state_action_bloc](state_action_bloc)

> All PRs and names suggestions are welcome :)
30 changes: 30 additions & 0 deletions simple_nav/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Miscellaneous
*.class
*.log
*.pyc
*.swp
.DS_Store
.atom/
.buildlog/
.history
.svn/
migrate_working_dir/

# IntelliJ related
*.iml
*.ipr
*.iws
.idea/

# The .vscode folder contains launch configuration and tasks you configure in
# VS Code which you may wish to be included in version control, so this line
# is commented out by default.
#.vscode/

# Flutter/Dart/Pub related
# Libraries should not include pubspec.lock, per https://dart.dev/guides/libraries/private-files#pubspeclock.
/pubspec.lock
**/doc/api/
.dart_tool/
.packages
build/
10 changes: 10 additions & 0 deletions simple_nav/.metadata
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# This file tracks properties of this Flutter project.
# Used by Flutter tool to assess capabilities and perform upgrades etc.
#
# This file should be version controlled and should not be manually edited.

version:
revision: 135454af32477f815a7525073027a3ff9eff1bfd
channel: stable

project_type: package
3 changes: 3 additions & 0 deletions simple_nav/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## 0.1.0

* Initial pre-release
674 changes: 674 additions & 0 deletions simple_nav/LICENSE

Large diffs are not rendered by default.

30 changes: 30 additions & 0 deletions simple_nav/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
A simple navigation resolver.

> this library is currently in WIP
## Features

TODO: List what your package can do. Maybe include images, gifs, or videos.

## Getting started

Add dependency to project:

```bash
flutter pub add simple_nav --git-url=https://github.com/pedrox-hs/flutter_packages --git-path=simple_nav
```

## Usage

TODO: Include short and useful examples for package users. Add longer examples
to `/example` folder.

```dart
const like = 'sample';
```

## Additional information

TODO: Tell users more about the package: where to find more information, how to
contribute to the package, how to file issues, what response they can expect
from the package authors, and more.
1 change: 1 addition & 0 deletions simple_nav/analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include: ../flutter_lints.yaml
3 changes: 3 additions & 0 deletions simple_nav/lib/simple_nav.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
library simple_nav;

export 'src/route_resolver.dart';
67 changes: 67 additions & 0 deletions simple_nav/lib/src/route_resolver.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import 'package:flutter/material.dart';

typedef PageBuilder = Widget Function(
BuildContext context,
RouteExtras extras,
);

class RouteResolver {
RouteResolver(this.routes);

final Map<String, PageBuilder> routes;

Route<dynamic>? call(RouteSettings settings) {
final result = _findRoute(settings.name!);
if (result == null) return null;

final extras = RouteExtras(result.arguments, settings.arguments);
return MaterialPageRoute(
settings: settings,
builder: (context) => result.pageBuilder(context, extras),
);
}

PageBuilderResult? _findRoute(String routeName) {
final uri = Uri.parse(routeName);

if (routes.containsKey(uri.path)) {
return PageBuilderResult(routes[uri.path]!, {...uri.queryParameters});
}

for (final String route in routes.keys) {
if (!route.contains(':')) continue;

final expr = route.replaceAllMapped(
RegExp(':([^/]+)'),
(m) => '(?<${m[1]}>[^/]+)',
);
final match = RegExp(expr).firstMatch(uri.path);
if (match == null) continue;

final arguments = {
for (final key in match.groupNames) key: match.namedGroup(key)
};

return PageBuilderResult(
routes[route]!,
{...uri.queryParameters, ...arguments},
);
}

return null;
}
}

class PageBuilderResult {
PageBuilderResult(this.pageBuilder, [this.arguments = const {}]);

final PageBuilder pageBuilder;
final Map<String, dynamic> arguments;
}

class RouteExtras {
RouteExtras(this.arguments, this.data);

final Map<String, dynamic> arguments;
final dynamic data;
}
55 changes: 55 additions & 0 deletions simple_nav/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
name: simple_nav
description: A new Flutter package project.
version: 0.1.0
homepage: https://github.com/pedrox-hs/flutter_packages
repository: https://github.com/pedrox-hs/flutter_packages/tree/main/simple_nav

environment:
sdk: '>=2.12.0 <3.0.0'
flutter: ">=1.17.0"

dependencies:
flutter:
sdk: flutter

dev_dependencies:
flutter_test:
sdk: flutter
flutter_lints: ^1.0.0

# For information on the generic Dart part of this file, see the
# following page: https://dart.dev/tools/pub/pubspec

# The following section is specific to Flutter packages.
flutter:

# To add assets to your package, add an assets section, like this:
# assets:
# - images/a_dot_burr.jpeg
# - images/a_dot_ham.jpeg
#
# For details regarding assets in packages, see
# https://flutter.dev/assets-and-images/#from-packages
#
# An image asset can refer to one or more resolution-specific "variants", see
# https://flutter.dev/assets-and-images/#resolution-aware

# To add custom fonts to your package, add a fonts section here,
# in this "flutter" section. Each entry in this list should have a
# "family" key with the font family name, and a "fonts" key with a
# list giving the asset and other descriptors for the font. For
# example:
# fonts:
# - family: Schyler
# fonts:
# - asset: fonts/Schyler-Regular.ttf
# - asset: fonts/Schyler-Italic.ttf
# style: italic
# - family: Trajan Pro
# fonts:
# - asset: fonts/TrajanPro.ttf
# - asset: fonts/TrajanPro_Bold.ttf
# weight: 700
#
# For details regarding fonts in packages, see
# https://flutter.dev/custom-fonts/#from-packages

0 comments on commit b423ca1

Please sign in to comment.