no_context_navigation 1.0.2

  • Readme
  • Changelog
  • Example
  • Installing
  • 84

No Context Navigation #

No Context Navigation Package - pub.dev

Instead of using Navigator.of(context).pushNamed or some other method like pop() or pushReplacementNamed() USING context. With this package you don't need to anymore.

This service use a GlobalKey of type NavigatorState which can be used across the entire applicaton.

Now, you only need to this:

navService.pushNamed('/detail_screen', args: 'From Home Screen');

But first, add the navigationKey to your MaterialApp's navigationKey property:

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      //Over here
      navigatorKey: NavigationService.navigationKey,
      onGenerateRoute: (RouteSettings settings) {
        switch (settings.name) {
          case '/':
            return MaterialPageRoute(builder: (context) => HomeScreen());
          case '/detail_screen':
            return MaterialPageRoute(
                builder: (context) =>
                    DetailScreen(message: settings.arguments));
          default:
            return null;
        }
      },
    );
  }
}

Use navService object to be able to acess those methods:

import 'package:flutter/material.dart';

final NavigationService navService = NavigationService();

class NavigationService<T, U> {
  static GlobalKey<NavigatorState> navigationKey = GlobalKey<NavigatorState>();

  Future<T> pushNamed(String routeName, {Object args}) async {
    return await navigationKey.currentState.pushNamed<T>(
      routeName,
      arguments: args,
    );
  }

  Future<T> push(Route<T> route) async {
    return await navigationKey.currentState.push<T>(route);
  }

  Future<T> pushReplacementNamed(String routeName, {Object args}) async {
    return await navigationKey.currentState.pushReplacementNamed<T, U>(
      routeName,
      arguments: args,
    );
  }

  Future<T> pushNamedAndRemoveUntil(String routeName, {Object args}) async {
    return await navigationKey.currentState.pushNamedAndRemoveUntil<T>(
      routeName,
      (Route<dynamic> route) => false,
      arguments: args,
    );
  }

  Future<bool> maybePop([Object args]) async {
    return await navigationKey.currentState.maybePop<bool>(args);
  }

  bool canPop() => navigationKey.currentState.canPop();

  bool goBack({Object result}) => navigationKey.currentState.pop<bool>(result);
}

Hope you enjoy!

[1.0.2] - 06/05/2020 #

  • Fixed error on goBack function

[1.0.0] - 19/03/2020 #

  • Updated to 1.0.0

[0.0.3] - 26/02/2020. #

  • Updated README.md and added a MIT license.

[0.0.1] - 26/02/2020. #

  • Initial release.

example/main.dart

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

import 'src/detail_screen/detail_screen.dart';
import 'src/home_screen/home_screen.dart';

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      navigatorKey: NavigationService.navigationKey,
      onGenerateRoute: (RouteSettings settings) {
        switch (settings.name) {
          case '/':
            return MaterialPageRoute(builder: (context) => HomeScreen());
          case '/detail_screen':
            return MaterialPageRoute(
                builder: (context) =>
                    DetailScreen(message: settings.arguments));
          default:
            return null;
        }
      },
    );
  }
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  no_context_navigation: ^1.0.2

2. Install it

You can install packages from the command line:

with Flutter:


$ flutter pub get

Alternatively, your editor might support flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:no_context_navigation/no_context_navigation.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
68
Health:
Code health derived from static analysis. [more]
99
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
84
Learn more about scoring.

We analyzed this package on Jul 2, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.8.4
  • pana: 0.13.13
  • Flutter: 1.17.5

Analysis suggestions

Package not compatible with SDK dart

because of import path [no_context_navigation] that is in a package requiring null.

Health issues and suggestions

Document public APIs. (-1 points)

13 out of 13 API elements have no dartdoc comment.Providing good documentation for libraries, classes, functions, and other API elements improves code readability and helps developers find and use your API.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.1.0 <3.0.0
flutter 0.0.0
Transitive dependencies
collection 1.14.12 1.14.13
meta 1.1.8
sky_engine 0.0.99
typed_data 1.1.6 1.2.0
vector_math 2.0.8
Dev dependencies
flutter_test