Skip to content

Commit

Permalink
code enhancement
Browse files Browse the repository at this point in the history
  • Loading branch information
wvzv committed Dec 4, 2022
1 parent cfb0934 commit 317695d
Show file tree
Hide file tree
Showing 12 changed files with 59 additions and 86 deletions.
20 changes: 0 additions & 20 deletions lib/Global/constant.dart
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
// ignore_for_file: prefer_const_constructors

import 'package:flappy_bird/Layouts/Pages/page_settings.dart';
import 'package:flappy_bird/Layouts/Widgets/widget_gradient _button.dart';
import 'package:flappy_bird/Layouts/Widgets/widget_bird.dart';
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 [GamePage]
int score = 0;
int topScore = 0;
Expand Down Expand Up @@ -37,16 +30,3 @@ double barrierMovement = 0.05;
/// Screen Boundary
double screenEnd = -1.9;
double screenStart = 3.5;

///[Settings] Variables
bool theme = true;
Color c = Colors.yellow;


/// to decide which page that class [Button] will navigate
enum Navigation{ home, settings, rateUs }

//
String im = "0";

String bird = "assets/pics/bird.png";
26 changes: 12 additions & 14 deletions lib/Global/functions.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// ignore_for_file: prefer_const_constructors

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

import '../Database/database.dart';
import 'constant.dart';

Expand Down Expand Up @@ -46,28 +44,28 @@ 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(Res.about, style: TextStyle(fontFamily: "Magic4"),),
content: Text(Str.about, style: TextStyle(fontFamily: "Magic4"),),
actions: [
gameButton(() {Navigator.pop(context);}, "Okay", Colors.deepOrange),
],
);
}

void initial() {
void init() {
if(read("score") != null){
topScore = read("score");
}else{
write("score", topScore);
}
if(read("background") != null){
im = read("background");
Str.image = read("background");
}else{
write("background", im);
write("background", Str.image);
}
if(read("bird") != null){
bird = read("bird");
Str.bird = read("bird");
}else{
write("bird", bird);
write("bird", Str.bird);
}
if(read("level") != null){
barrierMovement = read("level");
Expand All @@ -78,14 +76,14 @@ void initial() {

void navigate(context,navigate){
switch(navigate){
case Res.gamePage:
Navigator.pushNamed(context, Res.gamePage);
case Str.gamePage:
Navigator.pushNamed(context, Str.gamePage);
break;
case Res.settings:
Navigator.pushNamed(context, Res.settings);
case Str.settings:
Navigator.pushNamed(context, Str.settings);
break;
case Res.rateUs:
Navigator.pushNamed(context, Res.rateUs);
case Str.rateUs:
Navigator.pushNamed(context, Str.rateUs);
break;
}
}
6 changes: 2 additions & 4 deletions lib/Layouts/Pages/page_game.dart
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
// ignore_for_file: prefer_const_constructors, prefer_const_constructors_in_immutables, avoid_unnecessary_containers, avoid_print

import 'dart:async';
import 'package:flappy_bird/Layouts/Pages/page_start_screen.dart';
import 'package:flappy_bird/Layouts/Widgets/widget_bird.dart';
import 'package:flappy_bird/Layouts/Widgets/widget_barrier.dart';
import 'package:flappy_bird/Layouts/Widgets/widget_cover.dart';
import 'package:flutter/material.dart';
import 'package:hive/hive.dart';
import 'package:lottie/lottie.dart';
import '../../Database/database.dart';
import '../../Global/constant.dart';
import '../../Global/functions.dart';
import '../../Resources/strings.dart';

class GamePage extends StatefulWidget {
GamePage({Key? key}) : super(key: key);
Expand All @@ -28,7 +27,7 @@ class _GamePageState extends State<GamePage> {
Expanded(
flex: 3,
child: Container(
decoration: background(im),
decoration: background(Str.image),
child: Stack(
children: [
Bird(yAxis, birdWidth, birdHeight),
Expand Down Expand Up @@ -80,7 +79,6 @@ class _GamePageState extends State<GamePage> {
height = gravity * time * time + velocity * time;
setState(() {
yAxis = initialHeight - height;

});
/* < Barriers Movements > */
setState(() {
Expand Down
4 changes: 2 additions & 2 deletions lib/Layouts/Pages/page_rate_us.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import 'package:flutter/material.dart';
import 'package:rating_dialog/rating_dialog.dart';
import 'package:share_plus/share_plus.dart';
import '../../Global/constant.dart';
import '../../Global/functions.dart';
import '../../Resources/strings.dart';

class RateUs extends StatelessWidget {
RateUs({Key? key}) : super(key: key);
Expand All @@ -16,7 +16,7 @@ class RateUs extends StatelessWidget {
body: Container(
width: size.width,
height: size.height,
decoration: background(im),
decoration: background(Str.image),
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
Expand Down
7 changes: 2 additions & 5 deletions lib/Layouts/Pages/page_settings.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,8 @@ import 'package:flappy_bird/Layouts/Widgets/widget_bird_settings.dart';
import 'package:flappy_bird/Layouts/Widgets/widget_difficulty_settings.dart';
import 'package:flappy_bird/Layouts/Widgets/widget_music_settings.dart';
import 'package:flappy_bird/Layouts/Widgets/widget_themes_settings.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';

class Settings extends StatefulWidget {
Expand All @@ -27,7 +24,7 @@ class _SettingsState extends State<Settings> {
body: Container(
width: size.width,
height: size.height,
decoration: background(im),
decoration: background(Str.image),
child: Column(
children: [
Container(
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
Expand Up @@ -19,7 +19,7 @@ class _StartScreenState extends State<StartScreen> {
@override
void initState() {
// Todo : initialize the database <---
initial();
init();
super.initState();
}

Expand All @@ -32,13 +32,13 @@ class _StartScreenState extends State<StartScreen> {
body: Container(
width: size.width,
height: size.height,
decoration: background(im),
decoration: background(Str.image),
child: Column(
children: [
// Flappy bird text
Container(
margin: EdgeInsets.only(top: size.height * 0.25),
child: Text("FlappyBird", style: TextStyle(fontSize: 70,fontFamily: "Magic4",color: Colors.white,),)),
child: myText("FlappyBird", Colors.white,70)),
Bird(yAxis, birdWidth, birdHeight),
_buttons(),
AboutUs(size: size,)
Expand All @@ -53,12 +53,12 @@ class _StartScreenState extends State<StartScreen> {
Column _buttons(){
return Column(
children: [
Button(buttonType: "text",height: 60,width: 278,icon: Icon(Icons.play_arrow_rounded,size: 60,color: Colors.green,),page: Res.gamePage,),
Button(buttonType: "text",height: 60,width: 278,icon: Icon(Icons.play_arrow_rounded,size: 60,color: Colors.green,),page: Str.gamePage,),
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
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,),
Button(buttonType: "icon",height: 60,width: 110,icon: Icon(Icons.settings,size: 40,color: Colors.grey.shade900,),page: Str.settings,),
Button(buttonType: "icon",height: 60,width: 110,icon: Icon(Icons.star,size: 40,color: Colors.deepOrange,),page: Str.rateUs,),
],
),
],
Expand All @@ -77,7 +77,7 @@ class AboutUs extends StatelessWidget {
showDialog(context: context, builder: (context) {
return dialog(context);
},);
},child: Text("About Us",style: TextStyle(fontSize: 20,fontFamily: "Magic4",color: Colors.white),)),
},child: myText("About Us",Colors.white,20)),
);
}
}
6 changes: 2 additions & 4 deletions lib/Layouts/Widgets/widget_bird.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
// ignore_for_file: sized_box_for_whitespace, prefer_const_constructors, prefer_const_constructors_in_immutables, use_key_in_widget_constructors

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

import '../../Global/constant.dart';

class Bird extends StatelessWidget {
final double yAxis;
final double birdWidth;
Expand All @@ -16,7 +14,7 @@ class Bird extends StatelessWidget {
return AnimatedContainer(
alignment: Alignment(0, (2 * yAxis + birdHeight) / (2- birdHeight)),
duration: Duration(milliseconds: 0),
child: Image.asset(bird,
child: Image.asset(Str.bird,
width: size.width * birdWidth,
height: size.height * birdHeight,
fit: BoxFit.cover,
Expand Down
13 changes: 7 additions & 6 deletions lib/Layouts/Widgets/widget_bird_settings.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// ignore_for_file: prefer_const_constructors

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

import '../../Global/constant.dart';
Expand All @@ -21,8 +22,8 @@ class BirdSettings extends StatelessWidget {
children: [
GestureDetector(
onTap: () {
bird = "assets/pics/bird.png";
write("bird", bird);
Str.bird = "assets/pics/bird.png";
write("bird", Str.bird);
},
child: SizedBox(
width: 75,
Expand All @@ -35,8 +36,8 @@ class BirdSettings extends StatelessWidget {
),
GestureDetector(
onTap: () {
bird = "assets/pics/blue.png";
write("bird", bird);
Str.bird = "assets/pics/blue.png";
write("bird", Str.bird);
},
child: SizedBox(
width: 75,
Expand All @@ -49,8 +50,8 @@ class BirdSettings extends StatelessWidget {
),
GestureDetector(
onTap: () {
bird = "assets/pics/green.png";
write("bird", bird);
Str.bird = "assets/pics/green.png";
write("bird", Str.bird);
},
child: SizedBox(
width: 75,
Expand Down
24 changes: 11 additions & 13 deletions lib/Layouts/Widgets/widget_themes_settings.dart
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
// ignore_for_file: prefer_const_constructors

import 'package:flappy_bird/Database/database.dart';
import 'package:flappy_bird/Resources/strings.dart';
import 'package:flutter/material.dart';
import '../../Global/constant.dart';
import '../../Global/functions.dart';


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

@override
State<ThemesSettings> createState() => _ThemesSettingsState();
}
Expand All @@ -27,23 +25,23 @@ class _ThemesSettingsState extends State<ThemesSettings> {
children: [
GestureDetector(onTap: (){
setState(() {
im = "0";
write("background", im);
background(im);
Str.image = "0";
write("background", Str.image);
background(Str.image);
});
},child: Image.asset("assets/pics/0.png",width: 73,height: 70,)),
GestureDetector(onTap: (){
setState(() {
im = "1";
write("background", im);
background(im);
Str.image = "1";
write("background", Str.image);
background(Str.image);
});
},child: Image.asset("assets/pics/1.png",width: 73,height: 70,)),
GestureDetector(onTap: (){
setState(() {
im = "2";
write("background", im);
background(im);
Str.image = "2";
write("background", Str.image);
background(Str.image);
});
},child: Image.asset("assets/pics/2.png",width: 63,height: 66,)),
],
Expand All @@ -52,4 +50,4 @@ class _ThemesSettingsState extends State<ThemesSettings> {
),
);
}
}
}
15 changes: 9 additions & 6 deletions lib/Resources/strings.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
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"
class Str{
static const String home = "/";
static const String gamePage = "game";
static const String rateUs = "rate";
static const String settings = "settings";
static const 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";

static String image = "";
static String bird = "assets/pics/bird.png";
}
8 changes: 4 additions & 4 deletions lib/Routes/app_routes.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ import '../Resources/strings.dart';
class AppRoute{
Route? generateRoute(RouteSettings settings){
switch(settings.name){
case Res.home:
case Str.home:
return MaterialPageRoute(builder: (_) => StartScreen());
case Res.gamePage:
case Str.gamePage:
return MaterialPageRoute(builder: (_) => GamePage());
case Res.rateUs:
case Str.rateUs:
return MaterialPageRoute(builder: (_) => RateUs());
case Res.settings:
case Str.settings:
return MaterialPageRoute(builder: (_) => Settings());
default:
_errorRoute();
Expand Down
2 changes: 1 addition & 1 deletion lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class MyApp extends StatelessWidget {
return MaterialApp(
home: StartScreen(),
debugShowCheckedModeBanner: false,
initialRoute: Res.home,
initialRoute: Str.home,
onGenerateRoute: AppRoute().generateRoute,
);
}
Expand Down

0 comments on commit 317695d

Please sign in to comment.