Skip to content

Commit f005749

Browse files
committed
Create Routes Transition
*Create onGeneratedRoutes for custom route transitions.
1 parent 5d24adb commit f005749

File tree

4 files changed

+46
-12
lines changed

4 files changed

+46
-12
lines changed

lib/components/blog.dart

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import 'package:minimal/components/color.dart';
55
import 'package:minimal/components/spacing.dart';
66
import 'package:minimal/components/text.dart';
77
import 'package:minimal/components/typography.dart';
8+
import 'package:minimal/routes.dart';
89

910
class ImageWrapper extends StatelessWidget {
1011
final String image;
@@ -289,7 +290,7 @@ class ListItem extends StatelessWidget {
289290
child: Container(
290291
margin: marginBottom24,
291292
child: ReadMoreButton(
292-
onPressed: () => Navigator.pushNamed(context, '/post'),
293+
onPressed: () => Navigator.pushNamed(context, Routes.post),
293294
),
294295
),
295296
),
@@ -317,8 +318,8 @@ class MenuBar extends StatelessWidget {
317318
child: Row(
318319
children: <Widget>[
319320
GestureDetector(
320-
onTap: () =>
321-
Navigator.popUntil(context, ModalRoute.withName("/")),
321+
onTap: () => Navigator.popUntil(
322+
context, ModalRoute.withName(Navigator.defaultRouteName)),
322323
child: Text("MINIMAL",
323324
style: GoogleFonts.montserrat(
324325
color: textPrimary,
@@ -332,8 +333,8 @@ class MenuBar extends StatelessWidget {
332333
child: Wrap(
333334
children: <Widget>[
334335
FlatButton(
335-
onPressed: () => Navigator.popUntil(
336-
context, ModalRoute.withName("/")),
336+
onPressed: () => Navigator.popUntil(context,
337+
ModalRoute.withName(Navigator.defaultRouteName)),
337338
child: Text(
338339
"HOME",
339340
style: buttonTextStyle,
@@ -353,7 +354,8 @@ class MenuBar extends StatelessWidget {
353354
highlightColor: Colors.transparent,
354355
),
355356
FlatButton(
356-
onPressed: () => Navigator.pushNamed(context, '/style'),
357+
onPressed: () =>
358+
Navigator.pushNamed(context, Routes.style),
357359
child: Text(
358360
"STYLE",
359361
style: buttonTextStyle,

lib/main.dart

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import 'package:flutter/cupertino.dart';
21
import 'package:flutter/material.dart';
32
import 'package:minimal/pages/pages.dart';
3+
import 'package:minimal/routes.dart';
44
import 'package:responsive_framework/responsive_framework.dart';
55
import 'package:responsive_framework/utils/bouncing_scroll_behavior.dart';
66

@@ -27,11 +27,22 @@ class MyApp extends StatelessWidget {
2727
ResponsiveBreakpoint(breakpoint: 2460, name: "4K", autoScale: true),
2828
],
2929
background: Container(color: Color(0xFFF5F5F5))),
30-
initialRoute: "/",
31-
routes: {
32-
"/": (context) => ListPage(),
33-
"/post": (context) => PostPage(),
34-
"/style": (context) => TypographyPage(),
30+
initialRoute: Routes.home,
31+
onGenerateRoute: (RouteSettings settings) {
32+
switch (settings.name) {
33+
case Routes.home:
34+
return Routes.fadeThrough(settings, (context) => ListPage());
35+
break;
36+
case Routes.post:
37+
return Routes.fadeThrough(settings, (context) => PostPage());
38+
break;
39+
case Routes.style:
40+
return Routes.fadeThrough(settings, (context) => TypographyPage());
41+
break;
42+
default:
43+
return null;
44+
break;
45+
}
3546
},
3647
theme: Theme.of(context).copyWith(platform: TargetPlatform.android),
3748
debugShowCheckedModeBanner: false,

lib/routes.dart

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import 'package:animations/animations.dart';
2+
import 'package:flutter/widgets.dart';
3+
4+
class Routes {
5+
static const String home = "/";
6+
static const String post = "post";
7+
static const String style = "style";
8+
9+
static Route<T> fadeThrough<T>(RouteSettings settings, WidgetBuilder page,
10+
{int duration = 300}) {
11+
return PageRouteBuilder<T>(
12+
settings: settings,
13+
transitionDuration: Duration(milliseconds: duration),
14+
pageBuilder: (context, animation, secondaryAnimation) => page(context),
15+
transitionsBuilder: (context, animation, secondaryAnimation, child) {
16+
return FadeScaleTransition(animation: animation, child: child);
17+
},
18+
);
19+
}
20+
}

pubspec.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ dependencies:
1010
sdk: flutter
1111
responsive_framework: ^0.0.4
1212
google_fonts: ^0.3.10
13+
animations: ^1.0.0+5
1314

1415
dev_dependencies:
1516
flutter_test:

0 commit comments

Comments
 (0)