Skip to content

Commit

Permalink
feat: added simple router using onGenerateRoute
Browse files Browse the repository at this point in the history
create three simple screens
  • Loading branch information
kekavc24 committed Nov 19, 2022
1 parent 1bea7f8 commit a7b6c0a
Show file tree
Hide file tree
Showing 4 changed files with 166 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import 'package:animations/animations.dart';
import 'package:flutter/material.dart';

import 'screen_one.dart';
import 'screen_three.dart';
import 'screen_two.dart';

class Routes {
//
static const String screenOne = 'splash';
static const String screenTwo = 'loader';
static const String screenThree = 'login';
//
static Route<T> fadeThrough<T>(RouteSettings settings, WidgetBuilder page,
{int duration = 500}) {
return PageRouteBuilder<T>(
settings: settings,
transitionDuration: Duration(milliseconds: duration),
pageBuilder: (context, animation, secondaryAnimation) => page(context),
transitionsBuilder: (context, animation, secondaryAnimation, child) {
return FadeScaleTransition(animation: animation, child: child);
},
);
}
//
}

class AppRouter {
//
Route onGenerateRoute(RouteSettings settings) {
//
return Routes.fadeThrough(settings, (context) {
//
switch (settings.name) {
case Routes.screenOne:
return const ScreenOne();

case Routes.screenTwo:
return const ScreenTwo();

case Routes.screenThree:
return const ScreenThree();

default:
return const SizedBox.shrink();
}
//
});
//
}
//
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import 'package:flutter/material.dart';

import 'routes.dart';

class ScreenOne extends StatefulWidget {
const ScreenOne({Key? key}) : super(key: key);

@override
State<ScreenOne> createState() => _ScreenOneState();
}

class _ScreenOneState extends State<ScreenOne> {
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
//
Text(
"This is screen one",
style: Theme.of(context).textTheme.headlineMedium,
),

const SizedBox(height: 20),

ElevatedButton(
onPressed: () => Navigator.pushNamed(context, Routes.screenTwo),
child: const Text("Go To Screen Two"),
)
//
],
),
),
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import 'package:flutter/material.dart';

import 'routes.dart';

class ScreenThree extends StatefulWidget {
const ScreenThree({Key? key}) : super(key: key);

@override
State<ScreenThree> createState() => _ScreenThreeState();
}

class _ScreenThreeState extends State<ScreenThree> {
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
//
Text(
"This is screen three",
style: Theme.of(context).textTheme.headlineMedium,
),

const SizedBox(height: 20),

ElevatedButton(
onPressed: () => Navigator.pushNamed(context, Routes.screenOne),
child: const Text("Go Back To Screen One"),
)
//
],
),
),
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import 'package:flutter/material.dart';

import 'routes.dart';

class ScreenTwo extends StatefulWidget {
const ScreenTwo({Key? key}) : super(key: key);

@override
State<ScreenTwo> createState() => _ScreenTwoState();
}

class _ScreenTwoState extends State<ScreenTwo> {
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
//
Text(
"This is screen two",
style: Theme.of(context).textTheme.headlineMedium,
),

const SizedBox(height: 20),

ElevatedButton(
onPressed: () => Navigator.pushNamed(context, Routes.screenThree),
child: const Text("Go To Screen Three"),
)
//
],
),
),
);
}
}

0 comments on commit a7b6c0a

Please sign in to comment.