Skip to content

SmartDialog在各类路由框架中如何初始化 #131

Closed
@xdd666t

Description

@xdd666t

MaterialApp场景初始化

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: HomePage(),
      // here
      navigatorObservers: [FlutterSmartDialog.observer],
      // here
      builder: FlutterSmartDialog.init(),
    );
  }
}

配置全局自定义样式

  • SmartDialog自定义Loading或Toast是非常简单的;但是,使用的时候,可能会让你觉得有一点麻烦,举个例子
    • 使用自定义Loading:SmartDialog.showLoading(builder: (_) => CustomLoadingWidget);
    • 我们想要的使用效果,肯定是这样的:SmartDialog.showLoading();
  • 针对上面的考虑,我在入口处增加了,可设置自定义默认Loading,Toast样式,showNotify各类样式的功能
void main() => runApp(const MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: HomePage(),
      // here
      navigatorObservers: [FlutterSmartDialog.observer],
      // here
      builder: FlutterSmartDialog.init(
        //default toast widget
        toastBuilder: (String msg) => CustomToastWidget(msg: msg),
        //default loading widget
        loadingBuilder: (String msg) => CustomLoadingWidget(msg: msg),
        //default notify widget
        notifyStyle: FlutterSmartNotifyStyle(
          successBuilder: (String msg) => CustomSuccessWidget(msg: msg),
          failureBuilder: (String msg) => CustomFailureWidget(msg: msg),
          warningBuilder: (String msg) => CustomWarningWidget(msg: msg),
          alertBuilder: (String msg) => CustomAlertWidget(msg: msg),
          errorBuilder: (String msg) => CustomErrorWidget(msg: msg),
        ),
      ),
    );
  }
}

各类场景初始化

flutter_boost场景

  • 注意,跳转flutter路由,必须使用flutter_boost这套,路由需要写在routerMap中
void main() {
  MyFlutterBinding();
  runApp(const MyApp());
}

class MyFlutterBinding extends WidgetsFlutterBinding with BoostFlutterBinding {}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  static Map<String, FlutterBoostRouteFactory> routerMap = {
    '/page/test': (settings, uniqueId) {
      return MaterialPageRoute(settings: settings, builder: (_) => const TestPage());
    },
  };

  Route<dynamic>? _routeFactory(RouteSettings settings, String? uniqueId) {
    var func = routerMap[settings.name!];
    // here
    return FlutterSmartDialog.boostMonitor(func?.call(settings, uniqueId));
  }

  @override
  Widget build(BuildContext context) {
    // here
    final initSmartDialog = FlutterSmartDialog.init();
    final boost = BoostLifecycleBinding.instance;
    if (!boost.navigatorObserverList.contains(FlutterSmartDialog.observer)) {
      boost.addNavigatorObserver(FlutterSmartDialog.observer);
    }
    return FlutterBoostApp(
      _routeFactory,
      appBuilder: (Widget home) {
        return MaterialApp(
          home: home,
          debugShowCheckedModeBanner: true,
          //here
          builder: (context, child) => initSmartDialog(context, home),
        );
      },
    );
  }
}

MaterialApp.router场景

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

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp.router(
      routeInformationParser: MyRouteParser(),
      routerDelegate: MyRouteDelegate(),
      // here
      builder: FlutterSmartDialog.init(),
    );
  }
}

class MyRouteDelegate extends RouterDelegate<String>
    with PopNavigatorRouterDelegateMixin<String>, ChangeNotifier {
  @override
  Widget build(BuildContext context) {
    return Navigator(
      key: navigatorKey,
      onPopPage: _onPopPage,
      // here
      observers: [FlutterSmartDialog.observer],
      pages: List.generate(_stack.length, (index) {
        return MaterialPage(child: DemoPage(title: 'Route:${_stack[index]}'));
      }),
    );
  }

  List<String> _stack = [];

  @override
  GlobalKey<NavigatorState> navigatorKey = GlobalKey<NavigatorState>();

  @override
  String? get currentConfiguration => _stack.isNotEmpty ? _stack.last : null;

  @override
  Future<void> setNewRoutePath(String configuration) {
    _stack = [configuration];
    return SynchronousFuture<void>(null);
  }

  static MyRouteDelegate of(BuildContext context) {
    return Router.of(context).routerDelegate as MyRouteDelegate;
  }

  void push(String newRoute) {
    _stack.add(newRoute);
    notifyListeners();
  }

  void remove(String routeName) {
    _stack.remove(routeName);
    notifyListeners();
  }

  bool _onPopPage(Route<dynamic> route, dynamic result) {
    if (_stack.isNotEmpty) {
      if (_stack.last == route.settings.name) {
        _stack.remove(route.settings.name);
        notifyListeners();
      }
    }
    return route.didPop(result);
  }
}

class MyRouteParser extends RouteInformationParser<String> {
  @override
  Future<String> parseRouteInformation(RouteInformation routeInformation) {
    return SynchronousFuture(routeInformation.location ?? '');
  }

  @override
  RouteInformation restoreRouteInformation(String configuration) {
    return RouteInformation(location: configuration);
  }
}

class DemoPage extends StatelessWidget {
  const DemoPage({Key? key, required this.title}) : super(key: key);
  final String title;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text(title)),
      body: Center(
        child: ElevatedButton(
          child: const Text('Click Me'),
          onPressed: () => _showDialog(context),
        ),
      ),
    );
  }

  void _showDialog(BuildContext context) {
    SmartDialog.show(builder: (_) {
      return Container(
        height: 150,
        width: 150,
        color: Colors.white,
        alignment: Alignment.center,
        child: ElevatedButton(
          child: const Text('to next page'),
          onPressed: () => MyRouteDelegate.of(context).push('Route$hashCode'),
        ),
      );
    });
  }
}

auto_route场景

void main() => runApp(const App());

class App extends StatelessWidget {
  const App({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp.router(
      // here
      builder: FlutterSmartDialog.init(),
      // here
      routerDelegate: _appRouter.delegate(
        navigatorObservers: [FlutterSmartDialog.observer],
      ),
      routeInformationParser: _appRouter.defaultRouteParser(),
    );
  }
}

go_router场景

void main() => runApp(const App());

class App extends StatelessWidget {
  const App({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp.router(
      // here
      builder: FlutterSmartDialog.init(),
      routerConfig: GoRouter(
        initialLocation: '/'
        // here
        observers: [FlutterSmartDialog.observer],
        routes: <GoRoute>[],
      ),
    );
  }
}

routemaster场景

void main() => runApp(const App());

class App extends StatelessWidget {
  const App({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp.router(
      // here
      builder: FlutterSmartDialog.init(),
      routerDelegate: RoutemasterDelegate(
        // here
        observers: [FlutterSmartDialog.observer],
        routesBuilder: (context) => RouteMap(routes: {
          '/': (_) => MaterialPage(child: Container()),
        }),
      ),
      routeInformationParser: const RoutemasterParser(),
    );
  }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions