From 7b9f73296091f338bd0065baf39fa7afd1e2c2ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Malo=20L=C3=A9on?= Date: Tue, 21 Feb 2023 13:34:04 +0100 Subject: [PATCH] fix(ui_kit): make card core stateless --- .../cards/widgets/card_background.dart | 51 +++++++++++ .../cards/widgets/card_wrapper.dart | 90 ++++++------------- 2 files changed, 78 insertions(+), 63 deletions(-) create mode 100644 packages/wyatt_ui_kit/lib/src/components/cards/widgets/card_background.dart diff --git a/packages/wyatt_ui_kit/lib/src/components/cards/widgets/card_background.dart b/packages/wyatt_ui_kit/lib/src/components/cards/widgets/card_background.dart new file mode 100644 index 00000000..76862ab2 --- /dev/null +++ b/packages/wyatt_ui_kit/lib/src/components/cards/widgets/card_background.dart @@ -0,0 +1,51 @@ +import 'package:flutter/material.dart'; + +class CardBackground extends StatefulWidget { + const CardBackground({super.key, this.background, this.cardKey}); + + final Widget? background; + final GlobalKey? cardKey; + + @override + State createState() => _CardBackgroundState(); +} + +class _CardBackgroundState extends State { + Size _cardSize = Size.zero; + + @override + void initState() { + super.initState(); + if (widget.background != null) { + WidgetsBinding.instance.addPostFrameCallback((_) { + _resizeCard(); + }); + } + } + + @override + void didUpdateWidget(covariant CardBackground oldWidget) { + super.didUpdateWidget(oldWidget); + if (widget.background != null) { + _resizeCard(); + } + } + + void _resizeCard() { + final RenderObject? renderBox = + widget.cardKey?.currentContext?.findRenderObject(); + if (renderBox != null) { + setState(() { + _cardSize = + Size(renderBox.paintBounds.width, renderBox.paintBounds.height); + }); + } + } + + @override + Widget build(BuildContext context) => SizedBox( + width: _cardSize.width, + height: _cardSize.height, + child: Center(child: widget.background), + ); +} diff --git a/packages/wyatt_ui_kit/lib/src/components/cards/widgets/card_wrapper.dart b/packages/wyatt_ui_kit/lib/src/components/cards/widgets/card_wrapper.dart index 46a72582..db898c29 100644 --- a/packages/wyatt_ui_kit/lib/src/components/cards/widgets/card_wrapper.dart +++ b/packages/wyatt_ui_kit/lib/src/components/cards/widgets/card_wrapper.dart @@ -15,11 +15,12 @@ // along with this program. If not, see . import 'package:flutter/material.dart'; +import 'package:wyatt_ui_kit/src/components/cards/widgets/card_background.dart'; import 'package:wyatt_ui_kit/src/components/gradients/gradient_box_border.dart'; import 'package:wyatt_ui_kit/src/domain/card_theme_extension.dart'; -class CardWrapper extends StatefulWidget { - const CardWrapper({ +class CardWrapper extends StatelessWidget { + CardWrapper({ required this.child, required this.backgroundColors, required this.borderColors, @@ -40,54 +41,19 @@ class CardWrapper extends StatefulWidget { final Size? maxSize; final double? padding; - @override - State createState() => _CardWrapperState(); -} - -class _CardWrapperState extends State { - Size _cardSize = Size.zero; final GlobalKey _key = GlobalKey(); - @override - void initState() { - super.initState(); - if (widget.background != null) { - WidgetsBinding.instance.addPostFrameCallback((_) { - _resizeCard(); - }); - } - } - - @override - void didUpdateWidget(covariant CardWrapper oldWidget) { - super.didUpdateWidget(oldWidget); - if (widget.background != null) { - _resizeCard(); - } - } - - void _resizeCard() { - final RenderObject? renderBox = _key.currentContext?.findRenderObject(); - if (renderBox != null) { - setState(() { - _cardSize = - Size(renderBox.paintBounds.width, renderBox.paintBounds.height); - }); - } - } - - Widget _buildChild(Widget child) => (widget.background != null) + Widget _buildChild(Widget child) => (background != null) ? Stack( alignment: Alignment.center, children: [ - SizedBox( - width: _cardSize.width, - height: _cardSize.height, - child: Center(child: widget.background), + CardBackground( + cardKey: _key, + background: background, ), Padding( padding: EdgeInsets.all( - widget.padding ?? 25, + padding ?? 25, ), child: child, ), @@ -95,15 +61,14 @@ class _CardWrapperState extends State { ) : Padding( padding: EdgeInsets.all( - widget.padding ?? 25, + padding ?? 25, ), child: child, ); Gradient? _cardGradient(BuildContext context) { - if (widget.backgroundColors != null && - widget.backgroundColors!.length >= 2) { - return LinearGradient(colors: widget.backgroundColors!); + if (backgroundColors != null && backgroundColors!.length >= 2) { + return LinearGradient(colors: backgroundColors!); } else { final extensionCardColor = Theme.of(context).extension(); @@ -120,9 +85,8 @@ class _CardWrapperState extends State { } Color? _cardColor(BuildContext context) { - if (widget.backgroundColors != null && - widget.backgroundColors!.length == 1) { - return widget.backgroundColors!.first; + if (backgroundColors != null && backgroundColors!.length == 1) { + return backgroundColors!.first; } else { final extensionCardColor = Theme.of(context).extension(); @@ -136,16 +100,16 @@ class _CardWrapperState extends State { } BoxBorder? _boxBorder(BuildContext context) { - if (widget.borderColors != null) { - if (widget.borderColors!.length >= 2) { + if (borderColors != null) { + if (borderColors!.length >= 2) { return CustomGradientBoxBorder( gradient: LinearGradient( - colors: widget.borderColors!, + colors: borderColors!, ), ); - } else if (widget.borderColors!.isNotEmpty) { + } else if (borderColors!.isNotEmpty) { return Border.all( - color: widget.borderColors!.first, + color: borderColors!.first, ); } } else { @@ -171,8 +135,8 @@ class _CardWrapperState extends State { List _shadow(BuildContext context) { final shadows = List.empty(growable: true); - if (widget.shadow != null) { - shadows.add(widget.shadow!); + if (shadow != null) { + shadows.add(shadow!); } else { final extensionCardColor = Theme.of(context).extension(); @@ -195,17 +159,17 @@ class _CardWrapperState extends State { border: _boxBorder(context), boxShadow: _shadow(context), ), - child: (widget.minSize != null && widget.maxSize != null) + child: (minSize != null && maxSize != null) ? ConstrainedBox( constraints: BoxConstraints( - minWidth: widget.minSize!.width, - minHeight: widget.minSize!.height, - maxWidth: widget.maxSize!.width, - maxHeight: widget.maxSize!.height, + minWidth: minSize!.width, + minHeight: minSize!.height, + maxWidth: maxSize!.width, + maxHeight: maxSize!.height, ), - child: _buildChild(widget.child), + child: _buildChild(child), ) - : _buildChild(widget.child), + : _buildChild(child), ), ); }