action_bar_widget.dart 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. import 'package:flutter/material.dart';
  2. import 'package:photos/core/configuration.dart';
  3. import 'package:photos/models/gallery_type.dart';
  4. import 'package:photos/models/selected_files.dart';
  5. import 'package:photos/theme/ente_theme.dart';
  6. class ActionBarWidget extends StatefulWidget {
  7. final String? text;
  8. final List<Widget> iconButtons;
  9. final SelectedFiles? selectedFiles;
  10. final GalleryType galleryType;
  11. final bool isCollaborator;
  12. const ActionBarWidget({
  13. required this.iconButtons,
  14. required this.galleryType,
  15. this.text,
  16. this.selectedFiles,
  17. this.isCollaborator = false,
  18. super.key,
  19. });
  20. @override
  21. State<ActionBarWidget> createState() => _ActionBarWidgetState();
  22. }
  23. class _ActionBarWidgetState extends State<ActionBarWidget> {
  24. final ValueNotifier<int> _selectedFilesNotifier = ValueNotifier(0);
  25. final ValueNotifier<int> _selectedOwnedFilesNotifier = ValueNotifier(0);
  26. final int currentUserID = Configuration.instance.getUserID()!;
  27. @override
  28. void initState() {
  29. widget.selectedFiles?.addListener(_selectedFilesListener);
  30. super.initState();
  31. }
  32. @override
  33. void dispose() {
  34. widget.selectedFiles?.removeListener(_selectedFilesListener);
  35. super.dispose();
  36. }
  37. @override
  38. Widget build(BuildContext context) {
  39. return SizedBox(
  40. child: Row(
  41. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  42. children: _actionBarWidgets(context),
  43. ),
  44. );
  45. }
  46. List<Widget> _actionBarWidgets(BuildContext context) {
  47. final actionBarWidgets = <Widget>[];
  48. final initialLength = widget.iconButtons.length;
  49. final textTheme = getEnteTextTheme(context);
  50. final colorScheme = getEnteColorScheme(context);
  51. actionBarWidgets.addAll(widget.iconButtons);
  52. if (widget.text != null) {
  53. //adds 12 px spacing at the start and between iconButton elements
  54. for (var i = 0; i < initialLength; i++) {
  55. actionBarWidgets.insert(
  56. 2 * i,
  57. const SizedBox(
  58. width: 12,
  59. ),
  60. );
  61. }
  62. actionBarWidgets.insertAll(0, [
  63. const SizedBox(width: 20),
  64. Flexible(
  65. child: Row(
  66. children: [
  67. widget.selectedFiles != null
  68. ? ValueListenableBuilder(
  69. valueListenable: _selectedFilesNotifier,
  70. builder: (context, value, child) {
  71. return Text(
  72. "${_selectedFilesNotifier.value} selected" +
  73. (_selectedOwnedFilesNotifier.value !=
  74. _selectedFilesNotifier.value
  75. ? " (${_selectedOwnedFilesNotifier.value} "
  76. "yours) "
  77. : ""),
  78. style: textTheme.body.copyWith(
  79. color: colorScheme.blurTextBase,
  80. ),
  81. );
  82. },
  83. )
  84. : Text(
  85. widget.text!,
  86. style:
  87. textTheme.body.copyWith(color: colorScheme.textMuted),
  88. ),
  89. ],
  90. ),
  91. ),
  92. ]);
  93. //to add whitespace of 8pts or 12 pts at the end
  94. if (widget.iconButtons.length > 1) {
  95. actionBarWidgets.add(
  96. const SizedBox(width: 8),
  97. );
  98. } else {
  99. actionBarWidgets.add(
  100. const SizedBox(width: 12),
  101. );
  102. }
  103. }
  104. return actionBarWidgets;
  105. }
  106. void _selectedFilesListener() {
  107. if (widget.selectedFiles!.files.isNotEmpty) {
  108. _selectedFilesNotifier.value = widget.selectedFiles!.files.length;
  109. _selectedOwnedFilesNotifier.value = widget.selectedFiles!.files
  110. .where((f) => f.ownerID == null || f.ownerID! == currentUserID)
  111. .length;
  112. }
  113. }
  114. }