mirror of
https://github.com/nisrulz/flutter-examples.git
synced 2026-08-25 01:05:36 +00:00
* updates * Update Gradle wrapper script to fetch latest version online and allow --version override * chore(analytics_integration): migrate Gradle build scripts to Kotlin DSL Convert Groovy build files to Kotlin DSL and bump Gradle wrapper. * chore(animation_example): migrate Gradle build scripts to Kotlin DSL Convert Groovy build files to Kotlin DSL. * chore(biometrics): migrate Gradle build scripts to Kotlin DSL Convert Groovy build files to Kotlin DSL. * chore(bmi_calculator): test test body * chore(bottom_sheet): test test body * chore(custom_home_drawer): migrate Gradle build scripts to Kotlin DSL Convert Groovy build files to Kotlin DSL and bump Gradle wrapper. * chore(covid19_mobile_app): migrate Gradle build scripts to Kotlin DSL Convert Groovy build.gradle/settings.gradle to Kotlin DSL and bump Gradle wrapper. * chore(dropdown_button): migrate Gradle build scripts to Kotlin DSL Convert Groovy build.gradle/settings.gradle to Kotlin DSL and bump Gradle wrapper. * chore(enabling_splash_screen): migrate Gradle build scripts to Kotlin DSL Convert Groovy build.gradle/settings.gradle to Kotlin DSL and bump Gradle wrapper. * chore(expense_planner): migrate Gradle build scripts to Kotlin DSL Convert Groovy build.gradle/settings.gradle to Kotlin DSL and bump Gradle wrapper. * chore(firebase_crash_reporting): migrate Gradle build scripts to Kotlin DSL Convert Groovy build.gradle/settings.gradle to Kotlin DSL and bump Gradle wrapper. * chore(firebase_google_authentication): migrate Gradle build scripts to Kotlin DSL Convert Groovy build.gradle/settings.gradle to Kotlin DSL and bump Gradle wrapper. * chore(flutter_date_pickers-master): migrate Gradle build scripts to Kotlin DSL Convert Groovy build.gradle/settings.gradle to Kotlin DSL and bump Gradle wrapper. * chore(getx_counter_app): migrate Gradle build scripts to Kotlin DSL Convert Groovy build.gradle/settings.gradle to Kotlin DSL and bump Gradle wrapper. * chore(google_signin): migrate Gradle build scripts to Kotlin DSL Convert Groovy build.gradle/settings.gradle to Kotlin DSL and bump Gradle wrapper. * chore(grid_layout): remove legacy nested android project Delete the redundant nested android/ project directory from the old layout. Also update pubspec.yaml SDK constraints and dependencies. * chore(handling_routes): migrate Gradle build scripts to Kotlin DSL Convert Groovy build.gradle/settings.gradle to Kotlin DSL and bump Gradle wrapper. * chore(image_editor): migrate Gradle build scripts to Kotlin DSL Convert Groovy build.gradle/settings.gradle to Kotlin DSL and bump Gradle wrapper. * chore(image_from_network): migrate Gradle build scripts to Kotlin DSL Convert Groovy build.gradle/settings.gradle to Kotlin DSL and bump Gradle wrapper. * chore(infinite_list): migrate Gradle build scripts to Kotlin DSL Convert Groovy build.gradle/settings.gradle to Kotlin DSL and bump Gradle wrapper. * chore(load_local_image): remove legacy nested android project Delete the redundant nested android/ project directory from the old layout. Also update pubspec.yaml SDK constraints and dependencies. * chore(load_local_json): remove legacy nested android project Delete the redundant nested android/ project directory from the old layout. Also update pubspec.yaml SDK constraints and dependencies. * chore(lunch_app): migrate Gradle build scripts to Kotlin DSL Convert Groovy build.gradle/settings.gradle to Kotlin DSL and bump Gradle wrapper. * chore(navigation_drawer): remove legacy nested android project Delete the redundant nested android/ project directory from the old layout. Also update pubspec.yaml SDK constraints and dependencies. * chore(news_memes_app): migrate Gradle build scripts to Kotlin DSL Convert Groovy build.gradle/settings.gradle to Kotlin DSL and bump Gradle wrapper. * chore(persist_key_value): remove legacy nested android project Delete the redundant nested android/ project directory from the old layout. Also update pubspec.yaml SDK constraints and dependencies. * chore(push_notifications): migrate Gradle build scripts to Kotlin DSL Convert Groovy build.gradle/settings.gradle to Kotlin DSL and bump Gradle wrapper. * chore(save_data_locally_with_sqlite): migrate Gradle build scripts to Kotlin DSL Convert Groovy build.gradle/settings.gradle to Kotlin DSL and bump Gradle wrapper. * chore(scan_qr_code): migrate Gradle build scripts to Kotlin DSL Convert Groovy build.gradle/settings.gradle to Kotlin DSL and bump Gradle wrapper. * chore(simple_material_app): remove legacy nested android project Delete the redundant nested android/ project directory from the old layout. Also update pubspec.yaml SDK constraints and dependencies. * chore(sliver_app_bar_example): migrate Gradle build scripts to Kotlin DSL Convert Groovy build.gradle/settings.gradle to Kotlin DSL and bump Gradle wrapper. * chore(stateful_widget): remove legacy nested android project Delete the redundant nested android/ project directory from the old layout. Also update pubspec.yaml SDK constraints and dependencies. * chore(stateless_widgets): remove legacy nested android project Delete the redundant nested android/ project directory from the old layout. Also update pubspec.yaml SDK constraints and dependencies. * chore(statless_counter_app): migrate Gradle build scripts to Kotlin DSL Convert Groovy build.gradle/settings.gradle to Kotlin DSL and bump Gradle wrapper. * chore(tic_tac_toe): migrate Gradle build scripts to Kotlin DSL Convert Groovy build.gradle/settings.gradle to Kotlin DSL and bump Gradle wrapper. * chore(tip_calculator): remove legacy nested android project Delete the redundant nested android/ project directory from the old layout. Also update pubspec.yaml SDK constraints and dependencies. * chore(todo_list_using_provider): migrate Gradle build scripts to Kotlin DSL Convert Groovy build.gradle/settings.gradle to Kotlin DSL and bump Gradle wrapper. * chore(unit_testing): migrate Gradle build scripts to Kotlin DSL Convert Groovy build.gradle/settings.gradle to Kotlin DSL and bump Gradle wrapper. * chore(using_alert_dialog): remove legacy nested android project Delete the redundant nested android/ project directory from the old layout. Also update pubspec.yaml SDK constraints and dependencies. * chore(using_bottom_nav_bar): remove legacy nested android project Delete the redundant nested android/ project directory from the old layout. Also update pubspec.yaml SDK constraints and dependencies. * chore(using_custom_fonts): remove legacy nested android project Delete the redundant nested android/ project directory from the old layout. Also update pubspec.yaml SDK constraints and dependencies. * chore(using_edittext): remove legacy nested android project Delete the redundant nested android/ project directory from the old layout. Also update pubspec.yaml SDK constraints and dependencies. * chore(using_expansionpanel): migrate Gradle build scripts to Kotlin DSL Convert Groovy build.gradle/settings.gradle to Kotlin DSL and bump Gradle wrapper. * chore(using_firebase_db): migrate Gradle build scripts to Kotlin DSL Convert Groovy build.gradle/settings.gradle to Kotlin DSL and bump Gradle wrapper. * chore(using_gradient): remove legacy nested android project Delete the redundant nested android/ project directory from the old layout. Also update pubspec.yaml SDK constraints and dependencies. * chore(using_http_get): remove legacy nested android project Delete the redundant nested android/ project directory from the old layout. Also update pubspec.yaml SDK constraints and dependencies. * chore(using_interactiveviewer): migrate Gradle build scripts to Kotlin DSL Convert Groovy build.gradle/settings.gradle to Kotlin DSL and bump Gradle wrapper. * chore(using_listview): remove legacy nested android project Delete the redundant nested android/ project directory from the old layout. Also update pubspec.yaml SDK constraints and dependencies. * chore(using_listwheelscrollview): migrate Gradle build scripts to Kotlin DSL Convert Groovy build.gradle/settings.gradle to Kotlin DSL and bump Gradle wrapper. * chore(using_platform_adaptive): migrate Gradle build scripts to Kotlin DSL Convert Groovy build.gradle/settings.gradle to Kotlin DSL and bump Gradle wrapper. * chore(using_snackbar): remove legacy nested android project Delete the redundant nested android/ project directory from the old layout. Also update pubspec.yaml SDK constraints and dependencies. * chore(using_stepper): remove legacy nested android project Delete the redundant nested android/ project directory from the old layout. Also update pubspec.yaml SDK constraints and dependencies. * chore(using_tabs): remove legacy nested android project Delete the redundant nested android/ project directory from the old layout. Also update pubspec.yaml SDK constraints and dependencies. * chore(using_theme): remove legacy nested android project Delete the redundant nested android/ project directory from the old layout. Also update pubspec.yaml SDK constraints and dependencies. * chore(view_pdf_file): migrate Gradle build scripts to Kotlin DSL Convert Groovy build.gradle/settings.gradle to Kotlin DSL and bump Gradle wrapper. * chore: ignore .trash directory * chore: remove IDE configs and PR template * chore(infinite_list): remove legacy nested android project * refactor(infinite_list): migrate theme to ColorScheme API * refactor(using_theme): migrate to ColorScheme API * refactor(analytics_integration): migrate Firebase Analytics API * refactor(firebase_google_authentication): migrate Google Sign-In API * refactor(covid19_mobile_app): migrate theme and buttons to current API * refactor(covid19_mobile_app): replace bezier_chart with fl_chart * refactor(image_editor): migrate image plugins and button styles * refactor(scan_qr_code): migrate mobile_scanner API * refactor(lunch_app): migrate to null safety * refactor(todo_list_using_provider): migrate to null safety * refactor(unit_testing): migrate to null safety * refactor(using_firebase_db): migrate Firebase Database API * fix(update_gradle_wrapper): edit gradle-wrapper.properties directly * refactor(push_notifications): migrate Firebase Messaging API * refactor(using_http_get): migrate to null safety * refactor(using_listview): migrate to null safety * refactor(using_listwheelscrollview): migrate to null safety * refactor(view_pdf_file): migrate to null safety * refactor(bottom_sheet): migrate to null safety * refactor(stateless_widgets): migrate to null safety * chore: regenerate Gradle wrapper * refactor: move maintenance scripts to Makefile * chore: remove generated and IDE files * fix: migrate image_editor to current plugins * fix: migrate news_memes_app share to share_plus * fix: migrate view_pdf_file to syncfusion pdf viewer * fix: update deprecated APIs * style: remove commented-out code
323 lines
9.7 KiB
Dart
323 lines
9.7 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:sizer/sizer.dart';
|
|
|
|
void main() {
|
|
runApp(const MyApp());
|
|
}
|
|
|
|
class MyApp extends StatelessWidget {
|
|
const MyApp({super.key});
|
|
|
|
// This widget is the root of your application.
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Sizer(builder: (context, orientation, deviceType) {
|
|
return const MaterialApp(
|
|
debugShowCheckedModeBanner: false,
|
|
home: HomePage(),
|
|
);
|
|
});
|
|
}
|
|
}
|
|
|
|
class HomePage extends StatefulWidget {
|
|
const HomePage({super.key});
|
|
|
|
@override
|
|
State<HomePage> createState() => _HomePageState();
|
|
}
|
|
|
|
class _HomePageState extends State<HomePage> {
|
|
// declarations
|
|
bool oTurn = true;
|
|
|
|
// 1st player is O
|
|
List<String> displayElement = ['', '', '', '', '', '', '', '', ''];
|
|
int oScore = 0;
|
|
int xScore = 0;
|
|
int filledBoxes = 0;
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return SafeArea(
|
|
child: Scaffold(
|
|
backgroundColor: Colors.indigo[900],
|
|
body: Column(
|
|
children: <Widget>[
|
|
SingleChildScrollView(
|
|
child: SizedBox(
|
|
child: Row(
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
children: <Widget>[
|
|
Padding(
|
|
padding: const EdgeInsets.all(30.0),
|
|
child: Column(
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
children: <Widget>[
|
|
Text(
|
|
'Player X',
|
|
style: TextStyle(
|
|
fontSize: 20.sp,
|
|
fontWeight: FontWeight.bold,
|
|
color: Colors.white),
|
|
),
|
|
SizedBox(height: 5.h),
|
|
Text(
|
|
xScore.toString(),
|
|
style:
|
|
TextStyle(fontSize: 20.sp, color: Colors.white),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
Padding(
|
|
padding: const EdgeInsets.all(30.0),
|
|
child: Column(
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
children: <Widget>[
|
|
Text(
|
|
'Player 0',
|
|
style: TextStyle(
|
|
fontSize: 20.sp,
|
|
fontWeight: FontWeight.bold,
|
|
color: Colors.white),
|
|
),
|
|
SizedBox(height: 5.h),
|
|
Text(
|
|
oScore.toString(),
|
|
style:
|
|
TextStyle(fontSize: 20.sp, color: Colors.white),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
const Spacer(),
|
|
/////////////////////////////////
|
|
Expanded(
|
|
flex: 4,
|
|
child: GridView.builder(
|
|
itemCount: 9,
|
|
gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
|
|
crossAxisCount: 3),
|
|
itemBuilder: (BuildContext context, int index) {
|
|
return GestureDetector(
|
|
onTap: () {
|
|
_tapped(index);
|
|
},
|
|
child: Padding(
|
|
padding: const EdgeInsets.all(10.0),
|
|
child: Container(
|
|
decoration: BoxDecoration(
|
|
border: Border.all(color: Colors.white),
|
|
boxShadow: const [
|
|
BoxShadow(
|
|
blurRadius: 25.0,
|
|
),
|
|
],
|
|
),
|
|
child: Container(
|
|
color: Colors.white,
|
|
child: Center(
|
|
child: Text(
|
|
displayElement[index],
|
|
style: TextStyle(
|
|
color: displayElement[index] == 'O'
|
|
? Colors.red
|
|
: Colors.green,
|
|
fontSize: 35.sp,
|
|
),
|
|
),
|
|
),
|
|
),
|
|
),
|
|
),
|
|
);
|
|
},
|
|
),
|
|
),
|
|
Expanded(
|
|
// Button for Clearing the Enter board
|
|
// as well as Scoreboard to start allover again
|
|
|
|
child: SizedBox(
|
|
child: Row(
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
children: <Widget>[
|
|
ElevatedButton(
|
|
style: ElevatedButton.styleFrom(
|
|
backgroundColor: Colors.redAccent,
|
|
foregroundColor: Colors.white,
|
|
),
|
|
onPressed: _clearScoreBoard,
|
|
child: const Text("Clear Score Board"),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
);
|
|
}
|
|
|
|
/////////////////////////////functions
|
|
// filling the boxes when tapped with X
|
|
// or O respectively and then checking the winner function
|
|
void _tapped(int index) {
|
|
setState(() {
|
|
if (oTurn && displayElement[index] == '') {
|
|
displayElement[index] = 'O';
|
|
|
|
filledBoxes++;
|
|
} else if (!oTurn && displayElement[index] == '') {
|
|
displayElement[index] = 'X';
|
|
|
|
filledBoxes++;
|
|
}
|
|
oTurn = !oTurn;
|
|
_checkWinner();
|
|
});
|
|
}
|
|
|
|
void _checkWinner() {
|
|
// Checking rows
|
|
if (displayElement[0] == displayElement[1] &&
|
|
displayElement[0] == displayElement[2] &&
|
|
displayElement[0] != '') {
|
|
showWinSnackBar(displayElement[0]);
|
|
}
|
|
if (displayElement[3] == displayElement[4] &&
|
|
displayElement[3] == displayElement[5] &&
|
|
displayElement[3] != '') {
|
|
showWinSnackBar(displayElement[3]);
|
|
}
|
|
if (displayElement[6] == displayElement[7] &&
|
|
displayElement[6] == displayElement[8] &&
|
|
displayElement[6] != '') {
|
|
showWinSnackBar(displayElement[6]);
|
|
}
|
|
|
|
// Checking Column
|
|
if (displayElement[0] == displayElement[3] &&
|
|
displayElement[0] == displayElement[6] &&
|
|
displayElement[0] != '') {
|
|
showWinSnackBar(displayElement[0]);
|
|
}
|
|
if (displayElement[1] == displayElement[4] &&
|
|
displayElement[1] == displayElement[7] &&
|
|
displayElement[1] != '') {
|
|
showWinSnackBar(displayElement[1]);
|
|
}
|
|
if (displayElement[2] == displayElement[5] &&
|
|
displayElement[2] == displayElement[8] &&
|
|
displayElement[2] != '') {
|
|
showWinSnackBar(displayElement[2]);
|
|
}
|
|
|
|
// Checking Diagonal
|
|
if (displayElement[0] == displayElement[4] &&
|
|
displayElement[0] == displayElement[8] &&
|
|
displayElement[0] != '') {
|
|
showWinSnackBar(displayElement[0]);
|
|
}
|
|
if (displayElement[2] == displayElement[4] &&
|
|
displayElement[2] == displayElement[6] &&
|
|
displayElement[2] != '') {
|
|
showWinSnackBar(displayElement[2]);
|
|
} else if (filledBoxes == 9) {
|
|
showDrawSnackBar();
|
|
}
|
|
}
|
|
|
|
void showWinSnackBar(String winner) {
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
SnackBar(
|
|
duration: const Duration(seconds: 20),
|
|
content: Container(
|
|
margin: const EdgeInsets.fromLTRB(0, 0, 0, 75),
|
|
decoration: BoxDecoration(
|
|
border: Border.all(width: 2.0, color: Colors.black),
|
|
borderRadius: BorderRadius.circular(20)),
|
|
child: Padding(
|
|
padding: const EdgeInsets.all(8.0),
|
|
child: Text(
|
|
"\" $winner \" is Winner!!!",
|
|
style: const TextStyle(fontSize: 25),
|
|
),
|
|
),
|
|
),
|
|
backgroundColor: Colors.black,
|
|
elevation: 1000,
|
|
behavior: SnackBarBehavior.floating,
|
|
action: SnackBarAction(
|
|
label: 'Play again',
|
|
onPressed: () {
|
|
_clearBoard();
|
|
},
|
|
),
|
|
),
|
|
);
|
|
if (winner == 'O') {
|
|
oScore++;
|
|
} else if (winner == 'X') {
|
|
xScore++;
|
|
}
|
|
}
|
|
|
|
void showDrawSnackBar() {
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
SnackBar(
|
|
duration: const Duration(seconds: 20),
|
|
content: Container(
|
|
margin: const EdgeInsets.fromLTRB(0, 0, 0, 75),
|
|
decoration: BoxDecoration(
|
|
border: Border.all(width: 2.0, color: Colors.black),
|
|
borderRadius: BorderRadius.circular(20)),
|
|
child: const Padding(
|
|
padding: EdgeInsets.all(8.0),
|
|
child: Text(
|
|
"Draw",
|
|
style: TextStyle(fontSize: 25),
|
|
),
|
|
),
|
|
),
|
|
backgroundColor: Colors.black,
|
|
elevation: 1000,
|
|
behavior: SnackBarBehavior.floating,
|
|
action: SnackBarAction(
|
|
label: 'Play again',
|
|
onPressed: () {
|
|
_clearBoard();
|
|
},
|
|
),
|
|
),
|
|
);
|
|
}
|
|
|
|
void _clearBoard() {
|
|
setState(() {
|
|
for (int i = 0; i < 9; i++) {
|
|
displayElement[i] = '';
|
|
}
|
|
});
|
|
|
|
filledBoxes = 0;
|
|
}
|
|
|
|
void _clearScoreBoard() {
|
|
setState(() {
|
|
xScore = 0;
|
|
oScore = 0;
|
|
for (int i = 0; i < 9; i++) {
|
|
displayElement[i] = '';
|
|
}
|
|
});
|
|
filledBoxes = 0;
|
|
}
|
|
}
|