file_caption_widget.dart 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 initState() {
  19. _focusNode.addListener(() {
  20. final caption = widget.file.caption;
  21. if (_focusNode.hasFocus && caption != null) {
  22. _textController.text = caption;
  23. editedCaption = caption;
  24. }
  25. });
  26. editedCaption = widget.file.caption;
  27. super.initState();
  28. }
  29. @override
  30. void dispose() {
  31. if (editedCaption != null) {
  32. editFileCaption(null, widget.file, editedCaption);
  33. }
  34. _textController.dispose();
  35. _focusNode.removeListener(() {});
  36. super.dispose();
  37. }
  38. @override
  39. Widget build(BuildContext context) {
  40. final colorScheme = getEnteColorScheme(context);
  41. final textTheme = getEnteTextTheme(context);
  42. final caption = widget.file.caption;
  43. return TextField(
  44. onEditingComplete: () {
  45. if (editedCaption != null) {
  46. editFileCaption(context, widget.file, editedCaption);
  47. }
  48. _focusNode.unfocus();
  49. },
  50. controller: _textController,
  51. focusNode: _focusNode,
  52. decoration: InputDecoration(
  53. counterStyle: textTheme.mini.copyWith(color: colorScheme.textMuted),
  54. counterText: currentLength > 99
  55. ? currentLength.toString() + " / " + maxLength.toString()
  56. : "",
  57. contentPadding: const EdgeInsets.all(16),
  58. border: InputBorder.none,
  59. focusedBorder: InputBorder.none,
  60. filled: true,
  61. fillColor: colorScheme.fillFaint,
  62. hintText:
  63. caption == null || caption.isEmpty ? "Add a caption" : caption,
  64. hintStyle: getEnteTextTheme(context)
  65. .small
  66. .copyWith(color: colorScheme.textMuted),
  67. ),
  68. style: getEnteTextTheme(context).small,
  69. cursorWidth: 1.5,
  70. maxLength: maxLength,
  71. minLines: 1,
  72. maxLines: 6,
  73. keyboardType: TextInputType.text,
  74. onChanged: (value) {
  75. setState(() {
  76. currentLength = value.length;
  77. editedCaption = value;
  78. });
  79. },
  80. );
  81. }
  82. }