Skip to content

Commit

Permalink
add -> 路由管理 auto_route
Browse files Browse the repository at this point in the history
  • Loading branch information
ducafecat committed Apr 16, 2020
1 parent e6759b8 commit e5dab4e
Show file tree
Hide file tree
Showing 13 changed files with 513 additions and 48 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ Tools • Dart 2.7.0
iconfont_builder --from ./assets/fonts --to ./lib/common/utils/iconfont.dart
```

## 生成路由

```
flutter packages pub run build_runner build
```

## 代码仓

https://github.com/ducafecat/flutter_learn_news
Expand Down
16 changes: 16 additions & 0 deletions lib/common/router/auth_grard.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import 'package:auto_route/auto_route.dart';
import 'package:flutter_ducafecat_news/common/router/router.gr.dart';
import 'package:flutter_ducafecat_news/common/utils/utils.dart';

class AuthGuard extends RouteGuard {
@override
Future<bool> canNavigate(ExtendedNavigatorState navigator, String routeName,
Object arguments) async {
var isAuth = await isAuthenticated();
if (isAuth == false) {
ExtendedNavigator.rootNavigator.pushNamed(Routes.signInPageRoute);
}

return isAuth;
}
}
34 changes: 34 additions & 0 deletions lib/common/router/router.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import 'package:auto_route/auto_route_annotations.dart';
import 'package:flutter/material.dart';
import 'package:flutter_ducafecat_news/common/router/auth_grard.dart';
import 'package:flutter_ducafecat_news/pages/application/application.dart';
import 'package:flutter_ducafecat_news/pages/details/details.dart';
import 'package:flutter_ducafecat_news/pages/index/index.dart';
import 'package:flutter_ducafecat_news/pages/sign_in/sign_in.dart';
import 'package:flutter_ducafecat_news/pages/sign_up/sign_up.dart';
import 'package:flutter_ducafecat_news/pages/welcome/welcome.dart';

Widget zoomInTransition(BuildContext context, Animation<double> animation,
Animation<double> secondaryAnimation, Widget child) {
// you get an animation object and a widget
// make your own transition
return ScaleTransition(scale: animation, child: child);
}

@MaterialAutoRouter()
class $AppRouter {
@initial
IndexPage indexPageRoute;

WelcomePage welcomePageRoute;

SignInPage signInPageRoute;

SignUpPage signUpPageRoute;

@GuardedBy([AuthGuard])
ApplicationPage applicationPageRoute;

@GuardedBy([AuthGuard])
DetailsPage detailsPageRoute;
}
144 changes: 144 additions & 0 deletions lib/common/router/router.gr.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions lib/common/utils/authentication.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import 'dart:async';

import 'package:auto_route/auto_route.dart';
import 'package:flutter/material.dart';
import 'package:flutter_ducafecat_news/common/router/router.gr.dart';
import 'package:flutter_ducafecat_news/common/utils/utils.dart';
import 'package:flutter_ducafecat_news/common/values/values.dart';
import 'package:flutter_ducafecat_news/global.dart';
Expand All @@ -20,6 +22,6 @@ Future deleteAuthentication() async {
/// 重新登录
Future goLoginPage(BuildContext context) async {
await deleteAuthentication();
Navigator.pushNamedAndRemoveUntil(
context, "/sign-in", (Route<dynamic> route) => false);
ExtendedNavigator.rootNavigator.pushNamedAndRemoveUntil(
Routes.signInPageRoute, (Route<dynamic> route) => false);
}
2 changes: 1 addition & 1 deletion lib/global.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class Global {
/// 是否离线登录
static bool isOfflineLogin = false;

/// 应用状态
/// 应用状态,
static AppState appState = AppState();

/// 是否 release
Expand Down
13 changes: 8 additions & 5 deletions lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import 'package:auto_route/auto_route.dart';
import 'package:flutter/material.dart';
import 'package:flutter_ducafecat_news/common/provider/provider.dart';
import 'package:flutter_ducafecat_news/common/router/auth_grard.dart';
import 'package:flutter_ducafecat_news/common/router/router.gr.dart';
import 'package:flutter_ducafecat_news/global.dart';
import 'package:flutter_ducafecat_news/pages/index/index.dart';
import 'package:flutter_ducafecat_news/routes.dart';
import 'package:provider/provider.dart';

// void main() => runApp(MyApp());
void main() => Global.init().then((e) => runApp(
MultiProvider(
providers: [
Expand All @@ -31,9 +31,12 @@ class MyApp extends StatelessWidget {
Widget build(BuildContext context) {
return MaterialApp(
title: 'ducafecat.tech',
home: IndexPage(),
routes: staticRoutes,
debugShowCheckedModeBanner: false,
builder: ExtendedNavigator<AppRouter>(
initialRoute: Routes.indexPageRoute,
router: AppRouter(),
guards: [AuthGuard()],
),
);
}
}
15 changes: 4 additions & 11 deletions lib/pages/sign_in/sign_in.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import 'package:auto_route/auto_route.dart';
import 'package:flutter/material.dart';
import 'package:flutter_ducafecat_news/common/apis/apis.dart';
import 'package:flutter_ducafecat_news/common/entitys/entitys.dart';
import 'package:flutter_ducafecat_news/common/router/router.gr.dart';
import 'package:flutter_ducafecat_news/common/utils/utils.dart';
import 'package:flutter_ducafecat_news/common/values/values.dart';
import 'package:flutter_ducafecat_news/common/widgets/widgets.dart';
Expand All @@ -21,10 +23,7 @@ class _SignInPageState extends State<SignInPage> {

// 跳转 注册界面
_handleNavSignUp() {
Navigator.pushNamed(
context,
"/sign-up",
);
ExtendedNavigator.rootNavigator.pushNamed(Routes.signUpPageRoute);
}

// 执行登录操作
Expand All @@ -49,13 +48,7 @@ class _SignInPageState extends State<SignInPage> {
);
Global.saveProfile(userProfile);

// List<NewsIndexResponseEntity> newsList = await NewsAPI.index();
// print(newsList.length);

Navigator.pushNamed(
context,
"/app",
);
ExtendedNavigator.rootNavigator.pushNamed(Routes.applicationPageRoute);
}

///////////////////////////////
Expand Down
7 changes: 3 additions & 4 deletions lib/pages/welcome/welcome.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import 'package:auto_route/auto_route.dart';
import 'package:flutter/material.dart';
import 'package:flutter_ducafecat_news/common/router/router.gr.dart';
import 'package:flutter_ducafecat_news/common/utils/utils.dart';
import 'package:flutter_ducafecat_news/common/values/values.dart';

Expand Down Expand Up @@ -93,10 +95,7 @@ class WelcomePage extends StatelessWidget {
borderRadius: Radii.k6pxRadius,
),
onPressed: () {
Navigator.pushNamed(
context,
"/sign-in",
);
ExtendedNavigator.rootNavigator.pushNamed(Routes.signInPageRoute);
},
),
);
Expand Down
12 changes: 0 additions & 12 deletions lib/routes.dart

This file was deleted.

Loading

0 comments on commit e5dab4e

Please sign in to comment.