Change the color of the squares of the square grid
dart - I am trying to make a page with a square grid all over with small squares. When any square is touched, the color of that square should change to green. (for touch test) Can anyone help?
Like this :
import 'package:flutter/material.dart';
class MyPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return LayoutBuilder(
builder: (BuildContext context, BoxConstraints constraints) {
double screenWidth = constraints.maxWidth;
double screenHeight = constraints.maxHeight;
double boxSize =
screenWidth < screenHeight ? screenWidth / 10 : screenHeight / 10;
int columns = (screenWidth / boxSize).floor();
int rows = (screenHeight / boxSize).floor();
int totalBoxes = columns * rows;
return GestureDetector(
onTapDown: (TapDownDetails details) {
RenderBox box = context.findRenderObject() as RenderBox;
Offset localPosition = box.globalToLocal(details.globalPosition);
int tappedIndex = (localPosition.dx ~/ boxSize) +
(localPosition.dy ~/ boxSize) * columns;
if (tappedIndex >= 0 && tappedIndex < totalBoxes) {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => DetailPage(),
),
);
}
},
child: GridView.count(
crossAxisCount: columns,
children: List.generate(
totalBoxes,
(index) => Container(
decoration: BoxDecoration(
color: Colors.blue,
border: Border.all(color: Colors.white),
),
),
),
),
);
},
);
}
}
class DetailPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Detail Page'),
),
body: Center(
child: Text('Detail Page Content'),
),
);
}
}
In this program, by touching each square, it will be return to the 'DetailPage' I just want the color of the touched square to change.