Skip to content

Instantly share code, notes, and snippets.

@your-diary
Created May 10, 2023 10:32
Show Gist options
  • Save your-diary/76bb282e5af3c2ed2abb3ac260319e93 to your computer and use it in GitHub Desktop.
Save your-diary/76bb282e5af3c2ed2abb3ac260319e93 to your computer and use it in GitHub Desktop.
import 'dart:math';
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
theme:
ThemeData(brightness: Brightness.dark, primaryColor: Colors.blueGrey),
home: W(),
debugShowCheckedModeBanner: false,
);
}
}
class W extends StatefulWidget {
const W({
super.key,
});
@override
State<W> createState() => _WState();
}
class _WState extends State<W> {
List<Tile> l = [Tile(text: "hello"), Tile(text: "world")];
// List<ColorCard> l = [ColorCard(), ColorCard()];
@override
Widget build(BuildContext context) {
return Scaffold(
floatingActionButton: FloatingActionButton(
onPressed: () =>
setState(() => {this.l.insert(1, this.l.removeAt(0))}),
child: Icon(Icons.swap_horiz)),
body: Center(
child: Row(
children: this.l,
)));
}
}
class Tile extends StatelessWidget {
final String text;
const Tile({required String this.text, super.key});
@override
Widget build(BuildContext context) {
return Stack(children: [
ColorCard(),
Text(this.text),
]);
}
}
Color createRandomColor() {
final colors = [
Colors.red,
Colors.blue,
Colors.yellow,
Colors.green,
Colors.purple,
];
return colors[Random().nextInt(colors.length)];
}
class ColorCard extends StatelessWidget {
final color = createRandomColor();
ColorCard({super.key});
@override
Widget build(BuildContext context) {
return Container(
width: 100,
height: 100,
color: this.color,
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment