From 80a24bafd69adec743a73232848b040e13215e78 Mon Sep 17 00:00:00 2001 From: Marco Date: Sun, 25 Dec 2022 16:16:23 +0100 Subject: [PATCH] Add GoRouter and do a lot of renaming. --- lib/{chessapp => chess}/chess_board.dart | 2 +- lib/{chessapp => chess}/chess_square.dart | 2 +- .../turn_indicator_widget.dart | 2 +- lib/chess_bloc/chess_bloc.dart | 2 +- lib/chess_bloc/chess_events.dart | 2 +- lib/connection/ws_connection.dart | 2 +- lib/main.dart | 15 ++++++-- lib/{chessapp => pages}/chess_app.dart | 8 ++--- lib/pages/lobby_selector.dart | 19 ++++++++++ lib/utils/chess_router.dart | 16 +++++++++ lib/{chessapp => utils}/chess_utils.dart | 0 pubspec.lock | 35 +++++++++++++++---- pubspec.yaml | 1 + test/widget_test.dart | 2 +- 14 files changed, 88 insertions(+), 20 deletions(-) rename lib/{chessapp => chess}/chess_board.dart (98%) rename lib/{chessapp => chess}/chess_square.dart (98%) rename lib/{chessapp => chess}/turn_indicator_widget.dart (93%) rename lib/{chessapp => pages}/chess_app.dart (91%) create mode 100644 lib/pages/lobby_selector.dart create mode 100644 lib/utils/chess_router.dart rename lib/{chessapp => utils}/chess_utils.dart (100%) diff --git a/lib/chessapp/chess_board.dart b/lib/chess/chess_board.dart similarity index 98% rename from lib/chessapp/chess_board.dart rename to lib/chess/chess_board.dart index 9f80353..25f226a 100644 --- a/lib/chessapp/chess_board.dart +++ b/lib/chess/chess_board.dart @@ -4,7 +4,7 @@ import 'package:flutter/material.dart'; import 'package:mchess/chess_bloc/chess_bloc.dart'; import 'chess_square.dart'; -import 'chess_utils.dart'; +import '../utils/chess_utils.dart'; class ChessBoard extends StatelessWidget { final ChessBoardState bState; diff --git a/lib/chessapp/chess_square.dart b/lib/chess/chess_square.dart similarity index 98% rename from lib/chessapp/chess_square.dart rename to lib/chess/chess_square.dart index 355e925..425a572 100644 --- a/lib/chessapp/chess_square.dart +++ b/lib/chess/chess_square.dart @@ -4,7 +4,7 @@ import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:mchess/chess_bloc/chess_bloc.dart'; import '../connection/ws_connection.dart'; -import 'chess_utils.dart'; +import '../utils/chess_utils.dart'; int messageIndex = 0; diff --git a/lib/chessapp/turn_indicator_widget.dart b/lib/chess/turn_indicator_widget.dart similarity index 93% rename from lib/chessapp/turn_indicator_widget.dart rename to lib/chess/turn_indicator_widget.dart index 3e7e691..ff7365c 100644 --- a/lib/chessapp/turn_indicator_widget.dart +++ b/lib/chess/turn_indicator_widget.dart @@ -2,7 +2,7 @@ import 'package:flutter/material.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:mchess/chess_bloc/chess_bloc.dart'; -import 'chess_utils.dart'; +import '../utils/chess_utils.dart'; class TurnIndicator extends StatelessWidget { const TurnIndicator({super.key}); diff --git a/lib/chess_bloc/chess_bloc.dart b/lib/chess_bloc/chess_bloc.dart index b3c21fc..0fd891f 100644 --- a/lib/chess_bloc/chess_bloc.dart +++ b/lib/chess_bloc/chess_bloc.dart @@ -1,6 +1,6 @@ import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:mchess/chess_bloc/chess_events.dart'; -import 'package:mchess/chessapp/chess_utils.dart'; +import 'package:mchess/utils/chess_utils.dart'; import 'dart:developer'; class ChessBloc extends Bloc { diff --git a/lib/chess_bloc/chess_events.dart b/lib/chess_bloc/chess_events.dart index 355a04f..01298e3 100644 --- a/lib/chess_bloc/chess_events.dart +++ b/lib/chess_bloc/chess_events.dart @@ -1,4 +1,4 @@ -import 'package:mchess/chessapp/chess_utils.dart'; +import 'package:mchess/utils/chess_utils.dart'; abstract class ChessEvent {} diff --git a/lib/connection/ws_connection.dart b/lib/connection/ws_connection.dart index 4ebcb09..68fb11c 100644 --- a/lib/connection/ws_connection.dart +++ b/lib/connection/ws_connection.dart @@ -4,7 +4,7 @@ import 'dart:io'; import 'package:flutter/foundation.dart'; import 'package:mchess/chess_bloc/chess_bloc.dart'; import 'package:mchess/chess_bloc/chess_events.dart'; -import 'package:mchess/chessapp/chess_utils.dart'; +import 'package:mchess/utils/chess_utils.dart'; import 'package:web_socket_channel/web_socket_channel.dart'; class ServerConnection { diff --git a/lib/main.dart b/lib/main.dart index d3c1585..728a2a0 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -1,6 +1,17 @@ import 'package:flutter/material.dart'; -import 'chessapp/chess_app.dart'; +import 'package:mchess/utils/chess_router.dart'; void main() { - runApp(const ChessApp()); + runApp(const MyApp()); +} + +class MyApp extends StatelessWidget { + const MyApp({super.key}); + + @override + Widget build(BuildContext context) { + return MaterialApp.router( + routerConfig: router, + ); + } } diff --git a/lib/chessapp/chess_app.dart b/lib/pages/chess_app.dart similarity index 91% rename from lib/chessapp/chess_app.dart rename to lib/pages/chess_app.dart index a77f22e..4a59ce0 100644 --- a/lib/chessapp/chess_app.dart +++ b/lib/pages/chess_app.dart @@ -3,12 +3,12 @@ import 'package:flutter/material.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:mchess/chess_bloc/chess_bloc.dart'; -import 'package:mchess/chessapp/chess_board.dart'; -import 'package:mchess/chessapp/turn_indicator_widget.dart'; +import 'package:mchess/chess/chess_board.dart'; +import 'package:mchess/chess/turn_indicator_widget.dart'; import 'package:mchess/connection_cubit/connection_cubit.dart'; -import '../connection/ws_connection.dart'; -import '../utils/widgets/server_log_widget.dart'; +import 'package:mchess/connection/ws_connection.dart'; +import 'package:mchess/utils/widgets/server_log_widget.dart'; class ChessApp extends StatelessWidget { const ChessApp({super.key}); diff --git a/lib/pages/lobby_selector.dart b/lib/pages/lobby_selector.dart new file mode 100644 index 0000000..fd0b85d --- /dev/null +++ b/lib/pages/lobby_selector.dart @@ -0,0 +1,19 @@ +import 'package:flutter/material.dart'; +import 'package:go_router/go_router.dart'; + +class LobbySelector extends StatelessWidget { + const LobbySelector({super.key}); + + @override + Widget build(BuildContext context) { + return MaterialApp( + home: Scaffold( + body: TextButton( + onPressed: () { + context.go("/play"); + }, + child: const Text('Random lobby'), + )), + ); + } +} diff --git a/lib/utils/chess_router.dart b/lib/utils/chess_router.dart new file mode 100644 index 0000000..785dc67 --- /dev/null +++ b/lib/utils/chess_router.dart @@ -0,0 +1,16 @@ +import 'package:go_router/go_router.dart'; +import 'package:mchess/pages/chess_app.dart'; +import 'package:mchess/pages/lobby_selector.dart'; + +final router = GoRouter( + routes: [ + GoRoute( + path: '/', + builder: (context, state) => const LobbySelector(), + ), + GoRoute( + path: '/play', + builder: (context, state) => const ChessApp(), + ) + ], +); diff --git a/lib/chessapp/chess_utils.dart b/lib/utils/chess_utils.dart similarity index 100% rename from lib/chessapp/chess_utils.dart rename to lib/utils/chess_utils.dart diff --git a/pubspec.lock b/pubspec.lock index 10b507b..5ed668d 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -107,14 +107,27 @@ packages: description: flutter source: sdk version: "0.0.0" + flutter_web_plugins: + dependency: transitive + description: flutter + source: sdk + version: "0.0.0" + go_router: + dependency: "direct main" + description: + name: go_router + sha256: "788a89932142b0059c319e3153e73308e0fc8bbab78e9a66b0128423090c4e01" + url: "https://pub.dev" + source: hosted + version: "6.0.0" js: dependency: transitive description: name: js - sha256: "5528c2f391ededb7775ec1daa69e65a2d61276f7552de2b5f7b8d34ee9fd4ab7" + sha256: "323b7c70073cccf6b9b8d8b334be418a3293cfb612a560dc2737160a37bf61bd" url: "https://pub.dev" source: hosted - version: "0.6.5" + version: "0.6.6" lints: dependency: transitive description: @@ -123,14 +136,22 @@ packages: url: "https://pub.dev" source: hosted version: "2.0.1" + logging: + dependency: transitive + description: + name: logging + sha256: c0bbfe94d46aedf9b8b3e695cf3bd48c8e14b35e3b2c639e0aa7755d589ba946 + url: "https://pub.dev" + source: hosted + version: "1.1.0" matcher: dependency: transitive description: name: matcher - sha256: "16db949ceee371e9b99d22f88fa3a73c4e59fd0afed0bd25fc336eb76c198b72" + sha256: c94db23593b89766cda57aab9ac311e3616cf87c6fa4e9749df032f66f30dcb8 url: "https://pub.dev" source: hosted - version: "0.12.13" + version: "0.12.14" material_color_utilities: dependency: transitive description: @@ -252,10 +273,10 @@ packages: dependency: transitive description: name: test_api - sha256: ad540f65f92caa91bf21dfc8ffb8c589d6e4dc0c2267818b4cc2792857706206 + sha256: c9282698e2982b6c3817037554e52f99d4daba493e8028f8112a83d68ccd0b12 url: "https://pub.dev" source: hosted - version: "0.4.16" + version: "0.4.17" typed_data: dependency: transitive description: @@ -290,4 +311,4 @@ packages: version: "6.2.2" sdks: dart: ">=2.19.0-392.0.dev <4.0.0" - flutter: ">=2.11.0-0.1.pre" + flutter: ">=3.3.0" diff --git a/pubspec.yaml b/pubspec.yaml index ba6e2cf..78cb013 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -39,6 +39,7 @@ dependencies: flutter_bloc: ^8.1.1 quiver: ^3.1.0 web_socket_channel: ^2.2.0 + go_router: ^6.0.0 dev_dependencies: flutter_test: diff --git a/test/widget_test.dart b/test/widget_test.dart index c17f3ef..f47dc42 100644 --- a/test/widget_test.dart +++ b/test/widget_test.dart @@ -7,7 +7,7 @@ import 'package:flutter/material.dart'; import 'package:flutter_test/flutter_test.dart'; -import 'package:mchess/chessapp/chess_app.dart'; +import 'package:mchess/pages/chess_app.dart'; void main() { testWidgets('Counter increments smoke test', (WidgetTester tester) async {