redux_navigator 0.2.1

  • Readme
  • Changelog
  • Example
  • Installing
  • 74

redux_navigator #

A Redux middleware for Navigator of Flutter. You can use default Navigator related Action, and also add custom behaviors.

This package is built to work with Redux.dart 3.0.0+.

  • navigatorMiddleware - A function that returns bundle of Navigator related middleware. You can also add more custom middleware by using NavigatorMiddlewareCallback.
  • NavigatorMiddlewareBuilder - A builder class for navigatorMiddleware. You can define custom behaviors with it.

Dart Version #

  • Dart 2.2.3+

Usage #

Demonstrate how to use redux_navigator.

  1. How to use default navigatorMiddleware.
  2. How to add custom behaviors with NavigatorMiddlewareBuilder.
import 'package:flutter/material.dart';
import 'package:flutter_redux/flutter_redux.dart';
import 'package:redux/redux.dart';
import 'package:redux_navigator/redux_navigator.dart';

class AppState {}

AppState appReducer(AppState state, dynamic action) {
  return state;
}

void main() {
  /// Initialize navigatorKey which is used for passing to
  /// [navigatorMiddleware] & [MaterialApp].
  final navigatorKey = GlobalKey<NavigatorState>();

  runApp(
    StoreProvider<AppState>(
      store: Store<AppState>(
        appReducer,
        initialState: AppState(),
        middleware: [
          /// Add navigatorMiddleware to middleware with [navigatorKey].
          ...navigatorMiddleware<AppState>(navigatorKey),
        ],
      ),
      child: MaterialApp(
        title: 'Flutter Demo',
        theme: ThemeData(
          primarySwatch: Colors.blue,
        ),

        /// Pass navigatorKey to [MaterialApp].
        navigatorKey: navigatorKey,
        home: const HomePage(),
      ),
    ),
  );
}

/// A page which presents list of infinity indexes.
class HomePage extends StatelessWidget {
  const HomePage({Key key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    /// Get Store by using StoreProvider. The store will be used dispatching
    /// Navigator Action.
    final store = StoreProvider.of<AppState>(context);
    return Scaffold(
      appBar: AppBar(
        title: const Text('redux_heaven_demo'),
      ),
      body: ListView.builder(
        itemBuilder: (context, index) => InkWell(
          /// Dispatch PushAction to navigate DetailPage.
          ///
          /// You can also use PushNamedAction with routeName parameter.
          onTap: () => store.dispatch(
            PushAction(
              MaterialPageRoute<void>(
                builder: (context) => DetailPage(index: index),
              ),
            ),
          ),
          child: Container(
            padding: const EdgeInsets.all(24),
            decoration: BoxDecoration(
              border: Border(
                bottom: BorderSide(
                  color: Colors.black.withOpacity(0.3),
                ),
              ),
            ),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: <Widget>[
                Text(
                  'Item: $index',
                  style: Theme.of(context).textTheme.button,
                ),
                const Icon(Icons.navigate_next)
              ],
            ),
          ),
        ),
      ),
    );
  }
}

/// Destination of [PushAction] above.
class DetailPage extends StatelessWidget {
  const DetailPage({
    @required this.index,
    Key key,
  }) : super(key: key);

  final int index;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Item Detail Page : $index'),
      ),
      body: Center(
        child: Text(
          'Item:$index',
          style: Theme.of(context).textTheme.title,
        ),
      ),
    );
  }
}

2. How to define custom behaviors. #

navigatorMiddleware<AppState>(
  navigatorKey,
  customBuilders: [
    /// You can add custom [Navigator] related behaviors by
    /// specifying [NavigatorMiddlewareBuilder]s list.
    NavigatorMiddlewareBuilder<AppState, ShowAlertDialogAction>(
      callback: (navigatorKey, store, action, next) {
        showDialog<void>(
          context: navigatorKey.currentState.overlay.context,
          builder: (context) {
            return const AlertDialog(
              content: Text('Addtional Middleware'),
            );
          },
        );
      },
    ),
  ],
),

[0.2.1] #

  • update dependencies for redux.dart-v4.0.0

[0.2.0] #

  • add custom builders.

[0.1.1] #

  • BUG FIX.

[0.1.0] #

  • create NavigatorMiddlewareCallback class & add Document comment, refactor.

[0.0.1] #

  • bundle private utils for navigator with Redux.

example/lib/main.dart

import 'package:flutter/material.dart';
import 'package:flutter_redux/flutter_redux.dart';
import 'package:redux/redux.dart';
import 'package:redux_navigator/redux_navigator.dart';

class AppState {
  const AppState();
}

AppState appReducer(AppState state, dynamic action) {
  return state;
}

/// A custom [Navigator] related action.
class ShowAlertDialogAction {}

void main() {
  /// Initialize navigatorKey which is used for passing to
  /// [navigatorMiddleware] & [MaterialApp].
  final navigatorKey = GlobalKey<NavigatorState>();

  runApp(
    StoreProvider<AppState>(
      store: Store<AppState>(
        appReducer,
        initialState: const AppState(),
        middleware: [
          /// Add navigatorMiddleware to middleware with [navigatorKey].
          ...navigatorMiddleware<AppState>(
            navigatorKey,
            customBuilders: [
              /// You can add custom [Navigator] related behaviors by
              /// specifying [NavigatorMiddlewareBuilder]s list.
              NavigatorMiddlewareBuilder<AppState, ShowAlertDialogAction>(
                callback: (navigatorKey, store, action, next) {
                  showDialog<void>(
                    context: navigatorKey.currentState.overlay.context,
                    builder: (context) {
                      return const AlertDialog(
                        content: Text('Addtional Middleware'),
                      );
                    },
                  );
                },
              ),
            ],
          ),
        ],
      ),
      child: MaterialApp(
        title: 'Flutter Demo',
        theme: ThemeData(
          primarySwatch: Colors.blue,
        ),

        /// Pass navigatorKey to [MaterialApp].
        navigatorKey: navigatorKey,
        home: const HomePage(),
      ),
    ),
  );
}

/// A page which presents list of infinity indexes.
class HomePage extends StatelessWidget {
  const HomePage({Key key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    /// Get Store by using StoreProvider. The store will be used dispatching
    /// Action of Navigator.
    final store = StoreProvider.of<AppState>(context);
    return Scaffold(
      appBar: AppBar(
        title: const Text('redux_heaven_demo'),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          store.dispatch(ShowAlertDialogAction());
        },
        child: Icon(Icons.check),
      ),
      body: ListView.builder(
        itemBuilder: (context, index) => InkWell(
          /// Dispatch PushAction to navigate DetailPage.
          ///
          /// You can also use PushNamedAction with routeName parameter.
          onTap: () => store.dispatch(
            PushAction(
              MaterialPageRoute<void>(
                builder: (context) => DetailPage(index: index),
              ),
            ),
          ),
          child: Container(
            padding: const EdgeInsets.all(24),
            decoration: BoxDecoration(
              border: Border(
                bottom: BorderSide(
                  color: Colors.black.withOpacity(0.3),
                ),
              ),
            ),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: <Widget>[
                Text(
                  'Item: $index',
                  style: Theme.of(context).textTheme.button,
                ),
                const Icon(Icons.navigate_next)
              ],
            ),
          ),
        ),
      ),
    );
  }
}

/// Destination of [PushAction] above.
class DetailPage extends StatelessWidget {
  const DetailPage({
    @required this.index,
    Key key,
  }) : super(key: key);

  final int index;

  @override
  Widget build(BuildContext context) {
    final store = StoreProvider.of<AppState>(context);
    return Scaffold(
      appBar: AppBar(
        title: Text('Item Detail Page : $index'),
      ),
      body: Center(
        child: RaisedButton(
          onPressed: () => store.dispatch(const PopAction()),
          child: const Text('POP'),
        ),
      ),
    );
  }
}

Use this package as a library

1. Depend on it

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


dependencies:
  redux_navigator: ^0.2.1

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:redux_navigator/redux_navigator.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
47
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]
74
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.9+1
  • Flutter: 1.17.3

Analysis suggestions

Package not compatible with SDK dart

because of import path [redux_navigator]

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.2.3 <3.0.0
flutter 0.0.0
redux ^4.0.0 4.0.0
redux_heaven ^0.1.0 0.1.0
Transitive dependencies
collection 1.14.12 1.14.13
meta 1.1.8 1.2.0
sky_engine 0.0.99
typed_data 1.1.6 1.2.0
vector_math 2.0.8
Dev dependencies
flutter_test
pedantic_mono ^1.4.0