Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

Add BannerComponent and corresponding tests for state handling and styling

AmanRajSinghMourya91511a75e652
diff --git a/mobile/packages/ente_components/lib/components/banner_component.dart b/mobile/packages/ente_components/lib/components/banner_component.dart
new file mode 100644
index 0000000000..d69e7f36a7
--- /dev/null
+++ b/mobile/packages/ente_components/lib/components/banner_component.dart
@@ -0,0 +1,205 @@
+import 'package:ente_components/theme/icon_sizes.dart';
+import 'package:ente_components/theme/radii.dart';
+import 'package:ente_components/theme/spacing.dart';
+import 'package:ente_components/theme/text_styles.dart';
+import 'package:ente_components/theme/theme.dart';
+import 'package:flutter/material.dart';
+import 'package:hugeicons/hugeicons.dart';
+
+enum BannerComponentState { failure, informative, success, warning, neutral }
+
+/// Layout Figma: https://www.figma.com/design/BuBNPPytxlVnqfmCUW0mgz/Ente-Visual-Design?node-id=14590-125265&m=dev
+/// State Figma: https://www.figma.com/design/BuBNPPytxlVnqfmCUW0mgz/Ente-Visual-Design?node-id=7255-38447&m=dev
+/// Section: Home gallery banners / Snack bar states
+/// Specs: 351px wide, 66px min height, 20px radius, 24px leading icon,
+/// body/mini text, and an optional trailing action.
+class BannerComponent extends StatelessWidget {
+ const BannerComponent({
+ required this.title,
+ required this.onTap,
+ this.leadingIcon,
+ this.leadingWidget,
+ this.state = BannerComponentState.neutral,
+ this.subtitle,
+ this.trailingWidget,
+ super.key,
+ });
+
+ static const double minHeight = 66;
+ static const double actionSize = 38;
+ static const double _leadingIconSize = IconSizes.medium;
+ static const double _actionIconSize = IconSizes.medium;
+
+ final String title;
+ final String? subtitle;
+ final List<List<dynamic>>? leadingIcon;
+ final Widget? leadingWidget;
+ final BannerComponentState state;
+ final Widget? trailingWidget;
+ final VoidCallback onTap;
+
+ @override
+ Widget build(BuildContext context) {
+ final colors = context.componentColors;
+ final accentColor = switch (state) {
+ BannerComponentState.failure => colors.warning,
+ BannerComponentState.informative => colors.blue,
+ BannerComponentState.success => colors.primaryDark,
+ BannerComponentState.warning => colors.caution,
+ BannerComponentState.neutral => colors.primaryDark,
+ };
+ final titleColor = switch (state) {
+ BannerComponentState.failure => colors.warning,
+ BannerComponentState.informative => colors.blue,
+ BannerComponentState.success => colors.primaryDark,
+ BannerComponentState.warning => colors.caution,
+ BannerComponentState.neutral => colors.textBase,
+ };
+ final subtitle = this.subtitle;
+ final leadingWidget = this.leadingWidget;
+ final trailingWidget = this.trailingWidget;
+
+ return Semantics(
+ button: true,
+ child: GestureDetector(
+ behavior: HitTestBehavior.opaque,
+ onTap: onTap,
+ child: SizedBox(
+ width: double.infinity,
+ child: ConstrainedBox(
+ constraints: const BoxConstraints(minHeight: minHeight),
+ child: DecoratedBox(
+ key: const ValueKey('banner-component-surface'),
+ decoration: BoxDecoration(
+ color: colors.fillLight,
+ borderRadius: BorderRadius.circular(Radii.button),
+ ),
+ child: Padding(
+ padding: const EdgeInsets.symmetric(
+ horizontal: Spacing.lg,
+ vertical: Spacing.md,
+ ),
+ child: Row(
+ children: [
+ leadingWidget == null
+ ? HugeIcon(
+ icon: leadingIcon ?? _defaultLeadingIcon,
+ size: _leadingIconSize,
+ color: accentColor,
+ )
+ : SizedBox(
+ width: _leadingIconSize,
+ height: _leadingIconSize,
+ child: Center(
+ child: IconTheme.merge(
+ data: IconThemeData(
+ color: accentColor,
+ size: _leadingIconSize,
+ ),
+ child: leadingWidget,
+ ),
+ ),
+ ),
+ const SizedBox(width: Spacing.lg),
+ Expanded(
+ child: subtitle == null || subtitle.isEmpty
+ ? Text(
+ title,
+ maxLines: 2,
+ overflow: TextOverflow.ellipsis,
+ style: TextStyles.bodyBold.copyWith(
+ color: titleColor,
+ ),
+ )
+ : Column(
+ mainAxisSize: MainAxisSize.min,
+ crossAxisAlignment: CrossAxisAlignment.start,
+ children: [
+ Text(
+ title,
+ maxLines: 1,
+ overflow: TextOverflow.ellipsis,
+ style: TextStyles.bodyBold.copyWith(
+ color: titleColor,
+ ),
+ ),
+ const SizedBox(height: Spacing.xs),
+ Text(
+ subtitle,
+ maxLines: 2,
+ overflow: TextOverflow.ellipsis,
+ style: TextStyles.mini.copyWith(
+ color: colors.textLight,
+ ),
+ ),
+ ],
+ ),
+ ),
+ const SizedBox(width: Spacing.md),
+ trailingWidget == null
+ ? _BannerActionButton(
+ iconColor: colors.textBase,
+ onTap: onTap,
+ )
+ : SizedBox(
+ key: const ValueKey(
+ 'banner-component-trailing-slot',
+ ),
+ width: actionSize,
+ height: actionSize,
+ child: Center(
+ child: IconTheme.merge(
+ data: IconThemeData(
+ color: colors.textBase,
+ size: _actionIconSize,
+ ),
+ child: trailingWidget,
+ ),
+ ),
+ ),
+ ],
+ ),
+ ),
+ ),
+ ),
+ ),
+ ),
+ );
+ }
+
+ List<List<dynamic>> get _defaultLeadingIcon {
+ return switch (state) {
+ BannerComponentState.failure => HugeIcons.strokeRoundedCancelCircle,
+ BannerComponentState.informative => HugeIcons.strokeRoundedAlertCircle,
+ BannerComponentState.success => HugeIcons.strokeRoundedCheckmarkCircle02,
+ BannerComponentState.warning => HugeIcons.strokeRoundedAlert02,
+ BannerComponentState.neutral => HugeIcons.strokeRoundedLoading03,
+ };
+ }
+}
+
+class _BannerActionButton extends StatelessWidget {
+ const _BannerActionButton({required this.iconColor, required this.onTap});
+
+ final Color iconColor;
+ final VoidCallback onTap;
+
+ @override
+ Widget build(BuildContext context) {
+ return GestureDetector(
+ behavior: HitTestBehavior.opaque,
+ onTap: onTap,
+ child: SizedBox(
+ width: BannerComponent.actionSize,
+ height: BannerComponent.actionSize,
+ child: Center(
+ child: Icon(
+ Icons.arrow_forward,
+ size: BannerComponent._actionIconSize,
+ color: iconColor,
+ ),
+ ),
+ ),
+ );
+ }
+}
diff --git a/mobile/packages/ente_components/test/components/banner_component_test.dart b/mobile/packages/ente_components/test/components/banner_component_test.dart
new file mode 100644
index 0000000000..547581d28f
--- /dev/null
+++ b/mobile/packages/ente_components/test/components/banner_component_test.dart
@@ -0,0 +1,216 @@
+import "package:ente_components/ente_components.dart";
+import "package:flutter/material.dart";
+import "package:flutter_test/flutter_test.dart";
+import "package:hugeicons/hugeicons.dart";
+
+void main() {
+ testWidgets("BannerComponent renders copy and handles taps", (tester) async {
+ var tapCount = 0;
+
+ await tester.pumpWidget(
+ _wrap(
+ BannerComponent(
+ title: "Subscribe",
+ subtitle: "Your subscription has expired",
+ state: BannerComponentState.failure,
+ onTap: () => tapCount += 1,
+ ),
+ ),
+ );
+
+ expect(find.text("Subscribe"), findsOneWidget);
+ expect(find.text("Your subscription has expired"), findsOneWidget);
+ expect(find.byType(HugeIcon), findsOneWidget);
+ expect(find.byIcon(Icons.arrow_forward), findsOneWidget);
+
+ final size = tester.getSize(
+ find.byKey(const ValueKey("banner-component-surface")),
+ );
+ expect(size.width, 351);
+ expect(size.height, greaterThanOrEqualTo(BannerComponent.minHeight));
+
+ await tester.tap(find.text("Subscribe"));
+ await tester.pump();
+
+ expect(tapCount, 1);
+ });
+
+ testWidgets("BannerComponent supports no-subtitle banners", (tester) async {
+ await tester.pumpWidget(
+ _wrap(
+ BannerComponent(
+ leadingIcon: HugeIcons.strokeRoundedAlertCircle,
+ title: "Confirm your recovery key",
+ onTap: () {},
+ ),
+ ),
+ );
+
+ final title = tester.widget<Text>(find.text("Confirm your recovery key"));
+ expect(title.maxLines, 2);
+ expect(title.overflow, TextOverflow.ellipsis);
+ });
+
+ testWidgets("BannerComponent maps state colors from component tokens", (
+ tester,
+ ) async {
+ await _expectTitleColor(
+ tester,
+ state: BannerComponentState.failure,
+ title: "Failure",
+ expectedColor: ComponentTheme.colorsForApp(ComponentApp.photos).warning,
+ );
+ await _expectTitleColor(
+ tester,
+ state: BannerComponentState.informative,
+ title: "Informative",
+ expectedColor: ComponentTheme.colorsForApp(ComponentApp.photos).blue,
+ );
+ await _expectTitleColor(
+ tester,
+ state: BannerComponentState.success,
+ title: "Success",
+ expectedColor: ComponentTheme.colorsForApp(
+ ComponentApp.photos,
+ ).primaryDark,
+ );
+ await _expectTitleColor(
+ tester,
+ state: BannerComponentState.warning,
+ title: "Warning",
+ expectedColor: ComponentTheme.colorsForApp(ComponentApp.photos).caution,
+ );
+ await _expectTitleColor(
+ tester,
+ state: BannerComponentState.neutral,
+ title: "Neutral",
+ expectedColor: ComponentTheme.colorsForApp(ComponentApp.photos).textBase,
+ );
+ });
+
+ testWidgets("BannerComponent uses component surface styling", (tester) async {
+ await tester.pumpWidget(
+ _wrap(
+ BannerComponent(
+ leadingIcon: HugeIcons.strokeRoundedDatabase,
+ title: "Upgrade",
+ subtitle: "Storage limit exceeded",
+ state: BannerComponentState.failure,
+ onTap: () {},
+ ),
+ brightness: Brightness.dark,
+ ),
+ );
+
+ final surface = tester.widget<DecoratedBox>(
+ find.byKey(const ValueKey("banner-component-surface")),
+ );
+ final decoration = surface.decoration as BoxDecoration;
+ final colors = ComponentTheme.colorsForApp(
+ ComponentApp.photos,
+ brightness: Brightness.dark,
+ );
+
+ expect(decoration.color, colors.fillLight);
+ expect(decoration.borderRadius, BorderRadius.circular(Radii.button));
+ expect(decoration.boxShadow, isNull);
+ });
+
+ testWidgets("BannerComponent supports a custom trailing widget", (
+ tester,
+ ) async {
+ await tester.pumpWidget(
+ _wrap(
+ BannerComponent(
+ title: "Informative",
+ subtitle: "Custom trailing affordance",
+ state: BannerComponentState.informative,
+ trailingWidget: const SizedBox(
+ key: ValueKey("custom-trailing"),
+ width: 24,
+ height: 24,
+ child: HugeIcon(icon: HugeIcons.strokeRoundedCancel01),
+ ),
+ onTap: () {},
+ ),
+ ),
+ );
+
+ expect(find.byKey(const ValueKey("custom-trailing")), findsOneWidget);
+ final trailingSlotSize = tester.getSize(
+ find.byKey(const ValueKey("banner-component-trailing-slot")),
+ );
+ expect(trailingSlotSize.width, BannerComponent.actionSize);
+ expect(trailingSlotSize.height, BannerComponent.actionSize);
+ expect(
+ find.descendant(
+ of: find.byKey(const ValueKey("custom-trailing")),
+ matching: find.byType(HugeIcon),
+ ),
+ findsOneWidget,
+ );
+ expect(find.byType(HugeIcon), findsNWidgets(2));
+ expect(find.byIcon(Icons.arrow_forward), findsNothing);
+ });
+
+ testWidgets("BannerComponent supports a custom leading widget", (
+ tester,
+ ) async {
+ await tester.pumpWidget(
+ _wrap(
+ BannerComponent(
+ leadingIcon: HugeIcons.strokeRoundedInformationCircle,
+ leadingWidget: const SizedBox(
+ key: ValueKey("custom-leading"),
+ width: 16,
+ height: 16,
+ child: CircularProgressIndicator(strokeWidth: 2),
+ ),
+ title: "Syncing",
+ subtitle: "Preparing secure upload",
+ state: BannerComponentState.informative,
+ onTap: () {},
+ ),
+ ),
+ );
+
+ expect(find.byKey(const ValueKey("custom-leading")), findsOneWidget);
+ expect(find.byType(CircularProgressIndicator), findsOneWidget);
+ expect(find.byType(HugeIcon), findsNothing);
+ expect(find.byIcon(Icons.arrow_forward), findsOneWidget);
+ });
+}
+
+Future<void> _expectTitleColor(
+ WidgetTester tester, {
+ required BannerComponentState state,
+ required String title,
+ required Color expectedColor,
+}) async {
+ await tester.pumpWidget(
+ _wrap(
+ BannerComponent(
+ leadingIcon: HugeIcons.strokeRoundedInformationCircle,
+ title: title,
+ state: state,
+ onTap: () {},
+ ),
+ ),
+ );
+
+ final text = tester.widget<Text>(find.text(title));
+ expect(text.style?.color, expectedColor);
+}
+
+Widget _wrap(Widget child, {Brightness brightness = Brightness.light}) {
+ return MaterialApp(
+ themeAnimationDuration: Duration.zero,
+ theme: ComponentTheme.themeForApp(
+ ComponentApp.photos,
+ brightness: brightness,
+ ),
+ home: Scaffold(
+ body: Center(child: SizedBox(width: 351, child: child)),
+ ),
+ );
+}