Skip to content

Commit

Permalink
flutter: Refactor game_page.dart
Browse files Browse the repository at this point in the history
  • Loading branch information
calcitem committed May 17, 2021
1 parent bf2d713 commit 0bb4bfb
Showing 1 changed file with 76 additions and 66 deletions.
142 changes: 76 additions & 66 deletions src/ui/flutter_app/lib/widgets/game_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,7 @@ class _GamePageState extends State<GamePage> with RouteAware {
}
}

newGame() {
onGameButtonPressed() {
confirm() {
Navigator.of(context).pop();
Game.instance.newGame();
Expand Down Expand Up @@ -464,6 +464,71 @@ class _GamePageState extends State<GamePage> with RouteAware {
);
}

onOptionButtonPressed() {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => GameSettingsPage()),
);
}

onMoveButtonPressed() {
final moveHistoryText = Game.instance.position.moveHistoryText;

showDialog(
context: context,
barrierDismissible: true,
builder: (BuildContext context) {
return AlertDialog(
backgroundColor: AppTheme.moveHistoryDialogBackgroundColor,
title: Text(S.of(context).moveList,
style: TextStyle(color: AppTheme.moveHistoryTextColor)),
content: SingleChildScrollView(
child:
Text(moveHistoryText, style: AppTheme.moveHistoryTextStyle)),
actions: <Widget>[
TextButton(
child: Text(S.of(context).copy,
style: AppTheme.moveHistoryTextStyle),
onPressed: () =>
Clipboard.setData(ClipboardData(text: moveHistoryText))
.then((_) {
showSnackBar(S.of(context).moveHistoryCopied);
}),
),
TextButton(
child: Text(S.of(context).cancel,
style: AppTheme.moveHistoryTextStyle),
onPressed: () => Navigator.of(context).pop(),
),
],
);
},
);
}

onInfoButtonPressed() {
final analyzeText = getInfoText();

showDialog(
context: context,
barrierDismissible: true,
builder: (BuildContext context) {
return AlertDialog(
backgroundColor: AppTheme.infoDialogackgroundColor,
content: SingleChildScrollView(
child: Text(analyzeText, style: AppTheme.moveHistoryTextStyle)),
actions: <Widget>[
TextButton(
child:
Text(S.of(context).ok, style: AppTheme.moveHistoryTextStyle),
onPressed: () => Navigator.of(context).pop(),
),
],
);
},
);
}

String getGameOverReasonString(GameOverReason? reason, String? winner) {
//String winnerStr =
// winner == Color.white ? S.of(context).white : S.of(context).black;
Expand Down Expand Up @@ -800,11 +865,7 @@ class _GamePageState extends State<GamePage> with RouteAware {
}

Widget createToolbar() {
final moveHistoryText = Game.instance.position.moveHistoryText;

final analyzeText = getInfoText();

var newGameButton = TextButton(
var gameButton = TextButton(
child: Column(
// Replace with a Row for horizontal icon + text
children: <Widget>[
Expand All @@ -816,10 +877,10 @@ class _GamePageState extends State<GamePage> with RouteAware {
style: TextStyle(color: AppTheme.toolbarTextColor)),
],
),
onPressed: newGame,
onPressed: onGameButtonPressed,
);

var undoButton = TextButton(
var optionsButton = TextButton(
child: Column(
// Replace with a Row for horizontal icon + text
children: <Widget>[
Expand All @@ -831,15 +892,10 @@ class _GamePageState extends State<GamePage> with RouteAware {
style: TextStyle(color: AppTheme.toolbarTextColor)),
],
),
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => GameSettingsPage()),
);
},
onPressed: onOptionButtonPressed,
);

var moveHistoryButton = TextButton(
var moveButton = TextButton(
child: Column(
// Replace with a Row for horizontal icon + text
children: <Widget>[
Expand All @@ -851,36 +907,7 @@ class _GamePageState extends State<GamePage> with RouteAware {
style: TextStyle(color: AppTheme.toolbarTextColor)),
],
),
onPressed: () => showDialog(
context: context,
barrierDismissible: true,
builder: (BuildContext context) {
return AlertDialog(
backgroundColor: AppTheme.moveHistoryDialogBackgroundColor,
title: Text(S.of(context).moveList,
style: TextStyle(color: AppTheme.moveHistoryTextColor)),
content: SingleChildScrollView(
child: Text(moveHistoryText,
style: AppTheme.moveHistoryTextStyle)),
actions: <Widget>[
TextButton(
child: Text(S.of(context).copy,
style: AppTheme.moveHistoryTextStyle),
onPressed: () =>
Clipboard.setData(ClipboardData(text: moveHistoryText))
.then((_) {
showSnackBar(S.of(context).moveHistoryCopied);
}),
),
TextButton(
child: Text(S.of(context).cancel,
style: AppTheme.moveHistoryTextStyle),
onPressed: () => Navigator.of(context).pop(),
),
],
);
},
),
onPressed: onMoveButtonPressed,
);

var infoButton = TextButton(
Expand All @@ -895,24 +922,7 @@ class _GamePageState extends State<GamePage> with RouteAware {
style: TextStyle(color: AppTheme.toolbarTextColor)),
],
),
onPressed: () => showDialog(
context: context,
barrierDismissible: true,
builder: (BuildContext context) {
return AlertDialog(
backgroundColor: AppTheme.infoDialogackgroundColor,
content: SingleChildScrollView(
child: Text(analyzeText, style: AppTheme.moveHistoryTextStyle)),
actions: <Widget>[
TextButton(
child: Text(S.of(context).ok,
style: AppTheme.moveHistoryTextStyle),
onPressed: () => Navigator.of(context).pop(),
),
],
);
},
),
onPressed: onInfoButtonPressed,
);

return Container(
Expand All @@ -924,11 +934,11 @@ class _GamePageState extends State<GamePage> with RouteAware {
padding: EdgeInsets.symmetric(vertical: 2),
child: Row(children: <Widget>[
Expanded(child: SizedBox()),
newGameButton,
gameButton,
Expanded(child: SizedBox()),
undoButton,
optionsButton,
Expanded(child: SizedBox()),
moveHistoryButton,
moveButton,
Expanded(child: SizedBox()), //dashboard_outlined
infoButton,
Expanded(child: SizedBox()),
Expand Down

0 comments on commit 0bb4bfb

Please sign in to comment.