123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215 |
- import 'package:flutter/material.dart';
- import 'package:flutter/services.dart';
- import 'package:sport/bean/comment.dart';
- import 'package:sport/bean/post_user.dart';
- import 'package:sport/pages/social/notification.dart';
- import 'package:sport/services/api/inject_api.dart';
- import 'package:sport/utils/toast.dart';
- import 'package:sport/widgets/dialog/bindphone_dialog.dart';
- import 'button_primary.dart';
- import 'decoration.dart';
- class TextInput extends StatefulWidget {
- final FocusNode? focusNode;
- final String subjectId;
- final String? parentCommentId;
- final String? toCommentId;
- final VoidCallback? callback;
- final bool comment;
- final PostUser? user;
- final bool autoFocus;
- final bool atUser;
- TextInput(this.subjectId,
- {this.focusNode,
- this.parentCommentId,
- this.toCommentId,
- this.callback,
- this.comment = false,
- this.user,
- this.autoFocus = false,
- this.atUser = true});
- @override
- State<StatefulWidget> createState() {
- return _TextInputState();
- }
- }
- class _TextInputState extends State<TextInput> with InjectApi {
- var _textFieldValue = "";
- TextEditingController? _controller;
- FocusNode? _focusNode;
- var _posting = false;
- ValueNotifier<bool> _postable = ValueNotifier(false);
- @override
- void initState() {
- super.initState();
- _focusNode = widget.focusNode ?? FocusNode();
- _controller = TextEditingController()
- ..addListener(() {
- _postable.value = _controller?.value.text.isNotEmpty ?? false;
- });
- if (widget.comment) {
- WidgetsBinding.instance?.addPostFrameCallback((timeStamp) {
- FocusScope.of(context).requestFocus(_focusNode);
- });
- }
- }
- @override
- void dispose() {
- _controller?.dispose();
- super.dispose();
- }
- @override
- Widget build(BuildContext context) {
- return Container(
- decoration: shadowTop(),
- child: ConstrainedBox(
- constraints: BoxConstraints(maxHeight: 150, minHeight: 50),
- child: Row(
- children: <Widget>[
- Expanded(
- child: Container(
- child: TextField(
- controller: _controller,
- focusNode: _focusNode,
- autofocus: widget.autoFocus,
- keyboardType: TextInputType.multiline,
- strutStyle: StrutStyle(height: 1.1),
- minLines: 1,
- maxLines: 3,
- maxLength: 200,
- onChanged: (value) {
- setState(() {
- _textFieldValue = value;
- });
- },
- style: TextStyle(fontSize: 15.0),
- decoration: InputDecoration(
- counterText: "",
- hintText: "发表你的看法...",
- hintStyle:
- TextStyle(color: Color(0xff999999), fontSize: 15.0),
- prefixIconConstraints: BoxConstraints(minWidth: 36),
- prefixIcon: Padding(
- padding: const EdgeInsets.fromLTRB(12.0, 0, 0, 0),
- child: Row(
- mainAxisSize: MainAxisSize.min,
- children: <Widget>[
- Image.asset(
- "lib/assets/img/bbs_icon_reportx.png"),
- SizedBox(
- width: 4,
- ),
- if (widget.user != null && widget.atUser == true)
- ConstrainedBox(
- constraints: BoxConstraints(maxWidth: 100),
- child: Text(
- "@${widget.user?.name}",
- maxLines: 1,
- overflow: TextOverflow.ellipsis,
- ))
- ],
- ),
- ),
- contentPadding: EdgeInsets.symmetric(vertical: 16.0),
- border: InputBorder.none),
- ),
- ),
- ),
- Padding(
- padding: const EdgeInsets.symmetric(horizontal: 12.0),
- child: _posting
- ? SizedBox(
- height: 22,
- width: 22,
- child: CircularProgressIndicator(),
- )
- :
- // PrimaryButton(
- // width: 80,
- // height: 35,
- // callback: _textFieldValue.isEmpty
- // ? null
- // : () async {
- // if (await showBindPhoneDialog(context) != true) {
- // return;
- // }
- // setState(() {
- // _posting = true;
- // });
- // var resp = await api
- // .postForumComment(widget.subjectId, _textFieldValue,
- // parentCommentId: widget.parentCommentId, toCommentId: widget.toCommentId)
- // .catchError((e) {});
- // if (resp != null && resp.code == 0) {
- // ToastUtil.show("发表成功");
- // _controller?.clear();
- // _focusNode?.unfocus();
- // widget.callback?.call();
- // _textFieldValue = '';
- // CommentNotification(Comment(id: resp.data.commentId, subjectId: widget.subjectId), CommentNotification.TYPE_ADD)
- // .dispatch(context);
- // }
- // setState(() {
- // _posting = false;
- // });
- //// SystemChannels.textInput.invokeMethod('TextInput.hide');
- // },
- // content: "发表",
- // ),
- ValueListenableBuilder(
- valueListenable: _postable,
- builder: (_, able, __) => PrimaryButton(
- content: "发表",
- width: 80,
- height: 35,
- callback: () async {
- if (await showBindPhoneDialog(context) !=
- true) {
- return;
- }
- setState(() {
- _posting = true;
- });
- var resp = await api
- .postForumComment(
- widget.subjectId, _textFieldValue,
- parentCommentId: widget.parentCommentId ?? "",
- toCommentId: widget.toCommentId ?? "")
- .catchError((e) {});
- if (resp != null && resp.code == 0) {
- ToastUtil.show("发表成功");
- _controller?.clear();
- _focusNode?.unfocus();
- widget.callback?.call();
- _textFieldValue = '';
- CommentNotification(
- Comment(
- id: resp.data?.commentId,
- subjectId: widget.subjectId),
- CommentNotification.TYPE_ADD)
- .dispatch(context);
- }
- setState(() {
- _posting = false;
- });
- },
- shadow: able == true,
- buttonColor: able == false
- ? Color(0xffFFC400).withOpacity(0.3)
- : null,
- )),
- )
- ],
- ),
- ),
- );
- }
- }
|