fix(ui_kit): make card core stateless
This commit is contained in:
parent
25224469b5
commit
133e0e910b
@ -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<CardBackground> createState() => _CardBackgroundState();
|
||||
}
|
||||
|
||||
class _CardBackgroundState extends State<CardBackground> {
|
||||
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),
|
||||
);
|
||||
}
|
@ -15,11 +15,12 @@
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
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<CardWrapper> createState() => _CardWrapperState();
|
||||
}
|
||||
|
||||
class _CardWrapperState extends State<CardWrapper> {
|
||||
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<CardWrapper> {
|
||||
)
|
||||
: 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<CardThemeExtension>();
|
||||
@ -120,9 +85,8 @@ class _CardWrapperState extends State<CardWrapper> {
|
||||
}
|
||||
|
||||
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<CardThemeExtension>();
|
||||
@ -136,16 +100,16 @@ class _CardWrapperState extends State<CardWrapper> {
|
||||
}
|
||||
|
||||
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<CardWrapper> {
|
||||
|
||||
List<BoxShadow> _shadow(BuildContext context) {
|
||||
final shadows = List<BoxShadow>.empty(growable: true);
|
||||
if (widget.shadow != null) {
|
||||
shadows.add(widget.shadow!);
|
||||
if (shadow != null) {
|
||||
shadows.add(shadow!);
|
||||
} else {
|
||||
final extensionCardColor =
|
||||
Theme.of(context).extension<CardThemeExtension>();
|
||||
@ -195,17 +159,17 @@ class _CardWrapperState extends State<CardWrapper> {
|
||||
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),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user