text_input.dart 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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, {this.focusNode, this.parentCommentId, this.toCommentId, this.callback, this.comment = false, this.user, this.autoFocus = false, this.atUser = true});
  22. @override
  23. State<StatefulWidget> createState() {
  24. return _TextInputState();
  25. }
  26. }
  27. class _TextInputState extends State<TextInput> with InjectApi {
  28. var _textFieldValue = "";
  29. TextEditingController _controller;
  30. FocusNode _focusNode;
  31. var _posting = false;
  32. @override
  33. void initState() {
  34. super.initState();
  35. _focusNode = widget.focusNode ?? FocusNode();
  36. _controller = TextEditingController();
  37. if (widget.comment) {
  38. WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
  39. FocusScope.of(context).requestFocus(_focusNode);
  40. });
  41. }
  42. }
  43. @override
  44. void dispose() {
  45. _controller?.dispose();
  46. super.dispose();
  47. }
  48. @override
  49. Widget build(BuildContext context) {
  50. return Container(
  51. decoration: shadowTop(),
  52. child: SafeArea(
  53. child: ConstrainedBox(
  54. constraints: BoxConstraints(maxHeight: 150, minHeight: 50),
  55. child: Row(
  56. children: <Widget>[
  57. Expanded(
  58. child: Container(
  59. child: TextField(
  60. controller: _controller,
  61. focusNode: _focusNode,
  62. autofocus: widget.autoFocus,
  63. keyboardType: TextInputType.multiline,
  64. strutStyle: StrutStyle(height: 1.1),
  65. minLines: 1,
  66. maxLines: 3,
  67. maxLength: 200,
  68. onChanged: (value) {
  69. setState(() {
  70. _textFieldValue = value;
  71. });
  72. },
  73. style: TextStyle(fontSize: 15.0),
  74. decoration: InputDecoration(
  75. counterText: "",
  76. hintText: "发表你的看法...",
  77. hintStyle: TextStyle(color: Color(0xff999999),fontSize: 15.0),
  78. prefixIconConstraints: BoxConstraints(minWidth: 36),
  79. prefixIcon: Padding(
  80. padding: const EdgeInsets.fromLTRB(12.0 ,0,0,0),
  81. child: Row(
  82. mainAxisSize: MainAxisSize.min,
  83. children: <Widget>[
  84. Image.asset("lib/assets/img/bbs_icon_reportx.png"),
  85. SizedBox(
  86. width: 4,
  87. ),
  88. if (widget.user != null && widget.atUser == true)
  89. ConstrainedBox(
  90. constraints: BoxConstraints(maxWidth: 100),
  91. child: Text(
  92. "@${widget.user.name}",
  93. maxLines: 1,
  94. overflow: TextOverflow.ellipsis,
  95. ))
  96. ],
  97. ),
  98. ),
  99. contentPadding: EdgeInsets.symmetric(vertical: 16.0),
  100. border: InputBorder.none),
  101. ),
  102. ),
  103. ),
  104. Padding(
  105. padding: const EdgeInsets.symmetric(horizontal: 12.0),
  106. child: _posting
  107. ? SizedBox(
  108. height: 22,
  109. width: 22,
  110. child: CircularProgressIndicator(),
  111. )
  112. : PrimaryButton(
  113. width: 80,
  114. height: 35,
  115. callback: _textFieldValue.isEmpty
  116. ? null
  117. : () async {
  118. if (await showBindPhoneDialog(context) != true) {
  119. return;
  120. }
  121. setState(() {
  122. _posting = true;
  123. });
  124. var resp = await api
  125. .postForumComment(widget.subjectId, _textFieldValue,
  126. parentCommentId: widget.parentCommentId, toCommentId: widget.toCommentId)
  127. .catchError((e) {});
  128. if (resp != null && resp.code == 0) {
  129. ToastUtil.show("发表成功");
  130. _controller?.clear();
  131. _focusNode?.unfocus();
  132. widget.callback?.call();
  133. _textFieldValue = '';
  134. CommentNotification(Comment(id: resp.data.commentId, subjectId: widget.subjectId), CommentNotification.TYPE_ADD)
  135. .dispatch(context);
  136. }
  137. setState(() {
  138. _posting = false;
  139. });
  140. // SystemChannels.textInput.invokeMethod('TextInput.hide');
  141. },
  142. content: "发表",
  143. ),
  144. )
  145. ],
  146. ),
  147. ),
  148. ),
  149. );
  150. }
  151. }