Skip to content

Commit

Permalink
Routes
Browse files Browse the repository at this point in the history
Navigation between screens with generated Route
  • Loading branch information
wvzv committed Dec 4, 2022
1 parent 0fbf47a commit cfb0934
Show file tree
Hide file tree
Showing 9 changed files with 103 additions and 83 deletions.
7 changes: 2 additions & 5 deletions lib/Global/constant.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import 'package:flappy_bird/Layouts/Pages/page_game.dart';
import 'package:flappy_bird/Layouts/Widgets/widget_barrier.dart';
import 'package:flutter/material.dart';

/// SCORE Variables calculated in function [startGame] in [HomePage]
/// SCORE Variables calculated in function [startGame] in [GamePage]
int score = 0;
int topScore = 0;

Expand All @@ -16,7 +16,7 @@ double yAxis = 0;
double birdWidth = 0.183;
double birdHeight = 0.183;

/// Variables to calculate bird movements function [startGame] in [HomePage]
/// Variables to calculate bird movements function [startGame] in [GamePage]
double time = 0;
double height = 0;
double gravity = -3.9; // How strong the Gravity
Expand Down Expand Up @@ -47,9 +47,6 @@ Color c = Colors.yellow;
enum Navigation{ home, settings, rateUs }

//
String about = "The game is a side-scroller where the player"
" controls a bird, attempting to fly between "
" columns of green pipes without hitting them";
String im = "0";

String bird = "assets/pics/bird.png";
17 changes: 16 additions & 1 deletion lib/Global/functions.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// ignore_for_file: prefer_const_constructors

import 'package:flappy_bird/Resources/strings.dart';
import 'package:flutter/material.dart';

import '../Database/database.dart';
Expand Down Expand Up @@ -45,7 +46,7 @@ AlertDialog dialog(BuildContext context){
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(18)),
actionsPadding: EdgeInsets.only(right: 8, bottom: 8),
title: myText("About Flappy Bird",Colors.black, 22),
content: Text(about, style: TextStyle(fontFamily: "Magic4"),),
content: Text(Res.about, style: TextStyle(fontFamily: "Magic4"),),
actions: [
gameButton(() {Navigator.pop(context);}, "Okay", Colors.deepOrange),
],
Expand Down Expand Up @@ -73,4 +74,18 @@ void initial() {
}else{
write("level", barrierMovement);
}
}

void navigate(context,navigate){
switch(navigate){
case Res.gamePage:
Navigator.pushNamed(context, Res.gamePage);
break;
case Res.settings:
Navigator.pushNamed(context, Res.settings);
break;
case Res.rateUs:
Navigator.pushNamed(context, Res.rateUs);
break;
}
}
8 changes: 4 additions & 4 deletions lib/Layouts/Pages/page_game.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ import '../../Database/database.dart';
import '../../Global/constant.dart';
import '../../Global/functions.dart';

class HomePage extends StatefulWidget {
HomePage({Key? key}) : super(key: key);
class GamePage extends StatefulWidget {
GamePage({Key? key}) : super(key: key);
@override
State<HomePage> createState() => _HomePageState();
State<GamePage> createState() => _GamePageState();
}
class _HomePageState extends State<HomePage> {
class _GamePageState extends State<GamePage> {

@override
Widget build(BuildContext context) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// ignore_for_file: prefer_const_constructors
// ignore_for_file: prefer_const_constructors, prefer_const_constructors_in_immutables

import 'package:flutter/material.dart';
import 'package:rating_dialog/rating_dialog.dart';
Expand All @@ -8,7 +8,7 @@ import '../../Global/functions.dart';

class RateUs extends StatelessWidget {
RateUs({Key? key}) : super(key: key);
double rating = 4.0;
final double rating = 4.0;
@override
Widget build(BuildContext context) {
Size size = MediaQuery.of(context).size;
Expand All @@ -26,8 +26,6 @@ class RateUs extends StatelessWidget {
image: Icon(Icons.star, size: 100, color: Colors.red),
submitButtonText: 'Submit',
onSubmitted: (response) {
print('onSubmitPressed: rating = ${response.rating}');
print('Comment :${response.comment}');
}),
SizedBox(
width: 100,
Expand Down
14 changes: 7 additions & 7 deletions lib/Layouts/Pages/page_start_screen.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// ignore_for_file: prefer_const_constructors, prefer_const_literals_to_create_immutables, prefer_const_constructors_in_immutables
import 'package:flappy_bird/Layouts/Widgets/widget_bird.dart';
import 'package:flappy_bird/Resources/strings.dart';
import 'package:flutter/material.dart';
import 'package:hive/hive.dart';
import '../../Database/database.dart';
import '../../Global/constant.dart';
import '../../Global/functions.dart';
import '../Widgets/widget_gradient _button.dart';
Expand Down Expand Up @@ -40,7 +40,7 @@ class _StartScreenState extends State<StartScreen> {
margin: EdgeInsets.only(top: size.height * 0.25),
child: Text("FlappyBird", style: TextStyle(fontSize: 70,fontFamily: "Magic4",color: Colors.white,),)),
Bird(yAxis, birdWidth, birdHeight),
Buttons(),
_buttons(),
AboutUs(size: size,)
],
),
Expand All @@ -50,23 +50,23 @@ class _StartScreenState extends State<StartScreen> {
}

// three buttons
Column Buttons(){
Column _buttons(){
return Column(
children: [
Button(buttonType: "text",height: 60,width: 278,icon: Icon(Icons.play_arrow_rounded,size: 60,color: Colors.green,),type: Navigation.home.name,),
Button(buttonType: "text",height: 60,width: 278,icon: Icon(Icons.play_arrow_rounded,size: 60,color: Colors.green,),page: Res.gamePage,),
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
Button(buttonType: "icon",height: 60,width: 110,icon: Icon(Icons.settings,size: 40,color: Colors.grey.shade900,),type: Navigation.settings.name,),
Button(buttonType: "icon",height: 60,width: 110,icon: Icon(Icons.star,size: 40,color: Colors.deepOrange,),type: Navigation.rateUs.name,),
Button(buttonType: "icon",height: 60,width: 110,icon: Icon(Icons.settings,size: 40,color: Colors.grey.shade900,),page: Res.settings,),
Button(buttonType: "icon",height: 60,width: 110,icon: Icon(Icons.star,size: 40,color: Colors.deepOrange,),page: Res.rateUs,),
],
),
],
);
}

class AboutUs extends StatelessWidget {
Size size;
final Size size;
AboutUs({required this.size,Key? key}) : super(key: key);

@override
Expand Down
49 changes: 13 additions & 36 deletions lib/Layouts/Widgets/widget_gradient _button.dart
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
// ignore_for_file: prefer_const_constructors, prefer_const_constructors_in_immutables
// ignore_for_file: prefer_const_constructors

import 'package:flappy_bird/Layouts/Pages/page_RateUs.dart';
import 'package:flappy_bird/Resources/strings.dart';
import 'package:flutter/material.dart';
import '../../Global/constant.dart';
import '../Pages/page_game.dart';
import '../Pages/page_settings.dart';
import '../Pages/page_RateUs.dart';
import '../../Global/functions.dart';



class Button extends StatelessWidget {
Icon? icon;
double width;
double height;
String type;
String buttonType;
Button({required this.width, required this.height,this.icon, required this.type,required this.buttonType,Key? key}) : super(key: key);
final Icon? icon;
final double width;
final double height;
final String page;
final String buttonType;
const Button({required this.width, required this.height,this.icon, required this.page,required this.buttonType,Key? key}) : super(key: key);

@override
Widget build(BuildContext context) {
Expand All @@ -32,33 +30,12 @@ class Button extends StatelessWidget {
end: Alignment.bottomCenter),
),
alignment: Alignment.center,
child: buttonType == "text"? Text("Play",style: TextStyle(fontFamily: "Magic4",color: Colors.green,fontSize: 35,),)
child: buttonType == "text"? myText("Play",Colors.green,35)
: icon
),
onTap: (){
if(type == Navigation.home.name){
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => HomePage(),
),
);
}else if(type == Navigation.settings.name){
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => Settings(),
),
);
}else{
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => RateUs(),
),
);
}
navigate(context, page);
},
);
}
}
}
9 changes: 9 additions & 0 deletions lib/Resources/strings.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class Res{
static const home = "/";
static const gamePage = "game";
static const rateUs = "rate";
static const settings = "settings";
static const about = "The game is a side-scroller where the player"
" controls a bird, attempting to fly between "
" columns of green pipes without hitting them";
}
38 changes: 38 additions & 0 deletions lib/Routes/app_routes.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// ignore_for_file: prefer_const_constructors
import 'package:flappy_bird/Layouts/Pages/page_game.dart';
import 'package:flappy_bird/Layouts/Pages/page_settings.dart';
import 'package:flutter/material.dart';
import '../Layouts/Pages/page_rate_us.dart';
import '../Layouts/Pages/page_start_screen.dart';
import '../Resources/strings.dart';

class AppRoute{
Route? generateRoute(RouteSettings settings){
switch(settings.name){
case Res.home:
return MaterialPageRoute(builder: (_) => StartScreen());
case Res.gamePage:
return MaterialPageRoute(builder: (_) => GamePage());
case Res.rateUs:
return MaterialPageRoute(builder: (_) => RateUs());
case Res.settings:
return MaterialPageRoute(builder: (_) => Settings());
default:
_errorRoute();
}
}

static Route<dynamic> _errorRoute (){
return MaterialPageRoute(builder: (context) {
return Scaffold(
appBar: AppBar(
title: Text("Error Page"),
backgroundColor: Colors.redAccent,
),
body: Center(
child: Text("Error"),
),
);
},);
}
}
38 changes: 12 additions & 26 deletions lib/Core/main.dart → lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,46 +1,32 @@
// ignore_for_file: prefer_const_constructors, unnecessary_import, unused_local_variable

import 'package:flappy_bird/Routes/app_routes.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:hive_flutter/hive_flutter.dart';
import '../Layouts/Pages/page_start_screen.dart';
import 'Layouts/Pages/page_start_screen.dart';
import 'Resources/strings.dart';

void main() async{
await Hive.initFlutter();
var box = await Hive.openBox('user');
runApp(
MaterialApp(
home: MyApp(),
debugShowCheckedModeBanner: false,
),
);
runApp(MyApp());
}

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

@override
State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {

@override
void initState() {
super.initState();
}

@override
void dispose() {
super.dispose();
}

@override
Widget build(BuildContext context) {
SystemChrome.setPreferredOrientations([
DeviceOrientation.portraitUp,
DeviceOrientation.portraitDown,
]);
return StartScreen();
return MaterialApp(
home: StartScreen(),
debugShowCheckedModeBanner: false,
initialRoute: Res.home,
onGenerateRoute: AppRoute().generateRoute,
);
}
}
}

0 comments on commit cfb0934

Please sign in to comment.