ente/lib/ui/viewer/file/file_caption_widget.dart

116 lines
3.4 KiB
Dart
Raw Normal View History

import 'package:flutter/material.dart';
2022-11-07 08:44:56 +00:00
import 'package:photos/core/constants.dart';
import 'package:photos/models/file.dart';
import 'package:photos/theme/ente_theme.dart';
import 'package:photos/utils/magic_util.dart';
class FileCaptionWidget extends StatefulWidget {
final File file;
const FileCaptionWidget({required this.file, super.key});
@override
State<FileCaptionWidget> createState() => _FileCaptionWidgetState();
}
class _FileCaptionWidgetState extends State<FileCaptionWidget> {
static const int maxLength = 5000;
2022-11-26 09:02:27 +00:00
// counterThreshold represents the nun of char after which
// currentLength/maxLength will show up
static const int counterThreshold = 1000;
int currentLength = 0;
final _textController = TextEditingController();
final _focusNode = FocusNode();
String? editedCaption;
2022-11-07 08:44:56 +00:00
String hintText = fileCaptionDefaultHint;
@override
void initState() {
2022-11-18 12:57:39 +00:00
_focusNode.addListener(_focusNodeListener);
editedCaption = widget.file.caption;
2022-11-05 18:02:03 +00:00
if (editedCaption != null && editedCaption!.isNotEmpty) {
2022-11-07 08:44:56 +00:00
hintText = editedCaption!;
2022-11-05 18:02:03 +00:00
}
super.initState();
}
@override
void dispose() {
if (editedCaption != null) {
editFileCaption(null, widget.file, editedCaption);
}
_textController.dispose();
2022-11-18 12:57:39 +00:00
_focusNode.removeListener(_focusNodeListener);
super.dispose();
}
@override
Widget build(BuildContext context) {
final colorScheme = getEnteColorScheme(context);
final textTheme = getEnteTextTheme(context);
return TextField(
2022-11-07 07:56:50 +00:00
onSubmitted: (value) async {
if (editedCaption != null) {
2022-11-07 07:56:50 +00:00
final isSuccesful =
await editFileCaption(context, widget.file, editedCaption);
if (isSuccesful) {
2022-11-07 09:02:39 +00:00
if (mounted) {
Navigator.pop(context);
}
2022-11-05 11:48:28 +00:00
}
}
},
controller: _textController,
focusNode: _focusNode,
decoration: InputDecoration(
counterStyle: textTheme.mini.copyWith(color: colorScheme.textMuted),
2022-11-26 09:02:27 +00:00
counterText: currentLength >= counterThreshold
? currentLength.toString() + " / " + maxLength.toString()
: "",
contentPadding: const EdgeInsets.all(16),
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(2),
borderSide: const BorderSide(
width: 0,
style: BorderStyle.none,
),
),
focusedBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(2),
borderSide: const BorderSide(
width: 0,
style: BorderStyle.none,
),
),
filled: true,
2022-11-06 05:28:25 +00:00
fillColor: colorScheme.fillFaint,
2022-11-05 18:02:03 +00:00
hintText: hintText,
2022-11-07 08:44:56 +00:00
hintStyle: hintText == fileCaptionDefaultHint
? textTheme.small.copyWith(color: colorScheme.textMuted)
: textTheme.small,
),
2022-11-07 08:44:56 +00:00
style: textTheme.small,
cursorWidth: 1.5,
maxLength: maxLength,
minLines: 1,
maxLines: 10,
textCapitalization: TextCapitalization.sentences,
keyboardType: TextInputType.text,
onChanged: (value) {
setState(() {
2022-11-07 08:44:56 +00:00
hintText = fileCaptionDefaultHint;
currentLength = value.length;
editedCaption = value;
});
},
);
}
2022-11-18 12:57:39 +00:00
void _focusNodeListener() {
final caption = widget.file.caption;
if (_focusNode.hasFocus && caption != null) {
_textController.text = caption;
editedCaption = caption;
}
}
}