file_caption_widget.dart 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import 'package:flutter/material.dart';
  2. import 'package:photos/models/file.dart';
  3. import 'package:photos/theme/ente_theme.dart';
  4. import 'package:photos/utils/magic_util.dart';
  5. class FileCaptionWidget extends StatefulWidget {
  6. final File file;
  7. const FileCaptionWidget({required this.file, super.key});
  8. @override
  9. State<FileCaptionWidget> createState() => _FileCaptionWidgetState();
  10. }
  11. class _FileCaptionWidgetState extends State<FileCaptionWidget> {
  12. int maxLength = 280;
  13. int currentLength = 0;
  14. final _textController = TextEditingController();
  15. final _focusNode = FocusNode();
  16. String editedCaption = "";
  17. @override
  18. void dispose() {
  19. editCaption(fromDispose: true);
  20. _textController.dispose();
  21. super.dispose();
  22. }
  23. @override
  24. Widget build(BuildContext context) {
  25. final colorScheme = getEnteColorScheme(context);
  26. final textTheme = getEnteTextTheme(context);
  27. final caption = widget.file.caption;
  28. return TextField(
  29. onEditingComplete: () {
  30. editCaption();
  31. _focusNode.unfocus();
  32. },
  33. controller: _textController,
  34. focusNode: _focusNode,
  35. decoration: InputDecoration(
  36. counterStyle: textTheme.mini.copyWith(color: colorScheme.textMuted),
  37. counterText: currentLength > 99
  38. ? currentLength.toString() + " / " + maxLength.toString()
  39. : "",
  40. contentPadding: const EdgeInsets.all(16),
  41. border: InputBorder.none,
  42. focusedBorder: InputBorder.none,
  43. filled: true,
  44. fillColor: colorScheme.fillFaint,
  45. hintText: caption.isEmpty ? "Add a caption" : caption,
  46. hintStyle: getEnteTextTheme(context)
  47. .small
  48. .copyWith(color: colorScheme.textMuted),
  49. ),
  50. style: getEnteTextTheme(context).small,
  51. cursorWidth: 1.5,
  52. maxLength: maxLength,
  53. minLines: 1,
  54. maxLines: 6,
  55. keyboardType: TextInputType.text,
  56. onChanged: (value) {
  57. setState(() {
  58. currentLength = value.length;
  59. editedCaption = value;
  60. });
  61. },
  62. );
  63. }
  64. void editCaption({bool fromDispose = false}) {
  65. if (editedCaption.isNotEmpty) {
  66. editFileCaption(fromDispose ? null : context, widget.file, editedCaption);
  67. }
  68. }
  69. }