text_input.dart 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. import 'package:flutter/material.dart';
  2. import 'package:flutter/services.dart';
  3. import 'package:sport/bean/comment.dart';
  4. import 'package:sport/bean/post_user.dart';
  5. import 'package:sport/pages/social/notification.dart';
  6. import 'package:sport/services/api/inject_api.dart';
  7. import 'package:sport/utils/toast.dart';
  8. import 'package:sport/widgets/dialog/bindphone_dialog.dart';
  9. import 'button_primary.dart';
  10. import 'decoration.dart';
  11. class TextInput extends StatefulWidget {
  12. final FocusNode? focusNode;
  13. final String subjectId;
  14. final String? parentCommentId;
  15. final String? toCommentId;
  16. final VoidCallback? callback;
  17. final bool comment;
  18. final PostUser? user;
  19. final bool autoFocus;
  20. final bool atUser;
  21. TextInput(this.subjectId,
  22. {this.focusNode,
  23. this.parentCommentId,
  24. this.toCommentId,
  25. this.callback,
  26. this.comment = false,
  27. this.user,
  28. this.autoFocus = false,
  29. this.atUser = true});
  30. @override
  31. State<StatefulWidget> createState() {
  32. return _TextInputState();
  33. }
  34. }
  35. class _TextInputState extends State<TextInput> with InjectApi {
  36. var _textFieldValue = "";
  37. TextEditingController? _controller;
  38. FocusNode? _focusNode;
  39. var _posting = false;
  40. ValueNotifier<bool> _postable = ValueNotifier(false);
  41. @override
  42. void initState() {
  43. super.initState();
  44. _focusNode = widget.focusNode ?? FocusNode();
  45. _controller = TextEditingController()
  46. ..addListener(() {
  47. _postable.value = _controller?.value.text.isNotEmpty ?? false;
  48. });
  49. if (widget.comment) {
  50. WidgetsBinding.instance?.addPostFrameCallback((timeStamp) {
  51. FocusScope.of(context).requestFocus(_focusNode);
  52. });
  53. }
  54. }
  55. @override
  56. void dispose() {
  57. _controller?.dispose();
  58. super.dispose();
  59. }
  60. @override
  61. Widget build(BuildContext context) {
  62. return Container(
  63. decoration: shadowTop(),
  64. child: ConstrainedBox(
  65. constraints: BoxConstraints(maxHeight: 150, minHeight: 50),
  66. child: Row(
  67. children: <Widget>[
  68. Expanded(
  69. child: Container(
  70. child: TextField(
  71. controller: _controller,
  72. focusNode: _focusNode,
  73. autofocus: widget.autoFocus,
  74. keyboardType: TextInputType.multiline,
  75. strutStyle: StrutStyle(height: 1.1),
  76. minLines: 1,
  77. maxLines: 3,
  78. maxLength: 200,
  79. onChanged: (value) {
  80. setState(() {
  81. _textFieldValue = value;
  82. });
  83. },
  84. style: TextStyle(fontSize: 15.0),
  85. decoration: InputDecoration(
  86. counterText: "",
  87. hintText: "发表你的看法...",
  88. hintStyle:
  89. TextStyle(color: Color(0xff999999), fontSize: 15.0),
  90. prefixIconConstraints: BoxConstraints(minWidth: 36),
  91. prefixIcon: Padding(
  92. padding: const EdgeInsets.fromLTRB(12.0, 0, 0, 0),
  93. child: Row(
  94. mainAxisSize: MainAxisSize.min,
  95. children: <Widget>[
  96. Image.asset(
  97. "lib/assets/img/bbs_icon_reportx.png"),
  98. SizedBox(
  99. width: 4,
  100. ),
  101. if (widget.user != null && widget.atUser == true)
  102. ConstrainedBox(
  103. constraints: BoxConstraints(maxWidth: 100),
  104. child: Text(
  105. "@${widget.user?.name}",
  106. maxLines: 1,
  107. overflow: TextOverflow.ellipsis,
  108. ))
  109. ],
  110. ),
  111. ),
  112. contentPadding: EdgeInsets.symmetric(vertical: 16.0),
  113. border: InputBorder.none),
  114. ),
  115. ),
  116. ),
  117. Padding(
  118. padding: const EdgeInsets.symmetric(horizontal: 12.0),
  119. child: _posting
  120. ? SizedBox(
  121. height: 22,
  122. width: 22,
  123. child: CircularProgressIndicator(),
  124. )
  125. :
  126. // PrimaryButton(
  127. // width: 80,
  128. // height: 35,
  129. // callback: _textFieldValue.isEmpty
  130. // ? null
  131. // : () async {
  132. // if (await showBindPhoneDialog(context) != true) {
  133. // return;
  134. // }
  135. // setState(() {
  136. // _posting = true;
  137. // });
  138. // var resp = await api
  139. // .postForumComment(widget.subjectId, _textFieldValue,
  140. // parentCommentId: widget.parentCommentId, toCommentId: widget.toCommentId)
  141. // .catchError((e) {});
  142. // if (resp != null && resp.code == 0) {
  143. // ToastUtil.show("发表成功");
  144. // _controller?.clear();
  145. // _focusNode?.unfocus();
  146. // widget.callback?.call();
  147. // _textFieldValue = '';
  148. // CommentNotification(Comment(id: resp.data.commentId, subjectId: widget.subjectId), CommentNotification.TYPE_ADD)
  149. // .dispatch(context);
  150. // }
  151. // setState(() {
  152. // _posting = false;
  153. // });
  154. //// SystemChannels.textInput.invokeMethod('TextInput.hide');
  155. // },
  156. // content: "发表",
  157. // ),
  158. ValueListenableBuilder(
  159. valueListenable: _postable,
  160. builder: (_, able, __) => PrimaryButton(
  161. content: "发表",
  162. width: 80,
  163. height: 35,
  164. callback: () async {
  165. if (await showBindPhoneDialog(context) !=
  166. true) {
  167. return;
  168. }
  169. setState(() {
  170. _posting = true;
  171. });
  172. var resp = await api
  173. .postForumComment(
  174. widget.subjectId, _textFieldValue,
  175. parentCommentId: widget.parentCommentId ?? "",
  176. toCommentId: widget.toCommentId ?? "")
  177. .catchError((e) {});
  178. if (resp != null && resp.code == 0) {
  179. ToastUtil.show("发表成功");
  180. _controller?.clear();
  181. _focusNode?.unfocus();
  182. widget.callback?.call();
  183. _textFieldValue = '';
  184. CommentNotification(
  185. Comment(
  186. id: resp.data?.commentId,
  187. subjectId: widget.subjectId),
  188. CommentNotification.TYPE_ADD)
  189. .dispatch(context);
  190. }
  191. setState(() {
  192. _posting = false;
  193. });
  194. },
  195. shadow: able == true,
  196. buttonColor: able == false
  197. ? Color(0xffFFC400).withOpacity(0.3)
  198. : null,
  199. )),
  200. )
  201. ],
  202. ),
  203. ),
  204. );
  205. }
  206. }