Skip to content

Commit

Permalink
Ensure clock widgets don't loose state when changing orientation
Browse files Browse the repository at this point in the history
  • Loading branch information
veloce committed Oct 10, 2023
1 parent 83597d1 commit de4b17f
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
11 changes: 11 additions & 0 deletions lib/src/view/game/game_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ class GameScreen extends ConsumerStatefulWidget {

class _GameScreenState extends ConsumerState<GameScreen>
with AndroidImmersiveMode, RouteAware, Wakelock {
final _whiteClockKey = GlobalKey(debugLabel: 'whiteClockOnGameScreen');
final _blackClockKey = GlobalKey(debugLabel: 'blackClockOnGameScreen');

@override
void didChangeDependencies() {
super.didChangeDependencies();
Expand Down Expand Up @@ -90,6 +93,8 @@ class _GameScreenState extends ConsumerState<GameScreen>
gameState: state,
ctrlProvider: ctrlProvider,
gameProvider: gameProvider,
whiteClockKey: _whiteClockKey,
blackClockKey: _blackClockKey,
);
return PlatformWidget(
androidBuilder: (context) => _androidBuilder(
Expand Down Expand Up @@ -239,11 +244,15 @@ class _Body extends ConsumerWidget {
required this.gameState,
required this.ctrlProvider,
required this.gameProvider,
required this.whiteClockKey,
required this.blackClockKey,
});

final GameState gameState;
final GameControllerProvider ctrlProvider;
final LobbyGameProvider gameProvider;
final GlobalKey whiteClockKey;
final GlobalKey blackClockKey;

@override
Widget build(BuildContext context, WidgetRef ref) {
Expand Down Expand Up @@ -293,6 +302,7 @@ class _Body extends ConsumerWidget {
: null,
clock: gameState.game.clock != null
? CountdownClock(
key: blackClockKey,
duration: gameState.game.clock!.black,
active: gameState.activeClockSide == Side.black,
emergencyThreshold:
Expand Down Expand Up @@ -325,6 +335,7 @@ class _Body extends ConsumerWidget {
: null,
clock: gameState.game.clock != null
? CountdownClock(
key: whiteClockKey,
duration: gameState.game.clock!.white,
active: gameState.activeClockSide == Side.white,
emergencyThreshold:
Expand Down
28 changes: 25 additions & 3 deletions lib/src/view/watch/tv_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ class _TvScreenState extends ConsumerState<TvScreen>
TvControllerProvider get _tvGameCtrl =>
tvControllerProvider(widget.channel, widget.initialGame);

final _whiteClockKey = GlobalKey(debugLabel: 'whiteClockOnTvScreen');
final _blackClockKey = GlobalKey(debugLabel: 'blackClockOnTvScreen');

@override
Widget build(BuildContext context) {
return PlatformWidget(
Expand All @@ -49,7 +52,12 @@ class _TvScreenState extends ConsumerState<TvScreen>
ToggleSoundButton(),
],
),
body: _Body(widget.channel, widget.initialGame),
body: _Body(
widget.channel,
widget.initialGame,
whiteClockKey: _whiteClockKey,
blackClockKey: _blackClockKey,
),
);
}

Expand All @@ -61,7 +69,12 @@ class _TvScreenState extends ConsumerState<TvScreen>
middle: Text('${widget.channel.label} TV'),
trailing: ToggleSoundButton(),
),
child: _Body(widget.channel, widget.initialGame),
child: _Body(
widget.channel,
widget.initialGame,
whiteClockKey: _whiteClockKey,
blackClockKey: _blackClockKey,
),
);
}

Expand Down Expand Up @@ -116,10 +129,17 @@ class _TvScreenState extends ConsumerState<TvScreen>
}

class _Body extends ConsumerWidget {
const _Body(this.channel, this.initialGame);
const _Body(
this.channel,
this.initialGame, {
required this.whiteClockKey,
required this.blackClockKey,
});

final TvChannel channel;
final (GameId id, Side orientation)? initialGame;
final GlobalKey whiteClockKey;
final GlobalKey blackClockKey;

@override
Widget build(BuildContext context, WidgetRef ref) {
Expand All @@ -145,6 +165,7 @@ class _Body extends ConsumerWidget {
player: game.black.setOnGame(true),
clock: gameState.game.clock != null
? CountdownClock(
key: blackClockKey,
duration: gameState.game.clock!.black,
active: gameState.activeClockSide == Side.black,
)
Expand All @@ -155,6 +176,7 @@ class _Body extends ConsumerWidget {
player: game.white.setOnGame(true),
clock: gameState.game.clock != null
? CountdownClock(
key: whiteClockKey,
duration: gameState.game.clock!.white,
active: gameState.activeClockSide == Side.white,
)
Expand Down

0 comments on commit de4b17f

Please sign in to comment.