问题描述
我是 Flutter 的新手,我在 Flutter 中使用 gridview 来创建棋盘 (7x7) 游戏。我在一些单元格中有图像,这些图像随着播放的移动而变化。我希望图像与单元格大小相同,而不是更小。该图像是方形图像,应与单元格大小相同。如何实现这一目标?
这是我当前的代码:
main class:
import 'package:Flutter/material.dart';
import 'BoardField.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',theme: ThemeData(
// This is the theme of your application.
//
// Try running your application with "Flutter run". You'll see the
// application has a blue toolbar. Then,without quitting the app,try
// changing the primarySwatch below to Colors.green and then invoke
// "hot reload" (press "r" in the console where you ran "Flutter run",// or simply save your changes to "hot reload" in a Flutter IDE).
// Notice that the counter didn't reset back to zero; the application
// is not restarted.
primarySwatch: Colors.blue,),home: MyHomePage(title: 'Flutter Demo Home Page'),);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key? key,required this.title}) : super(key: key);
// This widget is the home page of your application. It is stateful,meaning
// that it has a State object (defined below) that contains fields that affect
// how it looks.
// This class is the configuration for the state. It holds the values (in this
// case the title) provided by the parent (in this case the App widget) and
// used by the build method of the State. Fields in a Widget subclass are
// always marked "final".
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
final List<List<int>> gridState = [
[1,2],[0,-1,0],[2,1]
];
int selectedindex = -1;
@override
Widget build(BuildContext context) {
// This method is rerun every time setState is called,for instance as done
// by the _incrementCounter method above.
//
// The Flutter framework has been optimized to make rerunning build methods
// fast,so that you can just rebuild anything that needs updating rather
// than having to individually change instances of widgets.
return Scaffold(
appBar: AppBar(
// Here we take the value from the MyHomePage object that was created by
// the App.build method,and use it to set our appbar title.
title: Text(widget.title),body: Container(
child: GridView.count(
physics: NeverScrollableScrollPhysics(),childAspectRatio: 1,crossAxisCount: 7,children: List.generate(49,(index) {
return BoardField(
gridState: gridState,index: index,onButtonpressed: () {
setState(() {
int x = (index / gridState.first.length).floor();
int y = (index % gridState.first.length);
if (gridState[x][y] == 1 || gridState[x][y] == 2) {
if (selectedindex != -1) {
gridState[(selectedindex / gridState.first.length).floor()]
[(selectedindex % gridState.first.length)] -= 2;
}
selectedindex = index;
gridState[x][y] += 2;
}
});
},);
}),)),);
}
}
Second Class:
import 'package:Flutter/material.dart';
class BoardField extends StatelessWidget {
BoardField(
{Key? key,required this.gridState,required this.index,required this.onButtonpressed})
: super(key: key);
final List<List<int>> gridState;
final int index;
final VoidCallback onButtonpressed;
@override
Widget build(BuildContext context) {
int x,y = 0;
x = (index / gridState.first.length).floor();
y = (index % gridState.first.length);
bool dark = gridState[x][y] == -1;
return Container(
decoration: Boxdecoration(
border: Border.all(color: Colors.black,width: 0.5),color: dark ? Colors.black : Colors.white),child: Center(
child: BoardItem(
gridState: gridState,itemIdentifier: gridState[x][y],onButtonpressed: onButtonpressed,);
}
}
class BoardItem extends StatelessWidget {
BoardItem(
{Key? key,required this.itemIdentifier,required this.onButtonpressed})
: super(key: key);
final List<List<int>> gridState;
final int itemIdentifier;
final VoidCallback onButtonpressed;
@override
Widget build(BuildContext context) {
switch (itemIdentifier) {
case 1:
return TextButton(
onpressed: onButtonpressed,child: Image.asset(
'images/Red Sphere.jpg',));
case 2:
return TextButton(
onpressed: onButtonpressed,child: Image.asset(
'images/Blue Sphere.jpg',));
case 3:
return TextButton(
onpressed: () {},child: Image.asset(
'images/Red Sphere_highlight.jpg',));
case 4:
return TextButton(
onpressed: () {},child: Image.asset(
'images/Blue Sphere_highlight.jpg',));
default:
return Container(width: 0.0,height: 0.0);
}
}
}
谢谢。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)