Git Product home page Git Product logo

flutter_page_router's Introduction

flutter_page_router

A front-end develop-oriented Routing Manage library that route params, context less programmatic navigation and global hooks.

Version

README in Chinese

Features:

  • Modular, component-based router configuration
  • Route params
  • View transition effects(dependent on flutter_page_transition
  • Context less programmatic navigation
  • Global hooks

Getting Started

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

dependencies:
  flutter_page_router: ^0.1.0

You can also depend on this package stored in my repository:

flutter_page_router:
  git:
    url: git://github.com/handoing/flutter_page_router.git

You should then run flutter packages upgrade.

Example:

FlutterPageRouter routerInit () {

  // Define some routes
  List routes = [
    {
      r.name: 'home', // Define route name
      r.component: (params) => HomePage() // Define route component
    },
    {
      r.name: 'other',
      r.transitionType: PageTransitionType.slideInRight, // Define route transition type
      r.component: (params) {
        return new FutureBuilder<String>(
          future: Future.delayed(Duration(milliseconds: 1000)),
          builder: (BuildContext context, AsyncSnapshot<String> snapshot) {
            switch (snapshot.connectionState) {
              case ConnectionState.done: return OtherPage(id: params['id']);
              case ConnectionState.none: return LoadingPage();
              case ConnectionState.waiting: return LoadingPage();
              default:
                return new Text('Error: ${snapshot.error}');
            }
          },
        );
      }
    },
    {
      r.type: RouterType.NotFound, // Define not found
      r.transitionHandle: (Curve curve, Animation<double> animation, Animation<double> secondaryAnimation, Widget child) {
        return new SlideTransition(
          position: new Tween<Offset>(
            begin: const Offset(1.0, 0.0),
            end: const Offset(0.0, 0.0),
          ).animate(CurvedAnimation(parent: animation, curve: curve)),
          child: child,
        );
      }, // Define route transition handle
      r.component: (params) => NotFoundPage()
    }
  ];

  // Create the router instance
  FlutterPageRouter router = FlutterPageRouter(
      routes: routes
  );

  // Define global before hooks
  router.globalBeforeRouteUpdate((name, params, child) {
    // Requests PV can be sent uniformly, or wrap a logic component and so on
    return Container(
      child: child,
    );
  });

  return router;
}

FlutterPageRouter router = routerInit(); // Initialization Router

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      initialRoute: 'home',
      navigatorKey: router.navKey, // Define navigator Key, used to internally capture context
      onGenerateRoute: router.generator // Define generator
    );
  }
}

void main() => runApp(MyApp());

PageRouterNav

flutter_page_router provides a PageRouterNav for developers to route navigation operations, it's internal use is still Navigator, call a variety of navigation operations provided by Navigator, such as push, pushNamed, pop, etc. The convenience of PageRouterNav is that there is no need to pass in the context. for example:

PageRouterNav.pushNamed('other');

// or

PageRouterNav.pushNamed('other', params: {
 'id': 666
});

Test

run test

flutter test

Test Driver

run driver test

cd example/
flutter drive --target=test_driver/app.dart

License

MIT

flutter_page_router's People

Contributors

handoing avatar hugoheneault avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

hugoheneault

flutter_page_router's Issues

Type NavigateService is already registered

run example error:

Launching lib/main.dart on iPhone Xʀ in debug mode...
Running Xcode build...
Xcode build done.                                            4.9s
Syncing files to device iPhone Xʀ...
flutter: ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
flutter: The following assertion was thrown building MyApp(dirty):
flutter: Type NavigateService is already registered
flutter: 'package:get_it/get_it.dart':
flutter: Failed assertion: line 53 pos 12: 'allowReassignment || !_factories.containsKey(T)'
flutter:
flutter: Either the assertion indicates an error in the framework itself, or we should provide substantially
flutter: more information in this error message to help you determine and fix the underlying cause.
flutter: In either case, please report this assertion by filing a bug on GitHub:
flutter:   https://github.com/flutter/flutter/issues/new?template=BUG.md
flutter:
flutter: When the exception was thrown, this was the stack:
flutter: #2      GetIt.registerSingleton (package:get_it/get_it.dart:53:12)
flutter: #3      new PageRouterNav (package:flutter_page_router/page_router_nav.dart:10:11)
flutter: #4      new FlutterPageRouter (package:flutter_page_router/flutter_page_router.dart:21:31)
flutter: #5      routerInit (package:example/router/index.dart:51:30)
flutter: #6      MyApp.build (package:example/main.dart:12:32)
flutter: #7      StatelessElement.build (package:flutter/src/widgets/framework.dart:3974:28)
flutter: #8      ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3924:15)
flutter: #9      Element.rebuild (package:flutter/src/widgets/framework.dart:3721:5)
flutter: #10     BuildOwner.buildScope (package:flutter/src/widgets/framework.dart:2340:33)
flutter: #11     _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding&PaintingBinding&SemanticsBinding&RendererBinding&WidgetsBinding.drawFrame (package:flutter/src/widgets/binding.dart:700:20)
flutter: #12     _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding&PaintingBinding&SemanticsBinding&RendererBinding._handlePersistentFrameCallback (package:flutter/src/rendering/binding.dart:285:5)
flutter: #13     _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding._invokeFrameCallback (package:flutter/src/scheduler/binding.dart:1016:15)
flutter: #14     _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding.handleDrawFrame (package:flutter/src/scheduler/binding.dart:958:9)
flutter: #15     _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding.scheduleWarmUpFrame.<anonymous closure> (package:flutter/src/scheduler/binding.dart:784:7)
flutter: #17     _Timer._runTimers (dart:isolate-patch/timer_impl.dart:382:19)
flutter: #18     _Timer._handleMessage (dart:isolate-patch/timer_impl.dart:416:5)
flutter: #19     _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:172:12)
flutter: (elided 3 frames from class _AssertionError and package dart:async-patch)
flutter: ════════════════════════════════════════════════════════════════════════════════════════════════════

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.