我正在创建一个洗牌游戏,为此,我设置了3个级别的网格,
我拿了一个300x300的固定大小的容器,我不想改变它的大小,
在这里,我想要把所有的网格按钮正确地放在一个网格内,但我在网格视图的顶部获得了空间,因此,按钮显示得不合适.
以下是我的代码
final kdecoration = BoxDecoration(
border: Border.all(color: Colors.yellow, width: 1),
borderRadius: BorderRadius.circular(5),
color: Colors.red);
class HomeScreen extends StatefulWidget {
const HomeScreen({Key? key}) : super(key: key);
@override
State<HomeScreen> createState() => _HomeScreenState();
}
class _HomeScreenState extends State<HomeScreen> {
double w = 300;
double h = 300;
int gridvalue = 3;
@override
Widget build(BuildContext context) {
return Scaffold(
body: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Center(
child: Text(
'$gridvalue x $gridvalue',
style: TextStyle(fontSize: 30, color: Colors.blue),
)),
SizedBox(
height: 20,
),
Container(
color: Colors.blue,
height: h,
width: w,
child: GridView.builder(
physics: NeverScrollableScrollPhysics(),
itemCount: gridvalue * gridvalue,
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
mainAxisSpacing: 0,
crossAxisSpacing: 0,
crossAxisCount: gridvalue,
),
itemBuilder: (context, index) {
return Container(
decoration: kdecoration,
child: Center(
child: Text(
(index + 1).toString(),
style: TextStyle(fontSize: 20, color: Colors.white),
)),
//color: Colors.green,
);
})),
SizedBox(
height: 20,
),
Container(
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
ElevatedButton(
onPressed: () {
setState(() {
gridvalue = 3;
});
},
child: Text('3x3')),
ElevatedButton(
onPressed: () {
setState(() {
gridvalue = 4;
});
},
child: Text('4x4')),
ElevatedButton(
onPressed: () {
setState(() {
gridvalue = 5;
});
},
child: Text('5x5')),
],
),
)
],
),
);
}
}