【发布时间】:2021-11-25 18:09:35
【问题描述】:
我正在尝试通过编写数独游戏来自学 Flutter/Dart。
我的计划是用一个名为“Square”的对象来表示数独网格上 81 个方格中的每一个。
每个 Square 都会管理自己的状态,以显示用户输入和记住状态。到目前为止,我已经使用 StatefulWidget 对此进行了编程。
我卡住的地方是,还需要有一个顶级的游戏逻辑,它可以概览所有方块正在发生的事情并处理交互。这意味着我需要能够查询顶层的方块以了解它们的状态。
有没有办法用 Flutter 做到这一点?还是我需要使用其他结构?
下面是我当前的 Square 实现的副本。
import 'package:flutter/material.dart';
import 'package:sudoku_total/logical_board.dart';
class Square extends StatefulWidget {
final int squareIndex;
final int boxIndex;
final int rowIndex;
final int colIndex;
const Square(
this.squareIndex,
this.boxIndex,
this.rowIndex,
this.colIndex, {
Key? key,
}) : super(key: key);
@override
State<StatefulWidget> createState() => _SquareState();
}
class _SquareState extends State<Square> {
int _mainNumber = 0;
bool showEdit1 = false;
bool showEdit2 = false;
bool showEdit3 = false;
bool showEdit4 = false;
bool showEdit5 = false;
bool showEdit6 = false;
bool showEdit7 = false;
bool showEdit8 = false;
bool showEdit9 = false;
bool _selected = false;
bool _selectedCollection = false;
@override
Widget build(BuildContext context) {
LogicalBoard.numberButtonNotifier.addListener(() {
if(LogicalBoard.selectedSquare?.squareIndex == widget.squareIndex){
setState(() {
_mainNumber = LogicalBoard.numberLastClicked;
});
}
});
LogicalBoard.selectionNotifier.addListener(() {
setState(() {
_selected =
LogicalBoard.selectedSquare?.squareIndex == widget.squareIndex;
_selectedCollection =
LogicalBoard.selectedSquare?.squareIndex == widget.squareIndex ||
LogicalBoard.selectedSquare?.rowIndex == widget.rowIndex ||
LogicalBoard.selectedSquare?.colIndex == widget.colIndex ||
LogicalBoard.selectedSquare?.boxIndex == widget.boxIndex;
});
});
return Material(
child: InkWell(
onTap: () => LogicalBoard.selectionNotifier
.setSelectedSquare(widget.squareIndex),
child: Container(
padding: const EdgeInsets.all(2.0),
color: _selected
? Theme.of(context).errorColor
: Theme.of(context).primaryColorDark,
width: 52.0,
height: 52.0,
child: Container(
padding: _mainNumber == 0
? const EdgeInsets.fromLTRB(2.0, 8.0, 2.0, 2.0)
: const EdgeInsets.all(0.0),
color: _selectedCollection
? Theme.of(context).backgroundColor
: Theme.of(context).primaryColor,
width: 48.0,
height: 48.0,
child: _mainNumber != 0
? Center(
child: Text(
_mainNumber.toString(),
style: Theme.of(context).textTheme.headline3,
))
: Column(mainAxisSize: MainAxisSize.min, children: [
Flexible(
child: Row(
mainAxisSize: MainAxisSize.min,
children: [
Flexible(
child: Text(
showEdit1 ? "1" : "",
style: Theme.of(context).textTheme.bodyText2,
)),
Flexible(
child: Text(
showEdit2 ? "2" : "",
style: Theme.of(context).textTheme.bodyText2,
)),
Flexible(
child: Text(
showEdit3 ? "3" : "",
style: Theme.of(context).textTheme.bodyText2,
))
])),
Flexible(
child: Row(
mainAxisSize: MainAxisSize.min,
children: [
Flexible(
child: Text(
showEdit4 ? "4" : "",
style: Theme.of(context).textTheme.bodyText2,
)),
Flexible(
child: Text(
showEdit5 ? "5" : "",
style: Theme.of(context).textTheme.bodyText2,
)),
Flexible(
child: Text(
showEdit6 ? "6" : "",
style: Theme.of(context).textTheme.bodyText2,
))
])),
Flexible(
child: Row(
mainAxisSize: MainAxisSize.min,
children: [
Flexible(
child: Text(
showEdit7 ? "7" : "",
style: Theme.of(context).textTheme.bodyText2,
)),
Flexible(
child: Text(
showEdit8 ? "8" : "",
style: Theme.of(context).textTheme.bodyText2,
)),
Flexible(
child: Text(
showEdit9 ? "9" : "",
style: Theme.of(context).textTheme.bodyText2,
))
]))
])),
)));
}
}
感谢您的帮助。
【问题讨论】: