Compare commits
1 Commits
creation_p
...
strutural_
Author | SHA1 | Date | |
---|---|---|---|
c0d16da33c |
132
lib/main.dart
132
lib/main.dart
|
@ -20,68 +20,83 @@ 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),
|
||||
class LikeButtonDecorator extends StatelessWidget {
|
||||
const LikeButtonDecorator({super.key, required this.child, required this.height, required this.width});
|
||||
|
||||
final Widget child;
|
||||
final double height, width;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return SizedBox(
|
||||
height: height,
|
||||
width: width,
|
||||
child: Stack(
|
||||
children: [
|
||||
Center(child: child),
|
||||
Padding(
|
||||
padding: const EdgeInsets.all(2.0),
|
||||
child: Align(
|
||||
alignment: Alignment.topRight,
|
||||
child: Container(
|
||||
padding: const EdgeInsets.all(3.0),
|
||||
decoration: const BoxDecoration(shape: BoxShape.circle, color: Colors.blue),
|
||||
child: const Icon(Icons.thumb_up, color: Colors.white, size: 16),
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
BoxShape get boxShape;
|
||||
Color get shapeColor;
|
||||
}
|
||||
|
||||
class RedSquareBuilder extends ShapeBuilder {
|
||||
@override
|
||||
BoxShape get boxShape => BoxShape.rectangle;
|
||||
class AnimatedPulsingBorderDecorator extends StatefulWidget {
|
||||
const AnimatedPulsingBorderDecorator({super.key, required this.child});
|
||||
|
||||
final Widget child;
|
||||
|
||||
@override
|
||||
Color get shapeColor => Colors.red;
|
||||
State<StatefulWidget> createState() => AnimatedPulsingBorderDecoratorState();
|
||||
}
|
||||
|
||||
class BlueCircleBuilder extends ShapeBuilder {
|
||||
@override
|
||||
BoxShape get boxShape => BoxShape.circle;
|
||||
class AnimatedPulsingBorderDecoratorState extends State<AnimatedPulsingBorderDecorator>
|
||||
with SingleTickerProviderStateMixin {
|
||||
late final AnimationController controller;
|
||||
late final Animation<double> paddingAnimation;
|
||||
|
||||
@override
|
||||
Color get shapeColor => Colors.blue;
|
||||
}
|
||||
void initState() {
|
||||
super.initState();
|
||||
controller = AnimationController(
|
||||
duration: const Duration(milliseconds: 750),
|
||||
vsync: this,
|
||||
);
|
||||
paddingAnimation = Tween<double>(begin: 0.0, end: 3.0).animate(controller);
|
||||
controller.repeat(reverse: true);
|
||||
}
|
||||
|
||||
///
|
||||
/// 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));
|
||||
}
|
||||
void dispose() {
|
||||
super.dispose();
|
||||
controller.dispose();
|
||||
}
|
||||
|
||||
class ColdMessageWriter extends MessageWriter {
|
||||
@override
|
||||
Text write(String text) => Text(text, style: const TextStyle(color: Colors.white, fontSize: 14));
|
||||
Widget build(BuildContext context) {
|
||||
return AnimatedBuilder(
|
||||
builder: (context, _) {
|
||||
return Container(
|
||||
padding: EdgeInsets.all(paddingAnimation.value),
|
||||
decoration: BoxDecoration(color: Colors.amber, borderRadius: BorderRadius.circular(15.0)),
|
||||
child: widget.child,
|
||||
);
|
||||
},
|
||||
animation: paddingAnimation,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
final List<ShapeBuilder> builders = [
|
||||
RedSquareBuilder(),
|
||||
BlueCircleBuilder(),
|
||||
RedSquareBuilder(),
|
||||
];
|
||||
|
||||
final List<MessageWriter> writers = [
|
||||
ColdMessageWriter(),
|
||||
HotMessageWriter(),
|
||||
HotMessageWriter(),
|
||||
];
|
||||
|
||||
class MyHomePage extends StatefulWidget {
|
||||
const MyHomePage({super.key});
|
||||
|
||||
|
@ -95,20 +110,29 @@ class _MyHomePageState extends State<MyHomePage> {
|
|||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
|
||||
title: const Text('Builders'),
|
||||
title: const Text('Decorator'),
|
||||
),
|
||||
body: Center(
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: <Widget>[
|
||||
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}'),
|
||||
),
|
||||
const Text(
|
||||
'Decorators!!',
|
||||
),
|
||||
LikeButtonDecorator(
|
||||
height: 30,
|
||||
width: 160,
|
||||
child: ElevatedButton(
|
||||
onPressed: () {},
|
||||
child: const SizedBox(
|
||||
height: 30,
|
||||
width: 160,
|
||||
child: Center(child: Text('Like Me!')),
|
||||
),
|
||||
)),
|
||||
const SizedBox(height: 20),
|
||||
AnimatedPulsingBorderDecorator(
|
||||
child: ElevatedButton(onPressed: () {}, child: const Text('Pulsing goodness!!')))
|
||||
],
|
||||
),
|
||||
),
|
||||
|
|
Loading…
Reference in New Issue
Block a user