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 12:02:08 -06:00
|
|
|
abstract class ShapeFactory {
|
|
|
|
Widget build(Size size, Color color);
|
|
|
|
}
|
|
|
|
|
|
|
|
class SquareFactory extends ShapeFactory {
|
|
|
|
@override
|
|
|
|
Widget build(Size size, Color color) {
|
|
|
|
return SizedBox.fromSize(
|
|
|
|
size: size,
|
|
|
|
child: Container(color: color),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class CircleFactory extends ShapeFactory {
|
|
|
|
@override
|
|
|
|
Widget build(Size size, Color color) {
|
|
|
|
return SizedBox.fromSize(
|
|
|
|
size: size,
|
|
|
|
child: Container(
|
|
|
|
decoration: BoxDecoration(shape: BoxShape.circle, color: color),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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> {
|
2024-05-02 12:02:08 -06:00
|
|
|
final List<ShapeFactory> shapeFactories = [CircleFactory(), SquareFactory()];
|
|
|
|
int factoryIndex = 0;
|
|
|
|
ShapeFactory get factory => shapeFactories[factoryIndex];
|
|
|
|
|
|
|
|
void swapFactories() {
|
|
|
|
if (factoryIndex == 0) {
|
|
|
|
setState(() => factoryIndex = 1);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
setState(() => factoryIndex = 0);
|
|
|
|
}
|
|
|
|
|
2024-05-02 11:41:57 -06:00
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return Scaffold(
|
|
|
|
appBar: AppBar(
|
|
|
|
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
|
2024-05-02 11:43:16 -06:00
|
|
|
title: const Text('Abstract Factory'),
|
2024-05-02 11:41:57 -06:00
|
|
|
),
|
|
|
|
body: Center(
|
|
|
|
child: Column(
|
|
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
|
|
children: <Widget>[
|
2024-05-02 12:02:08 -06:00
|
|
|
const Text('Abstract Factories!!'),
|
|
|
|
Text('Using ${shapeFactories[factoryIndex].runtimeType.toString()}'),
|
|
|
|
...List.generate(
|
|
|
|
3,
|
|
|
|
(index) => factory.build(
|
|
|
|
Size(20.0 * (index + 1), 30.0 * (index + 1)),
|
|
|
|
[Colors.red, Colors.green, Colors.blue][index],
|
|
|
|
)),
|
|
|
|
ElevatedButton(
|
|
|
|
onPressed: () => swapFactories(),
|
|
|
|
child: const Text('Swap Factories'),
|
2024-05-02 11:41:57 -06:00
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|