Skip to content

ouxiaoyong/route_life

Repository files navigation

if you wan to listen route lifecycle in widget, this is a way.

Usage

1.import route_life.dart

import 'package:route_life/route_life.dart';

2.set navigatorObservers

RouteLifeObserver routeLifeObserver = RouteLifeObserver();

MaterialApp(
      navigatorObservers: [
        routeLifeObserver
      ],
    );
  1. with RouteLifeMixin or AppLifeMixin
class _GamePageState extends State<GamePage> with RouteLifeMixin,AppLifeMixin{
  @override
  void initState() {
    super.initState();
    _playGamePageMusic();
  }

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

  @override
  void onRoutePause(Route nextRoute) {
    _pauseGamePageMusic();
  }

  @override
  void onRouteResume(Route nextRoute) {
    _resumeGamePageMusic();
  }

  @override
  void onAppLifeChanged(bool resume) {
    if(!isRouteShowing){
      return;
    }
    if(resume){
      _resumeGamePageMusic();
    }else{
      _pauseGamePageMusic();
    }
  }
}

About

a flutter package for listen route lifecycle

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages