beauty builders

This commit is contained in:
Nathan Anderson 2024-05-02 13:04:05 -06:00
parent 73bf1500f9
commit 55d6069b84

View File

@ -20,6 +20,68 @@ class MyApp extends StatelessWidget {
} }
} }
///
/// 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(),
];
class MyHomePage extends StatefulWidget { class MyHomePage extends StatefulWidget {
const MyHomePage({super.key}); const MyHomePage({super.key});
@ -33,18 +95,19 @@ class _MyHomePageState extends State<MyHomePage> {
return Scaffold( return Scaffold(
appBar: AppBar( appBar: AppBar(
backgroundColor: Theme.of(context).colorScheme.inversePrimary, backgroundColor: Theme.of(context).colorScheme.inversePrimary,
title: const Text('Abstract Factory'), title: const Text('Builders'),
), ),
body: Center( body: Center(
child: Column( child: Column(
mainAxisAlignment: MainAxisAlignment.center, mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[ children: <Widget>[
const Text( const Text('Builders!!'),
'You have pushed the button this many times:', ...List.generate(
), 3,
Text( (index) => builders[index].build(
'some text', Size(80.0 * (index + 1), 90.0 * (index + 1)),
style: Theme.of(context).textTheme.headlineMedium, writers[index].write('build ${index + 1}'),
),
), ),
], ],
), ),