Add Connection cubit that updates the part that must be rebuilt after a reconnect.
This commit is contained in:
parent
4768d22168
commit
8d5cf7592d
@ -4,61 +4,69 @@ import 'package:mchess/chess_bloc/chess_bloc.dart';
|
|||||||
import 'package:mchess/connection/ws_connection.dart';
|
import 'package:mchess/connection/ws_connection.dart';
|
||||||
|
|
||||||
import 'package:mchess/chessapp/chess_board.dart';
|
import 'package:mchess/chessapp/chess_board.dart';
|
||||||
|
import 'package:mchess/connection_cubit/connection_cubit.dart';
|
||||||
|
|
||||||
class ChessApp extends StatelessWidget {
|
class ChessApp extends StatelessWidget {
|
||||||
const ChessApp({super.key});
|
const ChessApp({super.key});
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
return MaterialApp(
|
return BlocProvider(
|
||||||
title: 'mChess',
|
create: (_) => ConnectionCubit.getInstance(),
|
||||||
home: Scaffold(
|
child: MaterialApp(
|
||||||
body: Container(
|
title: 'mChess',
|
||||||
decoration: const BoxDecoration(
|
home: Scaffold(
|
||||||
gradient: LinearGradient(
|
body: Container(
|
||||||
begin: Alignment.topCenter,
|
decoration: const BoxDecoration(
|
||||||
end: Alignment.bottomCenter,
|
gradient: LinearGradient(
|
||||||
colors: [
|
begin: Alignment.topCenter,
|
||||||
Color.fromARGB(255, 20, 20, 20),
|
end: Alignment.bottomCenter,
|
||||||
Color.fromARGB(255, 30, 30, 30),
|
colors: [
|
||||||
Color.fromARGB(255, 40, 40, 40),
|
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: Column(
|
|
||||||
children: [
|
|
||||||
Container(
|
|
||||||
margin: const EdgeInsets.all(20),
|
|
||||||
child: BlocProvider(
|
|
||||||
create: (_) => ChessBloc.getInstance(),
|
|
||||||
child: BlocBuilder<ChessBloc, ChessBoardState>(
|
|
||||||
builder: (context, state) {
|
|
||||||
return ChessBoard(
|
|
||||||
bState: state,
|
|
||||||
);
|
|
||||||
},
|
|
||||||
),
|
|
||||||
),
|
|
||||||
),
|
|
||||||
StreamBuilder(
|
|
||||||
stream: ServerConnection.getInstance().channel.stream,
|
|
||||||
builder: (context, snapshot) {
|
|
||||||
return Text(
|
|
||||||
style: const TextStyle(color: Colors.white),
|
|
||||||
snapshot.data.toString());
|
|
||||||
},
|
|
||||||
)
|
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
|
child: Center(
|
||||||
|
child: FittedBox(
|
||||||
|
fit: BoxFit.contain,
|
||||||
|
child: Column(
|
||||||
|
children: [
|
||||||
|
Container(
|
||||||
|
margin: const EdgeInsets.all(20),
|
||||||
|
child: BlocProvider(
|
||||||
|
create: (_) => ChessBloc.getInstance(),
|
||||||
|
child: BlocBuilder<ChessBloc, ChessBoardState>(
|
||||||
|
builder: (context, state) {
|
||||||
|
return ChessBoard(
|
||||||
|
bState: state,
|
||||||
|
);
|
||||||
|
},
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
BlocBuilder<ConnectionCubit, ConnectionCubitState>(
|
||||||
|
builder: (context, state) {
|
||||||
|
return StreamBuilder(
|
||||||
|
stream: ServerConnection.getInstance().channel.stream,
|
||||||
|
builder: (context, snapshot) {
|
||||||
|
return Text(
|
||||||
|
style: const TextStyle(color: Colors.white),
|
||||||
|
snapshot.data.toString());
|
||||||
|
},
|
||||||
|
);
|
||||||
|
},
|
||||||
|
)
|
||||||
|
],
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
floatingActionButton: const FloatingActionButton(
|
||||||
|
onPressed: reconnect,
|
||||||
|
child: Icon(Icons.connect_without_contact),
|
||||||
),
|
),
|
||||||
),
|
|
||||||
floatingActionButton: const FloatingActionButton(
|
|
||||||
onPressed: reconnect,
|
|
||||||
child: Icon(Icons.connect_without_contact),
|
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
@ -67,4 +75,5 @@ class ChessApp extends StatelessWidget {
|
|||||||
|
|
||||||
void reconnect() {
|
void reconnect() {
|
||||||
ServerConnection.getInstance().reconnect();
|
ServerConnection.getInstance().reconnect();
|
||||||
|
ConnectionCubit.getInstance().reconnect();
|
||||||
}
|
}
|
||||||
|
29
lib/connection_cubit/connection_cubit.dart
Normal file
29
lib/connection_cubit/connection_cubit.dart
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||||
|
|
||||||
|
class ConnectionCubit extends Cubit<ConnectionCubitState> {
|
||||||
|
static final ConnectionCubit _instance = ConnectionCubit._internal();
|
||||||
|
|
||||||
|
ConnectionCubit._internal() : super(ConnectionCubitState.init());
|
||||||
|
|
||||||
|
factory ConnectionCubit.getInstance() {
|
||||||
|
return ConnectionCubit();
|
||||||
|
}
|
||||||
|
|
||||||
|
factory ConnectionCubit() {
|
||||||
|
return _instance;
|
||||||
|
}
|
||||||
|
|
||||||
|
void reconnect() {
|
||||||
|
emit(ConnectionCubitState(true));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class ConnectionCubitState {
|
||||||
|
final bool reconnecting;
|
||||||
|
|
||||||
|
ConnectionCubitState(this.reconnecting);
|
||||||
|
|
||||||
|
factory ConnectionCubitState.init() {
|
||||||
|
return ConnectionCubitState(false);
|
||||||
|
}
|
||||||
|
}
|
@ -236,10 +236,10 @@ packages:
|
|||||||
dependency: transitive
|
dependency: transitive
|
||||||
description:
|
description:
|
||||||
name: string_scanner
|
name: string_scanner
|
||||||
sha256: "862015c5db1f3f3c4ea3b94dc2490363a84262994b88902315ed74be1155612f"
|
sha256: "556692adab6cfa87322a115640c11f13cb77b3f076ddcc5d6ae3c20242bedcde"
|
||||||
url: "https://pub.dev"
|
url: "https://pub.dev"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "1.1.1"
|
version: "1.2.0"
|
||||||
term_glyph:
|
term_glyph:
|
||||||
dependency: transitive
|
dependency: transitive
|
||||||
description:
|
description:
|
||||||
@ -252,10 +252,10 @@ packages:
|
|||||||
dependency: transitive
|
dependency: transitive
|
||||||
description:
|
description:
|
||||||
name: test_api
|
name: test_api
|
||||||
sha256: c9aba3b3dbfe8878845dfab5fa096eb8de7b62231baeeb1cea8e3ee81ca8c6d8
|
sha256: ad540f65f92caa91bf21dfc8ffb8c589d6e4dc0c2267818b4cc2792857706206
|
||||||
url: "https://pub.dev"
|
url: "https://pub.dev"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "0.4.15"
|
version: "0.4.16"
|
||||||
typed_data:
|
typed_data:
|
||||||
dependency: transitive
|
dependency: transitive
|
||||||
description:
|
description:
|
||||||
|
Loading…
Reference in New Issue
Block a user