From 4626084abdea50df2ba351289927db3455a8212c Mon Sep 17 00:00:00 2001 From: Marco Date: Sun, 25 Dec 2022 19:57:47 +0100 Subject: [PATCH] Do some more restructuring. ChessApp is now the app. ChessGame is the widget that contains the game and board. --- lib/chess/chess_app.dart | 27 ++++++++++++ lib/connection/ws_connection.dart | 2 - lib/main.dart | 15 +------ lib/pages/chess_game.dart | 69 +++++++++++++++++++++++++++++++ lib/utils/chess_router.dart | 4 +- test/widget_test.dart | 4 +- 6 files changed, 102 insertions(+), 19 deletions(-) create mode 100644 lib/chess/chess_app.dart create mode 100644 lib/pages/chess_game.dart diff --git a/lib/chess/chess_app.dart b/lib/chess/chess_app.dart new file mode 100644 index 0000000..02a85d1 --- /dev/null +++ b/lib/chess/chess_app.dart @@ -0,0 +1,27 @@ +import 'package:flutter/material.dart'; +import 'package:flutter_bloc/flutter_bloc.dart'; +import 'package:mchess/chess_bloc/chess_bloc.dart'; +import 'package:mchess/connection_cubit/connection_cubit.dart'; +import 'package:mchess/utils/chess_router.dart'; + +class ChessApp extends StatelessWidget { + const ChessApp({super.key}); + + @override + Widget build(BuildContext context) { + return BlocProvider( + create: (_) => ConnectionCubit.getInstance(), + child: BlocProvider( + create: (_) => ChessBloc.getInstance(), + child: Container( + color: Colors.amber, + padding: const EdgeInsets.all(2), + child: MaterialApp.router( + routerConfig: router, + title: 'mChess v0.1.1337', + ), + ), + ), + ); + } +} diff --git a/lib/connection/ws_connection.dart b/lib/connection/ws_connection.dart index 68fb11c..e433bda 100644 --- a/lib/connection/ws_connection.dart +++ b/lib/connection/ws_connection.dart @@ -1,6 +1,4 @@ import 'dart:developer'; -import 'dart:io'; - import 'package:flutter/foundation.dart'; import 'package:mchess/chess_bloc/chess_bloc.dart'; import 'package:mchess/chess_bloc/chess_events.dart'; diff --git a/lib/main.dart b/lib/main.dart index 728a2a0..34e0a50 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -1,17 +1,6 @@ import 'package:flutter/material.dart'; -import 'package:mchess/utils/chess_router.dart'; +import 'package:mchess/chess/chess_app.dart'; void main() { - runApp(const MyApp()); -} - -class MyApp extends StatelessWidget { - const MyApp({super.key}); - - @override - Widget build(BuildContext context) { - return MaterialApp.router( - routerConfig: router, - ); - } + runApp(const ChessApp()); } diff --git a/lib/pages/chess_game.dart b/lib/pages/chess_game.dart new file mode 100644 index 0000000..1c32429 --- /dev/null +++ b/lib/pages/chess_game.dart @@ -0,0 +1,69 @@ +import 'package:flutter/foundation.dart'; +import 'package:flutter/material.dart'; +import 'package:flutter_bloc/flutter_bloc.dart'; +import 'package:mchess/chess_bloc/chess_bloc.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 'package:mchess/connection/ws_connection.dart'; +import 'package:mchess/utils/widgets/server_log_widget.dart'; + +class ChessGame extends StatelessWidget { + const ChessGame({super.key}); + + @override + Widget build(BuildContext context) { + return Scaffold( + body: Container( + decoration: const BoxDecoration( + gradient: LinearGradient( + begin: Alignment.topCenter, + end: Alignment.bottomCenter, + colors: [ + Color.fromARGB(255, 20, 20, 20), + Color.fromARGB(255, 30, 30, 30), + Color.fromARGB(255, 40, 40, 40), + ], + ), + ), + child: Center( + child: FittedBox( + fit: BoxFit.contain, + child: Row( + children: [ + if (kDebugMode) + StreamBuilder( + stream: ServerConnection.getInstance().broadcast, + builder: (context, snapshot) { + return ServerLogWidget( + snapshot.data ?? "", + textColor: Colors.white, + ); + }, + ), + Container( + margin: const EdgeInsets.all(20), + child: BlocBuilder( + builder: (context, state) { + return ChessBoard( + bState: state, + ); + }, + ), + ), + if (kDebugMode) const TurnIndicator(), + ], + ), + )), + ), + floatingActionButton: FloatingActionButton( + onPressed: () { + ConnectionCubit.getInstance().reconnect(); + }, + child: const Icon(Icons.network_wifi), + ), + ); + } +} diff --git a/lib/utils/chess_router.dart b/lib/utils/chess_router.dart index 785dc67..8170df9 100644 --- a/lib/utils/chess_router.dart +++ b/lib/utils/chess_router.dart @@ -1,5 +1,5 @@ import 'package:go_router/go_router.dart'; -import 'package:mchess/pages/chess_app.dart'; +import 'package:mchess/pages/chess_game.dart'; import 'package:mchess/pages/lobby_selector.dart'; final router = GoRouter( @@ -10,7 +10,7 @@ final router = GoRouter( ), GoRoute( path: '/play', - builder: (context, state) => const ChessApp(), + builder: (context, state) => const ChessGame(), ) ], ); diff --git a/test/widget_test.dart b/test/widget_test.dart index f47dc42..721f7e3 100644 --- a/test/widget_test.dart +++ b/test/widget_test.dart @@ -7,12 +7,12 @@ import 'package:flutter/material.dart'; import 'package:flutter_test/flutter_test.dart'; -import 'package:mchess/pages/chess_app.dart'; +import 'package:mchess/pages/chess_game.dart'; void main() { testWidgets('Counter increments smoke test', (WidgetTester tester) async { // Build our app and trigger a frame. - await tester.pumpWidget(const ChessApp()); + await tester.pumpWidget(const ChessGame()); // Verify that our counter starts at 0. expect(find.text('0'), findsOneWidget);