flutter: Refactor game_page.dart

This commit is contained in:
Calcitem 2021-05-17 21:28:19 +08:00
parent bf2d7131e1
commit 0bb4bfb762
1 changed files with 76 additions and 66 deletions

View File

@ -433,7 +433,7 @@ class _GamePageState extends State<GamePage> with RouteAware {
} }
} }
newGame() { onGameButtonPressed() {
confirm() { confirm() {
Navigator.of(context).pop(); Navigator.of(context).pop();
Game.instance.newGame(); Game.instance.newGame();
@ -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 getGameOverReasonString(GameOverReason? reason, String? winner) {
//String winnerStr = //String winnerStr =
// winner == Color.white ? S.of(context).white : S.of(context).black; // winner == Color.white ? S.of(context).white : S.of(context).black;
@ -800,11 +865,7 @@ class _GamePageState extends State<GamePage> with RouteAware {
} }
Widget createToolbar() { Widget createToolbar() {
final moveHistoryText = Game.instance.position.moveHistoryText; var gameButton = TextButton(
final analyzeText = getInfoText();
var newGameButton = TextButton(
child: Column( child: Column(
// Replace with a Row for horizontal icon + text // Replace with a Row for horizontal icon + text
children: <Widget>[ children: <Widget>[
@ -816,10 +877,10 @@ class _GamePageState extends State<GamePage> with RouteAware {
style: TextStyle(color: AppTheme.toolbarTextColor)), style: TextStyle(color: AppTheme.toolbarTextColor)),
], ],
), ),
onPressed: newGame, onPressed: onGameButtonPressed,
); );
var undoButton = TextButton( var optionsButton = TextButton(
child: Column( child: Column(
// Replace with a Row for horizontal icon + text // Replace with a Row for horizontal icon + text
children: <Widget>[ children: <Widget>[
@ -831,15 +892,10 @@ class _GamePageState extends State<GamePage> with RouteAware {
style: TextStyle(color: AppTheme.toolbarTextColor)), style: TextStyle(color: AppTheme.toolbarTextColor)),
], ],
), ),
onPressed: () { onPressed: onOptionButtonPressed,
Navigator.push(
context,
MaterialPageRoute(builder: (context) => GameSettingsPage()),
);
},
); );
var moveHistoryButton = TextButton( var moveButton = TextButton(
child: Column( child: Column(
// Replace with a Row for horizontal icon + text // Replace with a Row for horizontal icon + text
children: <Widget>[ children: <Widget>[
@ -851,36 +907,7 @@ class _GamePageState extends State<GamePage> with RouteAware {
style: TextStyle(color: AppTheme.toolbarTextColor)), style: TextStyle(color: AppTheme.toolbarTextColor)),
], ],
), ),
onPressed: () => showDialog( onPressed: onMoveButtonPressed,
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(),
),
],
);
},
),
); );
var infoButton = TextButton( var infoButton = TextButton(
@ -895,24 +922,7 @@ class _GamePageState extends State<GamePage> with RouteAware {
style: TextStyle(color: AppTheme.toolbarTextColor)), style: TextStyle(color: AppTheme.toolbarTextColor)),
], ],
), ),
onPressed: () => showDialog( onPressed: onInfoButtonPressed,
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(),
),
],
);
},
),
); );
return Container( return Container(
@ -924,11 +934,11 @@ class _GamePageState extends State<GamePage> with RouteAware {
padding: EdgeInsets.symmetric(vertical: 2), padding: EdgeInsets.symmetric(vertical: 2),
child: Row(children: <Widget>[ child: Row(children: <Widget>[
Expanded(child: SizedBox()), Expanded(child: SizedBox()),
newGameButton, gameButton,
Expanded(child: SizedBox()), Expanded(child: SizedBox()),
undoButton, optionsButton,
Expanded(child: SizedBox()), Expanded(child: SizedBox()),
moveHistoryButton, moveButton,
Expanded(child: SizedBox()), //dashboard_outlined Expanded(child: SizedBox()), //dashboard_outlined
infoButton, infoButton,
Expanded(child: SizedBox()), Expanded(child: SizedBox()),