我正在编写一个由gridview中的100个按钮组成的迷宫应用程序。我想在按下任何按钮时将所有按钮的颜色更改为绿色。另外,是否可以在按下一个按钮时更改其他按钮的颜色(即,对其进行编码,以便在按下左上方的按钮时右下方的按钮可以更改颜色)?与我的问题相关的演示代码如下。
import 'dart:async';
import 'package:flutter/material.dart';
Color color=Colors.grey;
void main() {
maze maze1=new maze();
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class gameButton extends StatefulWidget {
@override
gameButtonState createState() => gameButtonState();
}
class gameButtonState extends State<gameButton> {
@override
Widget build(BuildContext context) {
return Container(
decoration: BoxDecoration(border: Border.all(color: Colors.black)),
child: FlatButton(
color: color,
materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
onPressed: () {
setState(() {
color=Colors.green;
});
},
),
);
}
}
class maze extends StatefulWidget {
List<gameButton> empty_grid = [
for (var i = 0; i < 100; i++) new gameButton()
];
@override
mazeState createState() => mazeState();
}
class mazeState extends State<maze> {
@override
Widget build(BuildContext context) {
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
maze maze1=new maze();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Demo Code"),
),
body: Center(
child:Column(
children: <Widget>[
GridView.builder(
itemCount: 100,
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 10,
crossAxisSpacing: 0,
mainAxisSpacing: 0),
shrinkWrap: true,
itemBuilder: (BuildContext context, int index) {
return maze1.empty_grid[index];
},
),
],
)
),
);
}
}