π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
[mobile] Add floating action button component
Priyanshu Dangare569929e87eca
diff --git a/mobile/packages/ente_components/example/lib/main.dart b/mobile/packages/ente_components/example/lib/main.dartindex 33af05e48a..fe225967d8 100644--- a/mobile/packages/ente_components/example/lib/main.dart+++ b/mobile/packages/ente_components/example/lib/main.dart@@ -1,5 +1,6 @@import 'dart:async';+import 'package:ente_components/components/buttons/fab_component.dart';import 'package:ente_components/ente_components.dart';import 'package:flutter/gestures.dart';import 'package:flutter/material.dart';@@ -251,6 +252,19 @@ class _CatalogHomeState extends State<CatalogHome> {components: const ['Button', 'Icon button'],previewBuilder: (_) => const _ButtonMatrix(),),+ CatalogSection(+ title: 'Floating action button',+ icon: HugeIcons.strokeRoundedAdd01,+ components: const [+ 'Primary',+ 'Secondary',+ 'Icon',+ 'Medium',+ 'Medium with icon',+ ],+ previewBuilder: (_) => const SizedBox.shrink(),+ routeBuilder: _buildFABDemo,+ ),CatalogSection(title: 'Bottom sheets',icon: HugeIcons.strokeRoundedLayoutTable01,@@ -358,6 +372,17 @@ Widget _buildHeaderAppBarDemo();}+Widget _buildFABDemo(+ BuildContext context,+ ThemeMode themeMode,+ ValueChanged<ThemeMode> onThemeModeChanged,+) {+ return FABDemoPage(+ themeMode: themeMode,+ onThemeModeChanged: onThemeModeChanged,+ );+}+class CatalogSection {const CatalogSection({required this.title,@@ -1457,6 +1482,234 @@ class _ButtonMatrix extends StatelessWidget {}}+class FABDemoPage extends StatefulWidget {+ const FABDemoPage({+ super.key,+ required this.themeMode,+ required this.onThemeModeChanged,+ });++ final ThemeMode themeMode;+ final ValueChanged<ThemeMode> onThemeModeChanged;++ @override+ State<FABDemoPage> createState() => _FABDemoPageState();+}++class _FABDemoPageState extends State<FABDemoPage> {+ late ThemeMode _themeMode = widget.themeMode;++ void _setThemeMode(ThemeMode mode) {+ setState(() => _themeMode = mode);+ widget.onThemeModeChanged(mode);+ }++ @override+ Widget build(BuildContext context) {+ final colors = context.componentColors;+ return Scaffold(+ body: AppBarComponent(+ title: 'Floating action button',+ subtitle: 'Primary, Secondary, Icon, Medium',+ onBack: () => Navigator.of(context).pop(),+ actions: [+ _CatalogThemeCycleButton(+ themeMode: _themeMode,+ onChanged: _setThemeMode,+ ),+ ],+ slivers: [+ SliverPadding(+ padding: const EdgeInsets.fromLTRB(+ Spacing.lg,+ Spacing.xs,+ Spacing.lg,+ Spacing.lg,+ ),+ sliver: SliverToBoxAdapter(+ child: _FABDemoList(+ themeMode: _themeMode,+ onThemeModeChanged: _setThemeMode,+ ),+ ),+ ),+ ],+ ),+ backgroundColor: colors.backgroundBase,+ );+ }+}++class _FABDemoList extends StatelessWidget {+ const _FABDemoList({+ required this.themeMode,+ required this.onThemeModeChanged,+ });++ final ThemeMode themeMode;+ final ValueChanged<ThemeMode> onThemeModeChanged;++ @override+ Widget build(BuildContext context) {+ return MenuGroupComponent(+ items: [+ _FABDemoLink(+ title: 'Primary medium',+ variant: FABComponentVariant.primary,+ hasIcon: false,+ label: 'Add',+ themeMode: themeMode,+ onThemeModeChanged: onThemeModeChanged,+ ),+ _FABDemoLink(+ title: 'Primary medium with icon',+ variant: FABComponentVariant.primary,+ hasIcon: true,+ label: 'Add',+ themeMode: themeMode,+ onThemeModeChanged: onThemeModeChanged,+ ),+ _FABDemoLink(+ title: 'Primary icon',+ variant: FABComponentVariant.primary,+ hasIcon: true,+ themeMode: themeMode,+ onThemeModeChanged: onThemeModeChanged,+ ),+ _FABDemoLink(+ title: 'Secondary medium',+ variant: FABComponentVariant.secondary,+ hasIcon: false,+ label: 'Add',+ themeMode: themeMode,+ onThemeModeChanged: onThemeModeChanged,+ ),+ _FABDemoLink(+ title: 'Secondary medium with icon',+ variant: FABComponentVariant.secondary,+ hasIcon: true,+ label: 'Add',+ themeMode: themeMode,+ onThemeModeChanged: onThemeModeChanged,+ ),+ _FABDemoLink(+ title: 'Secondary icon',+ variant: FABComponentVariant.secondary,+ hasIcon: true,+ themeMode: themeMode,+ onThemeModeChanged: onThemeModeChanged,+ ),+ ],+ );+ }+}++class _FABDemoLink extends StatelessWidget {+ const _FABDemoLink({+ required this.title,+ required this.variant,+ required this.hasIcon,+ this.label,+ required this.themeMode,+ required this.onThemeModeChanged,+ });++ final String title;+ final FABComponentVariant variant;+ final bool hasIcon;+ final String? label;+ final ThemeMode themeMode;+ final ValueChanged<ThemeMode> onThemeModeChanged;++ @override+ Widget build(BuildContext context) {+ return MenuComponent(+ title: title,+ trailing: const _CatalogHugeIcon(+ HugeIcons.strokeRoundedArrowRight02,+ size: IconSizes.small,+ ),+ onTap: () => Navigator.of(context).push(+ MaterialPageRoute<void>(+ builder: (_) => _FABDemoSubPage(+ title: title,+ variant: variant,+ hasIcon: hasIcon,+ label: label,+ themeMode: themeMode,+ onThemeModeChanged: onThemeModeChanged,+ ),+ ),+ ),+ );+ }+}++class _FABDemoSubPage extends StatefulWidget {+ const _FABDemoSubPage({+ required this.title,+ required this.variant,+ required this.hasIcon,+ this.label,+ required this.themeMode,+ required this.onThemeModeChanged,+ });++ final String title;+ final FABComponentVariant variant;+ final bool hasIcon;+ final String? label;+ final ThemeMode themeMode;+ final ValueChanged<ThemeMode> onThemeModeChanged;++ @override+ State<_FABDemoSubPage> createState() => _FABDemoSubPageState();+}++class _FABDemoSubPageState extends State<_FABDemoSubPage> {+ late ThemeMode _themeMode = widget.themeMode;++ void _setThemeMode(ThemeMode mode) {+ setState(() => _themeMode = mode);+ widget.onThemeModeChanged(mode);+ }++ @override+ Widget build(BuildContext context) {+ final colors = context.componentColors;+ return Scaffold(+ body: AppBarComponent(+ title: widget.title,+ subtitle: 'Floating action button',+ onBack: () => Navigator.of(context).pop(),+ actions: [+ _CatalogThemeCycleButton(+ themeMode: _themeMode,+ onChanged: _setThemeMode,+ ),+ ],+ slivers: const [],+ ),+ floatingActionButton: Padding(+ padding: const EdgeInsets.only(bottom: 4),+ child: FABComponent(+ icon: widget.hasIcon+ ? const _CatalogHugeIcon(+ HugeIcons.strokeRoundedAdd01,+ size: IconSizes.small,+ )+ : null,+ label: widget.label,+ variant: widget.variant,+ onTap: () {},+ ),+ ),+ floatingActionButtonLocation: FloatingActionButtonLocation.centerFloat,+ backgroundColor: colors.backgroundBase,+ );+ }+}+class ButtonStateCyclePreview extends StatefulWidget {const ButtonStateCyclePreview({super.key});diff --git a/mobile/packages/ente_components/lib/components/buttons/fab_component.dart b/mobile/packages/ente_components/lib/components/buttons/fab_component.dartnew file mode 100644index 0000000000..06df828720--- /dev/null+++ b/mobile/packages/ente_components/lib/components/buttons/fab_component.dart@@ -0,0 +1,317 @@+import "dart:async";++import "package:ente_components/models/component_execution_state.dart";+import "package:ente_components/theme/icon_sizes.dart";+import "package:ente_components/theme/motion.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 FABComponentVariant { primary, secondary }++/// Figma: https://www.figma.com/design/BuBNPPytxlVnqfmCUW0mgz/Ente-Visual-Design?node-id=21843-6537&m=dev+/// Section: Buttons / Floating action button+/// Specs: Extended is 56px high with 20px leading padding, 24px trailing+/// padding, an 8px gap, and a 999px radius. Icon is 52px square with a 30px+/// radius.+/// Types: Extended, Icon.+/// States: Extended has default and pressed. Icon has default.+class FABComponent extends StatefulWidget {+ final String? label;++ final FutureOr<void> Function()? onTap;++ final FABComponentVariant variant;++ final bool isDisabled;++ final Widget? icon;++ const FABComponent({+ super.key,+ this.icon,+ this.label,+ this.onTap,+ this.variant = FABComponentVariant.primary,+ this.isDisabled = false,+ this.shouldSurfaceExecutionStates = true,+ this.shouldShowSuccessState = true,+ this.shouldShowSuccessConfirmation = false,+ });++ final bool shouldSurfaceExecutionStates;++ final bool shouldShowSuccessState;++ final bool shouldShowSuccessConfirmation;++ @override+ State<FABComponent> createState() => _FABComponentState();+}++class _FABComponentState extends State<FABComponent>+ with SingleTickerProviderStateMixin {+ static const Duration _loadingDelay = Duration(milliseconds: 300);+ static const Duration _successDisplayDuration = Duration(seconds: 1);++ late final AnimationController _loadingController;+ bool _isPressed = false;+ Timer? _loadingTimer;+ Timer? _successResetTimer;+ bool _loadingVisible = false;+ ComponentExecutionState _executionState = ComponentExecutionState.idle;++ @override+ void initState() {+ super.initState();+ _loadingController = AnimationController(+ vsync: this,+ duration: const Duration(milliseconds: 800),+ );+ }++ @override+ void didUpdateWidget(covariant FABComponent oldWidget) {+ super.didUpdateWidget(oldWidget);+ if (widget.isDisabled && !oldWidget.isDisabled) {+ _isPressed = false;+ }+ _syncLoadingController();+ }++ @override+ void dispose() {+ _loadingTimer?.cancel();+ _successResetTimer?.cancel();+ _loadingController.dispose();+ super.dispose();+ }++ @override+ Widget build(BuildContext context) {+ final isEnabled =+ !widget.isDisabled &&+ widget.onTap != null &&+ !_isExecuting &&+ !_isSuccessful;+ final colors = context.componentColors;+ final backgroundColor = switch (widget.variant) {+ FABComponentVariant.primary =>+ _isPressed ? colors.primaryDark : colors.primary,+ FABComponentVariant.secondary =>+ _isPressed ? colors.fillDarker : colors.fillDark,+ };+ final foregroundColor = switch (widget.variant) {+ FABComponentVariant.primary => colors.specialWhite,+ FABComponentVariant.secondary => colors.primary,+ };+ return GestureDetector(+ behavior: HitTestBehavior.opaque,+ onTap: isEnabled ? _handleTap : null,+ onTapDown: isEnabled ? (_) => _setPressed(true) : null,+ onTapUp: isEnabled ? (_) => _setPressed(false) : null,+ onTapCancel: isEnabled ? () => _setPressed(false) : null,+ child: AnimatedScale(+ scale: _isPressed ? 0.98 : 1,+ duration: Motion.quick,+ curve: Curves.easeOutCubic,+ child: AnimatedContainer(+ duration: Motion.standard,+ curve: Curves.easeInOutCubic,+ decoration: BoxDecoration(+ color: backgroundColor,+ borderRadius: BorderRadius.circular(9999),+ ),+ child: ConstrainedBox(+ constraints: const BoxConstraints(minWidth: 56, minHeight: 56),+ child: Padding(+ padding: EdgeInsets.symmetric(+ horizontal: widget.label == null ? 0 : 24,+ ),+ child: AnimatedSwitcher(+ duration: Motion.quick,+ switchInCurve: Curves.easeOutCubic,+ switchOutCurve: Curves.easeInCubic,+ child: _content(foregroundColor),+ ),+ ),+ ),+ ),+ ),+ );+ }++ void _setPressed(bool value) {+ if (_isPressed == value) return;+ setState(() => _isPressed = value);+ }++ Widget _content(Color foreground) {+ if (_showLoading) {+ return _executionContent(+ key: const ValueKey("loading"),+ child: RotationTransition(+ turns: _loadingController,+ child: HugeIcon(+ icon: HugeIcons.strokeRoundedLoading03,+ size: IconSizes.small,+ color: foreground,+ ),+ ),+ );+ }+ if (_showSuccess) {+ return _executionContent(+ key: const ValueKey("success"),+ child: HugeIcon(+ icon: HugeIcons.strokeRoundedTick02,+ size: IconSizes.small,+ color: foreground,+ ),+ );+ }+ return _idleContent(foreground);+ }++ Widget _idleContent(Color foreground) {+ return Row(+ key: const ValueKey("content"),+ spacing: 8,+ mainAxisSize: MainAxisSize.min,+ mainAxisAlignment: MainAxisAlignment.center,+ children: [+ if (widget.icon != null)+ IconTheme.merge(+ data: IconThemeData(color: foreground, size: IconSizes.small),+ child: widget.icon!,+ ),+ if (widget.label != null)+ Text(+ widget.label!,+ overflow: TextOverflow.ellipsis,+ maxLines: 1,+ style: TextStyles.bodyBold.copyWith(color: foreground),+ ),+ ],+ );+ }++ Widget _executionContent({required Key key, required Widget child}) {+ return Stack(+ key: key,+ alignment: Alignment.center,+ children: [+ Visibility(+ visible: false,+ maintainAnimation: true,+ maintainSize: true,+ maintainState: true,+ child: _idleContent(Colors.transparent),+ ),+ child,+ ],+ );+ }++ bool get _isExecuting =>+ _executionState == ComponentExecutionState.inProgress;++ bool get _isSuccessful =>+ _executionState == ComponentExecutionState.successful;++ bool get _showLoading =>+ widget.shouldSurfaceExecutionStates && _isExecuting && _loadingVisible;++ bool get _showSuccess =>+ widget.shouldSurfaceExecutionStates &&+ widget.shouldShowSuccessState &&+ _isSuccessful;++ void _syncLoadingController() {+ if (_showLoading) {+ if (!_loadingController.isAnimating) {+ _loadingController.repeat();+ }+ return;+ }++ if (_loadingController.isAnimating || _loadingController.value != 0) {+ _loadingController.stop();+ _loadingController.reset();+ }+ }++ Future<void> _handleTap() async {+ final callback = widget.onTap;+ if (callback == null) return;++ _successResetTimer?.cancel();+ var loadingSurfaced = false;+ _loadingTimer?.cancel();+ setState(() {+ _executionState = ComponentExecutionState.inProgress;+ _loadingVisible = false;+ _isPressed = false;+ });+ _loadingTimer = Timer(_loadingDelay, () {+ if (!mounted) return;+ loadingSurfaced = true;+ setState(() {+ _loadingVisible = true;+ _isPressed = false;+ });+ _syncLoadingController();+ });++ try {+ await Future.sync(callback);+ if (!mounted) return;++ final loadingPending = _loadingTimer?.isActive ?? false;+ _loadingTimer?.cancel();+ _loadingTimer = null;++ final shouldShowSuccess =+ widget.shouldSurfaceExecutionStates &&+ widget.shouldShowSuccessState &&+ (loadingSurfaced ||+ (loadingPending && widget.shouldShowSuccessConfirmation));++ if (shouldShowSuccess) {+ _showSuccessForDuration();+ } else {+ _clearExecutionState();+ }+ } catch (_) {+ _loadingTimer?.cancel();+ _loadingTimer = null;+ if (mounted) {+ _clearExecutionState();+ }+ }+ }++ void _clearExecutionState() {+ setState(() {+ _executionState = ComponentExecutionState.idle;+ _loadingVisible = false;+ _isPressed = false;+ });+ _syncLoadingController();+ }++ void _showSuccessForDuration() {+ setState(() {+ _executionState = ComponentExecutionState.successful;+ _loadingVisible = false;+ _isPressed = false;+ });+ _syncLoadingController();+ _successResetTimer?.cancel();+ _successResetTimer = Timer(_successDisplayDuration, () {+ if (!mounted) return;+ _clearExecutionState();+ });+ }+}