comments_input.dart 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import 'package:cpt_community/modules/community/newsfeed_detail/newsfeed_detail_vm.dart';
  2. import 'package:cs_resources/generated/l10n.dart';
  3. import 'package:cs_resources/theme/app_colors_theme.dart';
  4. import 'package:flutter/material.dart';
  5. import 'package:flutter_hooks/flutter_hooks.dart';
  6. import 'package:hooks_riverpod/hooks_riverpod.dart';
  7. import 'package:shared/utils/log_utils.dart';
  8. import 'package:widgets/my_text_view.dart';
  9. /// 多行输入框
  10. class CommentsTextareaInput extends HookConsumerWidget {
  11. bool showCounter = true;
  12. bool autoFocus = false;
  13. CommentsTextareaInput({Key? key,this.showCounter = true, this.autoFocus = false}): super(key: key);
  14. @override
  15. Widget build(BuildContext context, WidgetRef ref) {
  16. final state = ref.watch(newsfeedDetailVmProvider);
  17. final vm = ref.read(newsfeedDetailVmProvider.notifier);
  18. final noteCount = useState(0);
  19. useEffect((){
  20. return (){
  21. vm.resetCommentField();
  22. Log.d('dispose');
  23. };
  24. },[]);
  25. return _buildTextAreaLayout(context, ref, state, vm, noteCount);
  26. }
  27. Widget _buildTextAreaLayout(BuildContext context, ref, state, vm , noteCount){
  28. return Row(
  29. mainAxisSize: MainAxisSize.max,
  30. children: [
  31. Expanded(
  32. child: TextField(
  33. cursorColor: context.appColors.authFiledText,
  34. cursorWidth: 1.5,
  35. autofocus: autoFocus,
  36. textAlign: TextAlign.start,
  37. enabled: true,
  38. maxLines: null,
  39. expands: true,
  40. focusNode: state.commentFieldInfo['focusNode'],
  41. controller: state.commentFieldInfo!['controller'],
  42. decoration: InputDecoration(
  43. isDense: true,
  44. isCollapsed: true,
  45. border: InputBorder.none,
  46. hintText: state.commentFieldInfo!['hintText'],
  47. hintStyle: TextStyle(
  48. color: context.appColors.authFiledHint,
  49. fontSize: 14.0,
  50. fontWeight: FontWeight.w300,
  51. ),
  52. ),
  53. style: TextStyle(
  54. color: context.appColors.authFiledText,
  55. fontSize: 14.0,
  56. fontWeight: FontWeight.w300,
  57. ),
  58. textInputAction: TextInputAction.done,
  59. onSubmitted: (value) {
  60. // FocusScope.of(context).unfocus();
  61. },
  62. onChanged: (text) {
  63. // 当文本改变时,更新字符数量
  64. Log.d("text $text");
  65. Log.d("文本改变 ${state.commentFieldInfo!['controller']?.text}");
  66. if(showCounter){
  67. noteCount.value = text.length;
  68. }
  69. },
  70. ),
  71. ),
  72. ],
  73. );
  74. }
  75. }