|
| 1 | +import 'package:collection/collection.dart'; |
| 2 | +import 'package:flutter/material.dart'; |
| 3 | +import 'package:shimmer/shimmer.dart'; |
| 4 | + |
| 5 | +import '../extensions/control.dart'; |
| 6 | +import '../models/control.dart'; |
| 7 | +import '../utils/colors.dart'; |
| 8 | +import '../utils/gradient.dart'; |
| 9 | +import '../utils/numbers.dart'; |
| 10 | +import '../utils/time.dart'; |
| 11 | +import '../widgets/error.dart'; |
| 12 | +import 'base_controls.dart'; |
| 13 | + |
| 14 | +class ShimmerControl extends StatelessWidget { |
| 15 | + final Control control; |
| 16 | + |
| 17 | + const ShimmerControl({super.key, required this.control}); |
| 18 | + |
| 19 | + @override |
| 20 | + Widget build(BuildContext context) { |
| 21 | + debugPrint("Shimmer build: ${control.id}"); |
| 22 | + |
| 23 | + final content = control.buildWidget("content"); |
| 24 | + if (content == null) { |
| 25 | + return const ErrorControl("Shimmer.content must be specified"); |
| 26 | + } |
| 27 | + |
| 28 | + final gradient = control.getGradient("gradient", Theme.of(context)); |
| 29 | + final baseColor = control.getColor("base_color", context); |
| 30 | + final highlightColor = control.getColor("highlight_color", context); |
| 31 | + |
| 32 | + if (gradient == null && (baseColor == null || highlightColor == null)) { |
| 33 | + return const ErrorControl( |
| 34 | + "Shimmer requires either gradient or base/highlight colors"); |
| 35 | + } |
| 36 | + |
| 37 | + final direction = _parseDirection(control.getString("direction")); |
| 38 | + final period = |
| 39 | + control.getDuration("period", const Duration(milliseconds: 1500))!; |
| 40 | + final loop = control.getInt("loop", 0)!; |
| 41 | + |
| 42 | + final shimmerWidget = gradient != null |
| 43 | + ? Shimmer( |
| 44 | + gradient: gradient, |
| 45 | + direction: direction, |
| 46 | + period: period, |
| 47 | + loop: loop, |
| 48 | + enabled: !control.disabled, |
| 49 | + child: content, |
| 50 | + ) |
| 51 | + : Shimmer.fromColors( |
| 52 | + baseColor: baseColor!, |
| 53 | + highlightColor: highlightColor!, |
| 54 | + direction: direction, |
| 55 | + period: period, |
| 56 | + loop: loop, |
| 57 | + enabled: !control.disabled, |
| 58 | + child: content, |
| 59 | + ); |
| 60 | + |
| 61 | + return LayoutControl(control: control, child: shimmerWidget); |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +ShimmerDirection _parseDirection(String? value, |
| 66 | + [ShimmerDirection defaultValue = ShimmerDirection.ltr]) { |
| 67 | + if (value == null) { |
| 68 | + return defaultValue; |
| 69 | + } |
| 70 | + return ShimmerDirection.values.firstWhereOrNull( |
| 71 | + (dir) => dir.name.toLowerCase() == value.toLowerCase()) ?? |
| 72 | + defaultValue; |
| 73 | +} |
0 commit comments