simple_navigation 1.0.30

  • Readme
  • Changelog
  • Example
  • Installing
  • 53

simple_navigation #

Simple named route navigation that unofficially addresses Flutter issue 11655 ("build needlessly called for non-current screens lower on stack").

I consider issue 11655 to be the single biggest problem with the Flutter widget build mechanism; it really bothers me so I felt compelled to roll my own solution.

THIS PACKAGE ISN'T FOR EVERYONE. I prefer the navigation approach implemented herein but it's non-standard.

Warning: This package is an active proof of concept--under active development--and may change drastically at any time.

Simplifications #

  • for simplicity of development, navigation is static
  • don't need to pass context
  • class named 'Nav' instead of 'Navigation' because it's shorter

Limitations #

  • since static, can't have multiple instances
  • currently doesn't work with showDialog() return values

Features #

  • backButtonCaption with callback function

TODO #

  • use generics for arguments: either Object (standard) or Map (my preference)
  • force redraw entire tree (not sure if possible)
  • pop all the way out of application (configure yes/no)
  • test: hot reload doesn't erase stack
  • solve: object vs. named route problem
  • pass in trace flag to constructor
  • return values? (friends showDialog... pop(return))

Pull Request #

Pull requests are welcome!

Changelog #

Version 1.0.30 (2020-03-10) #

  • improved 404 route not found dump

Version 1.0.28 (2020-03-07) #

  • features and fixed bugs

Version 1.0.27 (2020-03-06) #

  • features and fixed bugs

Version 1.0.19 (2020-03-06) #

  • publish to pub.dev

example/lib/main.dart

// Copyright 2020 Peter Alvin. All rights reserved.

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

int counter = 0;

class Scrn extends StatefulWidget {
  Scrn(this.name) {
    print("$name called");
  }

  final String name;

  @override
  _ScrnState createState() => _ScrnState();
}

class _ScrnState extends State<Scrn> {
  @override
  Widget build(BuildContext context) {
    String xtra = '';
//    if (ModalRoute.of(context).settings.arguments != null) {
//      xtra = ': ARGS=${ModalRoute.of(context).settings.arguments}';
//    }

    if (Nav.hasArgs) {
      xtra = ': ARGS=${Nav.args}';
    }

    print("scrn build: ${widget.name}$xtra");

    return Column(
      children: <Widget>[
        RaisedButton(
            child: Text(Nav.backButtonCaption),
            onPressed: () {
              Nav.pop();
            }),
        Text('${Nav.stack}', style: TextStyle(fontSize: 20)),
        Text('${widget.name}', style: TextStyle(fontSize: 30)),
        Text('$xtra', style: TextStyle(fontSize: 15)),
        RaisedButton(
            child: Text('pop'),
            onPressed: () {
              Nav.pop();
            }),
        RaisedButton(
            child: Text('popAll'),
            onPressed: () {
              Nav.popAll();
            }),
        RaisedButton(
            child: Text('push a'),
            onPressed: () {
//              print("pressed a");
              Nav.push('/a', {'counter': ++counter});
            }),
        RaisedButton(
            child: Text('push b'),
            onPressed: () {
//              print("pressed b");
              Nav.push('/b');
            }),
        RaisedButton(
            child: Text('repl a'),
            onPressed: () {
//              print("pressed a");
              Nav.repl('/a');
            }),
        RaisedButton(
            child: Text('repl b'),
            onPressed: () {
//              print("pressed b");
              Nav.repl('/b');
            }),
        RaisedButton(
            child: Text('push deanna'),
            onPressed: () {
              Nav.push('/deanna');
            }),
      ],
    );
  }

  @override
  void dispose() {
    super.dispose();
  }

  @override
  void initState() {
    super.initState();
  }
}

void main() {
  WidgetsFlutterBinding.ensureInitialized();
  print("routes");
  Nav.routes = {
    '/': (context) => Scrn('home'),
    '/a': (context) => Scrn('a'),
    '/b': (context) => Scrn('b'),
  };
  runApp(new MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      home: Scaffold(
        appBar: AppBar(),
        body: Nav(
//        child: Scaffold(body: Center(child: Text('search me for search'))),
            backButtonCaptionCallback: (String route) {
          String s = route.toUpperCase().replaceAll('/', '');
          return s.length == 0 ? 'home' : s;
        }),
      ),
    );
  }
}

Use this package as a library

1. Depend on it

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


dependencies:
  simple_navigation: ^1.0.30

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:simple_navigation/simple_navigation.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
6
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
53
Learn more about scoring.

We analyzed this package on Jul 9, 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 [simple_navigation] that is in a package requiring null.

Health suggestions

Format lib/simple_navigation.dart.

Run flutter format to format lib/simple_navigation.dart.

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 1.2.1
sky_engine 0.0.99
typed_data 1.1.6 1.2.0
vector_math 2.0.8 2.1.0-nullsafety
Dev dependencies
flutter_test