feat: richtextfield

Signed-off-by: bggRGjQaUbCoE <githubaccount56556@proton.me>
This commit is contained in:
bggRGjQaUbCoE
2025-06-27 12:02:32 +08:00
parent 721bf2d59f
commit 6f2570c5be
26 changed files with 7154 additions and 870 deletions

View File

@@ -1,506 +0,0 @@
import 'dart:async';
import 'dart:io';
import 'dart:math';
import 'package:PiliPlus/common/widgets/button/icon_button.dart';
import 'package:PiliPlus/http/msg.dart';
import 'package:PiliPlus/models/common/image_preview_type.dart';
import 'package:PiliPlus/models/common/publish_panel_type.dart';
import 'package:PiliPlus/models_new/dynamic/dyn_mention/item.dart';
import 'package:PiliPlus/models_new/emote/emote.dart';
import 'package:PiliPlus/models_new/live/live_emote/emoticon.dart';
import 'package:PiliPlus/models_new/upload_bfs/data.dart';
import 'package:PiliPlus/pages/dynamics_mention/view.dart';
import 'package:PiliPlus/utils/extension.dart';
import 'package:PiliPlus/utils/feed_back.dart';
import 'package:chat_bottom_container/chat_bottom_container.dart';
import 'package:dio/dio.dart';
import 'package:easy_debounce/easy_throttle.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:get/get.dart';
import 'package:image_cropper/image_cropper.dart';
import 'package:image_picker/image_picker.dart';
abstract class CommonPublishPage extends StatefulWidget {
const CommonPublishPage({
super.key,
this.initialValue,
this.mentions,
this.imageLengthLimit,
this.onSave,
this.autofocus = true,
});
final String? initialValue;
final List<MentionItem>? mentions;
final int? imageLengthLimit;
final ValueChanged<({String text, List<MentionItem>? mentions})>? onSave;
final bool autofocus;
}
abstract class CommonPublishPageState<T extends CommonPublishPage>
extends State<T> with WidgetsBindingObserver {
late final focusNode = FocusNode();
late final controller = ChatBottomPanelContainerController<PanelType>();
late final editController = TextEditingController(text: widget.initialValue);
Rx<PanelType> panelType = PanelType.none.obs;
late final RxBool readOnly = false.obs;
late final RxBool enablePublish = false.obs;
late final imagePicker = ImagePicker();
late final RxList<String> pathList = <String>[].obs;
int get limit => widget.imageLengthLimit ?? 9;
List<MentionItem>? mentions;
@override
void initState() {
super.initState();
WidgetsBinding.instance.addObserver(this);
mentions = widget.mentions;
if (widget.initialValue?.trim().isNotEmpty == true) {
enablePublish.value = true;
}
if (widget.autofocus) {
Future.delayed(const Duration(milliseconds: 300)).whenComplete(() {
if (mounted) {
focusNode.requestFocus();
}
});
}
}
@override
void dispose() {
focusNode.dispose();
editController.dispose();
WidgetsBinding.instance.removeObserver(this);
super.dispose();
}
Future<void> _requestFocus() async {
await Future.delayed(const Duration(microseconds: 200));
focusNode.requestFocus();
}
@override
void didChangeAppLifecycleState(AppLifecycleState state) {
if (state == AppLifecycleState.resumed) {
if (mounted &&
widget.autofocus &&
panelType.value == PanelType.keyboard) {
WidgetsBinding.instance.addPostFrameCallback((_) {
if (focusNode.hasFocus) {
focusNode.unfocus();
_requestFocus();
} else {
_requestFocus();
}
});
}
} else if (state == AppLifecycleState.paused) {
controller.keepChatPanel();
if (focusNode.hasFocus) {
focusNode.unfocus();
}
}
}
void updatePanelType(PanelType type) {
final isSwitchToKeyboard = PanelType.keyboard == type;
final isSwitchToEmojiPanel = PanelType.emoji == type;
bool isUpdated = false;
switch (type) {
case PanelType.keyboard:
updateInputView(isReadOnly: false);
break;
case PanelType.emoji:
isUpdated = updateInputView(isReadOnly: true);
break;
default:
break;
}
void updatePanelTypeFunc() {
controller.updatePanelType(
isSwitchToKeyboard
? ChatBottomPanelType.keyboard
: ChatBottomPanelType.other,
data: type,
forceHandleFocus: isSwitchToEmojiPanel
? ChatBottomHandleFocus.requestFocus
: ChatBottomHandleFocus.none,
);
}
if (isUpdated) {
// Waiting for the input view to update.
WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
updatePanelTypeFunc();
});
} else {
updatePanelTypeFunc();
}
}
Future<void> hidePanel() async {
if (focusNode.hasFocus) {
await Future.delayed(const Duration(milliseconds: 100));
focusNode.unfocus();
}
updateInputView(isReadOnly: false);
if (ChatBottomPanelType.none == controller.currentPanelType) return;
controller.updatePanelType(ChatBottomPanelType.none);
}
bool updateInputView({
required bool isReadOnly,
}) {
if (readOnly.value != isReadOnly) {
readOnly.value = isReadOnly;
return true;
}
return false;
}
Future<void> onPublish() async {
feedBack();
List<Map<String, dynamic>>? pictures;
if (pathList.isNotEmpty) {
SmartDialog.showLoading(msg: '正在上传图片...');
final cancelToken = CancelToken();
try {
pictures = await Future.wait<Map<String, dynamic>>(
pathList.map((path) async {
Map result = await MsgHttp.uploadBfs(
path: path,
category: 'daily',
biz: 'new_dyn',
cancelToken: cancelToken,
);
if (!result['status']) throw HttpException(result['msg']);
UploadBfsResData data = result['data'];
return {
'img_width': data.imageWidth,
'img_height': data.imageHeight,
'img_size': data.imgSize,
'img_src': data.imageUrl,
};
}).toList(),
eagerError: true);
SmartDialog.dismiss();
} on HttpException catch (e) {
cancelToken.cancel();
SmartDialog.dismiss();
SmartDialog.showToast(e.message);
return;
}
}
onCustomPublish(message: editController.text, pictures: pictures);
}
Future<void> onCustomPublish({required String message, List? pictures});
void onChooseEmote(dynamic emote) {
if (emote is Emote) {
onInsertText(emote.text!);
} else if (emote is Emoticon) {
onInsertText(emote.emoji!);
}
}
Widget? get customPanel => null;
Widget buildEmojiPickerPanel() {
double height = context.isTablet ? 300 : 170;
final keyboardHeight = controller.keyboardHeight;
if (keyboardHeight != 0) {
height = max(height, keyboardHeight);
}
return SizedBox(
height: height,
child: customPanel,
);
}
Widget buildPanelContainer([Color? panelBgColor]) {
return ChatBottomPanelContainer<PanelType>(
controller: controller,
inputFocusNode: focusNode,
otherPanelWidget: (type) {
if (type == null) return const SizedBox.shrink();
switch (type) {
case PanelType.emoji:
return buildEmojiPickerPanel();
default:
return const SizedBox.shrink();
}
},
onPanelTypeChange: (panelType, data) {
// if (kDebugMode) debugPrint('panelType: $panelType');
switch (panelType) {
case ChatBottomPanelType.none:
this.panelType.value = PanelType.none;
break;
case ChatBottomPanelType.keyboard:
this.panelType.value = PanelType.keyboard;
break;
case ChatBottomPanelType.other:
if (data == null) return;
switch (data) {
case PanelType.emoji:
this.panelType.value = PanelType.emoji;
break;
default:
this.panelType.value = PanelType.none;
break;
}
break;
}
},
panelBgColor: panelBgColor ?? Theme.of(context).colorScheme.surface,
);
}
Widget buildImage(int index, double height) {
final color =
Theme.of(context).colorScheme.secondaryContainer.withValues(alpha: 0.5);
void onClear() {
pathList.removeAt(index);
if (pathList.isEmpty && editController.text.trim().isEmpty) {
enablePublish.value = false;
}
}
return Stack(
clipBehavior: Clip.none,
children: [
GestureDetector(
onTap: () {
controller.keepChatPanel();
context.imageView(
imgList: pathList
.map((path) => SourceModel(
url: path,
sourceType: SourceType.fileImage,
))
.toList(),
initialPage: index,
);
},
onLongPress: onClear,
child: ClipRRect(
borderRadius: const BorderRadius.all(Radius.circular(4)),
child: Image(
height: height,
fit: BoxFit.fitHeight,
filterQuality: FilterQuality.low,
image: FileImage(File(pathList[index])),
),
),
),
Positioned(
top: 34,
right: 5,
child: iconButton(
context: context,
icon: Icons.edit,
onPressed: () => onCropImage(index),
size: 24,
iconSize: 14,
bgColor: color,
),
),
Positioned(
top: 5,
right: 5,
child: iconButton(
context: context,
icon: Icons.clear,
onPressed: onClear,
size: 24,
iconSize: 14,
bgColor: color,
),
),
],
);
}
Future<void> onCropImage(int index) async {
final theme = Theme.of(context);
CroppedFile? croppedFile = await ImageCropper().cropImage(
sourcePath: pathList[index],
uiSettings: [
AndroidUiSettings(
toolbarTitle: '裁剪',
toolbarColor: theme.colorScheme.secondaryContainer,
toolbarWidgetColor: theme.colorScheme.onSecondaryContainer,
),
IOSUiSettings(
title: '裁剪',
),
],
);
if (croppedFile != null) {
pathList[index] = croppedFile.path;
}
}
void onPickImage([VoidCallback? callback]) {
EasyThrottle.throttle('imagePicker', const Duration(milliseconds: 500),
() async {
try {
List<XFile> pickedFiles = await imagePicker.pickMultiImage(
limit: limit,
imageQuality: 100,
);
if (pickedFiles.isNotEmpty) {
for (int i = 0; i < pickedFiles.length; i++) {
if (pathList.length == limit) {
SmartDialog.showToast('最多选择$limit张图片');
break;
} else {
pathList.add(pickedFiles[i].path);
}
}
callback?.call();
}
} catch (e) {
SmartDialog.showToast(e.toString());
}
});
}
List<Map<String, dynamic>>? getRichContent() {
if (mentions.isNullOrEmpty) {
return null;
}
List<Map<String, dynamic>> content = [];
void addPlainText(String text) {
content.add({
"raw_text": text,
"type": 1,
"biz_id": "",
});
}
final pattern = RegExp(
mentions!.toSet().map((e) => RegExp.escape('@${e.name!}')).join('|'));
editController.text.splitMapJoin(
pattern,
onMatch: (Match match) {
final name = match.group(0)!;
final item =
mentions!.firstWhereOrNull((e) => e.name == name.substring(1));
if (item != null) {
content.add({
"raw_text": name,
"type": 2,
"biz_id": item.uid,
});
} else {
addPlainText(name);
}
return '';
},
onNonMatch: (String text) {
addPlainText(text);
return '';
},
);
return content;
}
double _mentionOffset = 0;
void onMention([bool fromClick = false]) {
controller.keepChatPanel();
DynMentionPanel.onDynMention(
context,
offset: _mentionOffset,
callback: (offset) => _mentionOffset = offset,
).then((MentionItem? res) {
if (res != null) {
(mentions ??= <MentionItem>[]).add(res);
String atName = '${fromClick ? '@' : ''}${res.name} ';
onInsertText(atName);
}
});
}
void onInsertText(String text) {
if (text.isEmpty) {
return;
}
enablePublish.value = true;
final oldValue = editController.value;
final selection = oldValue.selection;
if (selection.isValid) {
TextEditingDelta delta;
if (selection.isCollapsed) {
delta = TextEditingDeltaInsertion(
oldText: oldValue.text,
textInserted: text,
insertionOffset: selection.start,
selection: TextSelection.collapsed(
offset: selection.start + text.length,
),
composing: TextRange.empty,
);
} else {
delta = TextEditingDeltaReplacement(
oldText: oldValue.text,
replacementText: text,
replacedRange: selection,
selection: TextSelection.collapsed(
offset: selection.start + text.length,
),
composing: TextRange.empty,
);
}
final newValue = delta.apply(oldValue);
if (oldValue == newValue) {
return;
}
editController.value = newValue;
} else {
editController.value = TextEditingValue(
text: text,
selection: TextSelection.collapsed(offset: text.length),
);
}
widget.onSave?.call((text: editController.text, mentions: mentions));
}
void onDelAtUser(String name) {
mentions!.removeFirstWhere((e) => e.name == name);
}
void onChanged(String value) {
bool isEmpty = value.trim().isEmpty;
if (isEmpty) {
enablePublish.value = false;
mentions?.clear();
} else {
enablePublish.value = true;
}
widget.onSave?.call((text: value, mentions: mentions));
}
}

View File

@@ -0,0 +1,256 @@
import 'dart:async';
import 'dart:io';
import 'dart:math' show max;
import 'package:PiliPlus/http/msg.dart';
import 'package:PiliPlus/models/common/publish_panel_type.dart';
import 'package:PiliPlus/models_new/upload_bfs/data.dart';
import 'package:PiliPlus/utils/feed_back.dart';
import 'package:chat_bottom_container/chat_bottom_container.dart';
import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:get/get.dart';
import 'package:image_picker/image_picker.dart';
abstract class CommonPublishPage<T> extends StatefulWidget {
const CommonPublishPage({
super.key,
this.initialValue,
this.imageLengthLimit,
this.onSave,
this.autofocus = true,
});
final String? initialValue;
final int? imageLengthLimit;
final ValueChanged<T>? onSave;
final bool autofocus;
}
abstract class CommonPublishPageState<T extends CommonPublishPage>
extends State<T> with WidgetsBindingObserver {
late final focusNode = FocusNode();
late final controller = ChatBottomPanelContainerController<PanelType>();
TextEditingController get editController;
Rx<PanelType> panelType = PanelType.none.obs;
late final RxBool readOnly = false.obs;
late final RxBool enablePublish = false.obs;
late final imagePicker = ImagePicker();
late final RxList<String> pathList = <String>[].obs;
int get limit => widget.imageLengthLimit ?? 9;
bool? hasPub;
void initPubState();
@override
void initState() {
super.initState();
WidgetsBinding.instance.addObserver(this);
initPubState();
if (widget.autofocus) {
Future.delayed(const Duration(milliseconds: 300)).whenComplete(() {
if (mounted) {
focusNode.requestFocus();
}
});
}
}
@override
void dispose() {
if (hasPub != true) {
onSave();
}
focusNode.dispose();
editController.dispose();
WidgetsBinding.instance.removeObserver(this);
super.dispose();
}
Future<void> _requestFocus() async {
await Future.delayed(const Duration(microseconds: 200));
focusNode.requestFocus();
}
@override
void didChangeAppLifecycleState(AppLifecycleState state) {
if (state == AppLifecycleState.resumed) {
if (mounted &&
widget.autofocus &&
panelType.value == PanelType.keyboard) {
WidgetsBinding.instance.addPostFrameCallback((_) {
if (focusNode.hasFocus) {
focusNode.unfocus();
_requestFocus();
} else {
_requestFocus();
}
});
}
} else if (state == AppLifecycleState.paused) {
controller.keepChatPanel();
if (focusNode.hasFocus) {
focusNode.unfocus();
}
}
}
void updatePanelType(PanelType type) {
final isSwitchToKeyboard = PanelType.keyboard == type;
final isSwitchToEmojiPanel = PanelType.emoji == type;
bool isUpdated = false;
switch (type) {
case PanelType.keyboard:
updateInputView(isReadOnly: false);
break;
case PanelType.emoji:
isUpdated = updateInputView(isReadOnly: true);
break;
default:
break;
}
void updatePanelTypeFunc() {
controller.updatePanelType(
isSwitchToKeyboard
? ChatBottomPanelType.keyboard
: ChatBottomPanelType.other,
data: type,
forceHandleFocus: isSwitchToEmojiPanel
? ChatBottomHandleFocus.requestFocus
: ChatBottomHandleFocus.none,
);
}
if (isUpdated) {
// Waiting for the input view to update.
WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
updatePanelTypeFunc();
});
} else {
updatePanelTypeFunc();
}
}
Future<void> hidePanel() async {
if (focusNode.hasFocus) {
await Future.delayed(const Duration(milliseconds: 100));
focusNode.unfocus();
}
updateInputView(isReadOnly: false);
if (ChatBottomPanelType.none == controller.currentPanelType) return;
controller.updatePanelType(ChatBottomPanelType.none);
}
bool updateInputView({
required bool isReadOnly,
}) {
if (readOnly.value != isReadOnly) {
readOnly.value = isReadOnly;
return true;
}
return false;
}
Widget buildEmojiPickerPanel() {
double height = context.isTablet ? 300 : 170;
final keyboardHeight = controller.keyboardHeight;
if (keyboardHeight != 0) {
height = max(height, keyboardHeight);
}
return SizedBox(
height: height,
child: customPanel,
);
}
Widget buildPanelContainer([Color? panelBgColor]) {
return ChatBottomPanelContainer<PanelType>(
controller: controller,
inputFocusNode: focusNode,
otherPanelWidget: (type) {
if (type == null) return const SizedBox.shrink();
switch (type) {
case PanelType.emoji:
return buildEmojiPickerPanel();
default:
return const SizedBox.shrink();
}
},
onPanelTypeChange: (panelType, data) {
// if (kDebugMode) debugPrint('panelType: $panelType');
switch (panelType) {
case ChatBottomPanelType.none:
this.panelType.value = PanelType.none;
break;
case ChatBottomPanelType.keyboard:
this.panelType.value = PanelType.keyboard;
break;
case ChatBottomPanelType.other:
if (data == null) return;
switch (data) {
case PanelType.emoji:
this.panelType.value = PanelType.emoji;
break;
default:
this.panelType.value = PanelType.none;
break;
}
break;
}
},
panelBgColor: panelBgColor ?? Theme.of(context).colorScheme.surface,
);
}
Future<void> onPublish() async {
feedBack();
List<Map<String, dynamic>>? pictures;
if (pathList.isNotEmpty) {
SmartDialog.showLoading(msg: '正在上传图片...');
final cancelToken = CancelToken();
try {
pictures = await Future.wait<Map<String, dynamic>>(
pathList.map((path) async {
Map result = await MsgHttp.uploadBfs(
path: path,
category: 'daily',
biz: 'new_dyn',
cancelToken: cancelToken,
);
if (!result['status']) throw HttpException(result['msg']);
UploadBfsResData data = result['data'];
return {
'img_width': data.imageWidth,
'img_height': data.imageHeight,
'img_size': data.imgSize,
'img_src': data.imageUrl,
};
}).toList(),
eagerError: true);
SmartDialog.dismiss();
} on HttpException catch (e) {
cancelToken.cancel();
SmartDialog.dismiss();
SmartDialog.showToast(e.message);
return;
}
}
onCustomPublish(pictures: pictures);
}
Future<void> onCustomPublish({List? pictures});
Widget? get customPanel => null;
void onChanged(String value) {
enablePublish.value = value.trim().isNotEmpty;
}
void onSave() {}
}

View File

@@ -0,0 +1,342 @@
import 'dart:io';
import 'package:PiliPlus/common/widgets/button/icon_button.dart';
import 'package:PiliPlus/common/widgets/text_field/controller.dart';
import 'package:PiliPlus/models/common/image_preview_type.dart';
import 'package:PiliPlus/models_new/dynamic/dyn_mention/item.dart';
import 'package:PiliPlus/models_new/emote/emote.dart' as e;
import 'package:PiliPlus/models_new/live/live_emote/emoticon.dart';
import 'package:PiliPlus/pages/common/publish/common_publish_page.dart';
import 'package:PiliPlus/pages/dynamics_mention/view.dart';
import 'package:PiliPlus/utils/extension.dart';
import 'package:easy_debounce/easy_throttle.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:image_cropper/image_cropper.dart';
import 'package:image_picker/image_picker.dart';
abstract class CommonRichTextPubPage
extends CommonPublishPage<List<RichTextItem>> {
const CommonRichTextPubPage({
super.key,
this.items,
super.onSave,
super.autofocus,
super.imageLengthLimit,
});
final List<RichTextItem>? items;
}
abstract class CommonRichTextPubPageState<T extends CommonRichTextPubPage>
extends CommonPublishPageState<T> {
bool? hasPub;
@override
late final RichTextEditingController editController =
RichTextEditingController(
items: widget.items,
onMention: onMention,
);
@override
void initPubState() {
if (editController.rawText.trim().isNotEmpty) {
enablePublish.value = true;
}
}
@override
void didChangeDependencies() {
editController.richStyle = null;
super.didChangeDependencies();
}
Widget buildImage(int index, double height) {
final color =
Theme.of(context).colorScheme.secondaryContainer.withValues(alpha: 0.5);
void onClear() {
pathList.removeAt(index);
if (pathList.isEmpty && editController.rawText.trim().isEmpty) {
enablePublish.value = false;
}
}
return Stack(
clipBehavior: Clip.none,
children: [
GestureDetector(
onTap: () {
controller.keepChatPanel();
context.imageView(
imgList: pathList
.map((path) => SourceModel(
url: path,
sourceType: SourceType.fileImage,
))
.toList(),
initialPage: index,
);
},
onLongPress: onClear,
child: ClipRRect(
borderRadius: const BorderRadius.all(Radius.circular(4)),
child: Image(
height: height,
fit: BoxFit.fitHeight,
filterQuality: FilterQuality.low,
image: FileImage(File(pathList[index])),
),
),
),
Positioned(
top: 34,
right: 5,
child: iconButton(
context: context,
icon: Icons.edit,
onPressed: () => onCropImage(index),
size: 24,
iconSize: 14,
bgColor: color,
),
),
Positioned(
top: 5,
right: 5,
child: iconButton(
context: context,
icon: Icons.clear,
onPressed: onClear,
size: 24,
iconSize: 14,
bgColor: color,
),
),
],
);
}
Future<void> onCropImage(int index) async {
final theme = Theme.of(context);
CroppedFile? croppedFile = await ImageCropper().cropImage(
sourcePath: pathList[index],
uiSettings: [
AndroidUiSettings(
toolbarTitle: '裁剪',
toolbarColor: theme.colorScheme.secondaryContainer,
toolbarWidgetColor: theme.colorScheme.onSecondaryContainer,
),
IOSUiSettings(
title: '裁剪',
),
],
);
if (croppedFile != null) {
pathList[index] = croppedFile.path;
}
}
void onPickImage([VoidCallback? callback]) {
EasyThrottle.throttle('imagePicker', const Duration(milliseconds: 500),
() async {
try {
List<XFile> pickedFiles = await imagePicker.pickMultiImage(
limit: limit,
imageQuality: 100,
);
if (pickedFiles.isNotEmpty) {
for (int i = 0; i < pickedFiles.length; i++) {
if (pathList.length == limit) {
SmartDialog.showToast('最多选择$limit张图片');
break;
} else {
pathList.add(pickedFiles[i].path);
}
}
callback?.call();
}
} catch (e) {
SmartDialog.showToast(e.toString());
}
});
}
void onChooseEmote(dynamic emote, double? width, double? height) {
if (emote is e.Emote) {
final isTextEmote = width == null;
onInsertText(
isTextEmote ? emote.text! : '\uFFFC',
RichTextType.emoji,
rawText: emote.text!,
emote: isTextEmote
? null
: Emote(
url: emote.url!,
width: width,
height: height,
),
);
} else if (emote is Emoticon) {
onInsertText(
'\uFFFC',
RichTextType.emoji,
rawText: emote.emoji!,
emote: Emote(
url: emote.url!,
width: width!,
height: height,
),
);
}
}
List<Map<String, dynamic>>? getRichContent() {
if (editController.items.isEmpty) return null;
return editController.items.map((e) {
return switch (e.type) {
RichTextType.text || RichTextType.composing => <String, dynamic>{
"raw_text": e.text,
"type": 1,
"biz_id": "",
},
RichTextType.at => <String, dynamic>{
"raw_text": '@${e.rawText}',
"type": 2,
"biz_id": e.uid,
},
RichTextType.emoji => <String, dynamic>{
"raw_text": e.rawText,
"type": 9,
"biz_id": "",
},
};
}).toList();
}
double _mentionOffset = 0;
void onMention([bool fromClick = false]) {
controller.keepChatPanel();
DynMentionPanel.onDynMention(
context,
offset: _mentionOffset,
callback: (offset) => _mentionOffset = offset,
).then((MentionItem? res) {
if (res != null) {
onInsertText(
'@${res.name} ',
RichTextType.at,
rawText: res.name,
uid: res.uid,
fromClick: fromClick,
);
}
});
}
void onInsertText(
String text,
RichTextType type, {
String? rawText,
Emote? emote,
String? uid,
bool? fromClick,
}) {
if (text.isEmpty) {
return;
}
enablePublish.value = true;
var oldValue = editController.value;
final selection = oldValue.selection;
if (selection.isValid) {
TextEditingDelta delta;
if (selection.isCollapsed) {
if (type == RichTextType.at && fromClick == false) {
delta = RichTextEditingDeltaReplacement(
oldText: oldValue.text,
replacementText: text,
replacedRange:
TextRange(start: selection.start - 1, end: selection.end),
selection: TextSelection.collapsed(
offset: selection.start - 1 + text.length,
),
composing: TextRange.empty,
rawText: rawText,
type: type,
emote: emote,
uid: uid,
);
} else {
delta = RichTextEditingDeltaInsertion(
oldText: oldValue.text,
textInserted: text,
insertionOffset: selection.start,
selection: TextSelection.collapsed(
offset: selection.start + text.length,
),
composing: TextRange.empty,
rawText: rawText,
type: type,
emote: emote,
uid: uid,
);
}
} else {
delta = RichTextEditingDeltaReplacement(
oldText: oldValue.text,
replacementText: text,
replacedRange: selection,
selection: TextSelection.collapsed(
offset: selection.start + text.length,
),
composing: TextRange.empty,
rawText: rawText,
type: type,
emote: emote,
uid: uid,
);
}
final newValue = delta.apply(oldValue);
if (oldValue == newValue) {
return;
}
editController
..value = newValue
..syncRichText(delta);
} else {
editController.value = TextEditingValue(
text: text,
selection: TextSelection.collapsed(offset: text.length),
);
editController.items
..clear()
..add(
RichTextItem(
type: type,
text: text,
rawText: rawText,
range: TextRange(
start: 0,
end: text.length,
),
emote: emote,
uid: uid,
),
);
}
}
@override
void onSave() {
widget.onSave?.call(editController.items);
}
}

View File

@@ -0,0 +1,29 @@
import 'package:PiliPlus/pages/common/publish/common_publish_page.dart';
import 'package:flutter/material.dart';
abstract class CommonTextPubPage extends CommonPublishPage<String> {
const CommonTextPubPage({
super.key,
super.initialValue,
super.onSave,
});
}
abstract class CommonTextPubPageState<T extends CommonTextPubPage>
extends CommonPublishPageState<T> {
@override
late final TextEditingController editController =
TextEditingController(text: widget.initialValue);
@override
void initPubState() {
if (widget.initialValue?.trim().isNotEmpty == true) {
enablePublish.value = true;
}
}
@override
void onSave() {
widget.onSave?.call(editController.text);
}
}

View File

@@ -1,10 +1,10 @@
import 'package:PiliPlus/common/widgets/text_field/controller.dart';
import 'package:PiliPlus/grpc/bilibili/main/community/reply/v1.pb.dart'
show MainListReply, ReplyInfo, SubjectControl, Mode;
import 'package:PiliPlus/grpc/bilibili/pagination.pb.dart';
import 'package:PiliPlus/http/loading_state.dart';
import 'package:PiliPlus/http/reply.dart';
import 'package:PiliPlus/models/common/reply/reply_sort_type.dart';
import 'package:PiliPlus/models_new/dynamic/dyn_mention/item.dart';
import 'package:PiliPlus/pages/common/common_list_controller.dart';
import 'package:PiliPlus/pages/video/reply_new/view.dart';
import 'package:PiliPlus/services/account_service.dart';
@@ -25,8 +25,7 @@ abstract class ReplyController<R> extends CommonListController<R, ReplyInfo> {
late Rx<ReplySortType> sortType;
late Rx<Mode> mode;
late final savedReplies =
<Object, ({String text, List<MentionItem>? mentions})?>{};
late final savedReplies = <Object, List<RichTextItem>?>{};
AccountService accountService = Get.find<AccountService>();
@@ -127,16 +126,20 @@ abstract class ReplyController<R> extends CommonListController<R, ReplyInfo> {
.push(
GetDialogRoute(
pageBuilder: (buildContext, animation, secondaryAnimation) {
final saved = savedReplies[key];
return ReplyPage(
oid: oid ?? replyItem!.oid.toInt(),
root: oid != null ? 0 : replyItem!.id.toInt(),
parent: oid != null ? 0 : replyItem!.id.toInt(),
replyType: replyItem?.type.toInt() ?? replyType!,
replyItem: replyItem,
initialValue: saved?.text,
mentions: saved?.mentions,
onSave: (reply) => savedReplies[key] = reply,
items: savedReplies[key],
onSave: (reply) {
if (reply.isEmpty) {
savedReplies.remove(key);
} else {
savedReplies[key] = reply.toList();
}
},
hint: hint,
);
},
@@ -238,4 +241,10 @@ abstract class ReplyController<R> extends CommonListController<R, ReplyInfo> {
SmartDialog.showToast(res['msg']);
}
}
@override
void onClose() {
savedReplies.clear();
super.onClose();
}
}