Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
xdd666t opened this issue May 20, 2023 · 1 comment
Closed

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

xdd666t opened this issue May 20, 2023 · 1 comment

Comments

@xdd666t
Copy link
Member

xdd666t commented May 20, 2023

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(),
    );
  }
}
@xdd666t xdd666t pinned this issue May 20, 2023
@xdd666t xdd666t changed the title flutter_smart_dialog初始化及其配置 SmartDialog初始化及其配置详解 May 20, 2023
@xdd666t xdd666t changed the title SmartDialog初始化及其配置详解 SmartDialog初始化及其配置(必看) May 20, 2023
@xdd666t xdd666t changed the title SmartDialog初始化及其配置(必看) SmartDialog在各类路由框架中如何初始化 May 20, 2023
@xdd666t xdd666t closed this as completed May 20, 2023
@cjcj125125
Copy link

看了上述初始化发现没有fluent_ui库的初始化方法,我最近使用fluent_ui库开发桌面程序,怎么初始化smartdialog呢?因为main.dart里面他需要用FluentApp.router,而不是MaterialApp,希望大佬指点,感激不尽

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants