Created a basic 5x5 Grid in the UI that randomly changes Color on click

main
Lachfrosch 2024-05-08 15:57:00 +02:00
parent cb1e20e715
commit cfb929afac
1 changed files with 47 additions and 57 deletions

View File

@ -1,3 +1,4 @@
import 'dart:math';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
void main() { void main() {
@ -11,7 +12,7 @@ class MyApp extends StatelessWidget {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return MaterialApp( return MaterialApp(
title: 'Flutter Demo', title: 'Bubble-Twist',
theme: ThemeData( theme: ThemeData(
// This is the theme of your application. // This is the theme of your application.
// //
@ -55,71 +56,60 @@ class MyHomePage extends StatefulWidget {
} }
class _MyHomePageState extends State<MyHomePage> { class _MyHomePageState extends State<MyHomePage> {
int _counter = 0; List<List<int>> _grid = List.generate(5, (_) => List.generate(5, (_) => Random().nextInt(5)));
void _incrementCounter() {
setState(() {
// This call to setState tells the Flutter framework that something has
// changed in this State, which causes it to rerun the build method below
// so that the display can reflect the updated values. If we changed
// _counter without calling setState(), then the build method would not be
// called again, and so nothing would appear to happen.
_counter++;
});
}
@override @override
Widget build(BuildContext context) { 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( return Scaffold(
appBar: AppBar( appBar: AppBar(
// TRY THIS: Try changing the color here to a specific color (to title: Text('Candy Crush'),
// Colors.amber, perhaps?) and trigger a hot reload to see the AppBar
// change color while the other colors stay the same.
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
// 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: Center( body: Center(
// Center is a layout widget. It takes a single child and positions it child: GridView.builder(
// in the middle of the parent. shrinkWrap: true,
child: Column( gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
// Column is also a layout widget. It takes a list of children and crossAxisCount: 5,
// arranges them vertically. By default, it sizes itself to fit its ),
// children horizontally, and tries to be as tall as its parent. itemCount: _grid.length * _grid.length,
// itemBuilder: (BuildContext context, int index) {
// Column has various properties to control how it sizes itself and int row = index ~/ _grid.length;
// how it positions its children. Here we use mainAxisAlignment to int col = index % _grid.length;
// center the children vertically; the main axis here is the vertical return GestureDetector(
// axis because Columns are vertical (the cross axis would be onTap: () => _onTileTap(row, col),
// horizontal). child: Container(
// margin: EdgeInsets.all(4),
// TRY THIS: Invoke "debug painting" (choose the "Toggle Debug Paint" decoration: BoxDecoration(
// action in the IDE, or press "p" in the console), to see the color: _getColor(_grid[row][col]),
// wireframe for each widget. borderRadius: BorderRadius.circular(8),
mainAxisAlignment: MainAxisAlignment.center, ),
children: <Widget>[ ),
const Text( );
'You have pushed the button this many times:', },
),
Text(
'$_counter',
style: Theme.of(context).textTheme.headlineMedium,
),
],
), ),
), ),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: const Icon(Icons.add),
), // This trailing comma makes auto-formatting nicer for build methods.
); );
} }
Color _getColor(int value) {
switch (value) {
case 0:
return Colors.red;
case 1:
return Colors.blue;
case 2:
return Colors.green;
case 3:
return Colors.yellow;
case 4:
return Colors.orange;
default:
return Colors.white;
}
}
void _onTileTap(int row, int col) {
setState(() {
_grid[row][col] = Random().nextInt(5);
});
}
} }