Skip to content

Instantly share code, notes, and snippets.

@wolkenschieber
Created July 29, 2022 19:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wolkenschieber/23747bf35af0b93db066c34b98b60f24 to your computer and use it in GitHub Desktop.
Save wolkenschieber/23747bf35af0b93db066c34b98b60f24 to your computer and use it in GitHub Desktop.
Flutter puzzle (I)
import 'dart:math';
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key, required this.title}) : super(key: key);
final String title;
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
late int _counter;
late bool _reversed;
@override
void initState() {
super.initState();
_counter = 0;
_reversed = false;
}
void _incrementCounter() {
setState(() {
_counter++;
});
}
void _decrementCounter() {
setState(() {
_counter--;
});
}
void _resetCounter() {
setState(() {
_counter = 0;
_swap();
});
}
void _swap() {
setState(() {
_reversed = !_reversed;
});
}
@override
Widget build(BuildContext context) {
final incrementButton = FancyButton(
onPressed: () => _incrementCounter(), child: const Text("Increment"));
final decrementButton = FancyButton(
onPressed: _decrementCounter, child: const Text("Decrement"));
List<Widget> buttons = <Widget>[incrementButton, decrementButton];
if (_reversed) {
buttons = buttons.reversed.toList();
}
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
const Text(
'You have pushed the button this many times:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.headline4,
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
crossAxisAlignment: CrossAxisAlignment.center,
children: buttons)
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _resetCounter,
tooltip: 'Reset',
child: const Icon(Icons.refresh),
),
);
}
}
class FancyButton extends StatefulWidget {
final VoidCallback onPressed;
final Widget child;
const FancyButton({Key? key, required this.onPressed, required this.child})
: super(key: key);
@override
State<FancyButton> createState() => _FancyButtonState();
}
class _FancyButtonState extends State<FancyButton> {
@override
Widget build(BuildContext context) {
return ElevatedButton(
style: ElevatedButton.styleFrom(primary: _getColors()),
onPressed: widget.onPressed,
child: widget.child,
);
}
Color _getColors() {
return _buttonColors.putIfAbsent(
this, () => colors[next(0, colors.length - 1)]);
}
final Map<_FancyButtonState, Color> _buttonColors = {};
final _random = Random();
int next(int min, int max) => min + _random.nextInt(max - min);
List<Color> colors = [
Colors.blue,
Colors.green,
Colors.orange,
Colors.purple,
Colors.amber,
Colors.lightBlue,
Colors.teal,
Colors.yellow,
Colors.deepOrange,
Colors.greenAccent,
];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment