action_bar_widget.dart 3.0 KB

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