flutter_design_patterns/lib/main.dart

118 lines
2.6 KiB
Dart
Raw Permalink Normal View History

2024-05-02 11:41:57 -06:00
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(
title: 'Flutter Demo',
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
useMaterial3: true,
),
2024-05-02 11:43:16 -06:00
home: const MyHomePage(),
2024-05-02 11:41:57 -06:00
);
}
}
2024-05-02 13:04:05 -06:00
///
/// Shape Builder
///
abstract class ShapeBuilder {
Widget build(Size size, Widget child) {
return Container(
height: size.height,
width: size.width,
decoration: BoxDecoration(shape: boxShape, color: shapeColor),
child: Center(child: child),
);
}
BoxShape get boxShape;
Color get shapeColor;
}
class RedSquareBuilder extends ShapeBuilder {
@override
BoxShape get boxShape => BoxShape.rectangle;
@override
Color get shapeColor => Colors.red;
}
class BlueCircleBuilder extends ShapeBuilder {
@override
BoxShape get boxShape => BoxShape.circle;
@override
Color get shapeColor => Colors.blue;
}
///
/// Message Builder (Writer seemed appropriate)
///
abstract class MessageWriter {
Text write(String text);
}
class HotMessageWriter extends MessageWriter {
@override
Text write(String text) => Text(text, style: const TextStyle(fontWeight: FontWeight.bold, fontSize: 18));
}
class ColdMessageWriter extends MessageWriter {
@override
Text write(String text) => Text(text, style: const TextStyle(color: Colors.white, fontSize: 14));
}
final List<ShapeBuilder> builders = [
RedSquareBuilder(),
BlueCircleBuilder(),
RedSquareBuilder(),
];
final List<MessageWriter> writers = [
ColdMessageWriter(),
HotMessageWriter(),
HotMessageWriter(),
];
2024-05-02 11:41:57 -06:00
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key});
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
2024-05-02 13:04:05 -06:00
title: const Text('Builders'),
2024-05-02 11:41:57 -06:00
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
2024-05-02 13:04:05 -06:00
const Text('Builders!!'),
...List.generate(
3,
(index) => builders[index].build(
Size(80.0 * (index + 1), 90.0 * (index + 1)),
writers[index].write('build ${index + 1}'),
),
2024-05-02 11:41:57 -06:00
),
],
),
),
);
}
}