import 'package:dreampad/app/shared/shared.dart'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:flutter_screenutil/flutter_screenutil.dart'; class ChatInputWidget extends StatefulWidget { const ChatInputWidget({ this.onPressed, super.key, required this.controller, }); final Function(String)? onPressed; final TextEditingController controller; @override // ignore: library_private_types_in_public_api _ChatInputWidgetState createState() => _ChatInputWidgetState(); } class _ChatInputWidgetState extends State { late final TextEditingController _controller = widget.controller; final FocusNode _focus = FocusNode(); late bool clickable = false; void verify() { final String input = _controller.text; bool clickabled = true; if (input.isEmpty || input.length < 5) { clickabled = false; } setState(() { clickable = clickabled; }); } @override void initState() { super.initState(); _controller.addListener(verify); } @override void dispose() { _focus.dispose(); _controller.dispose(); _controller.removeListener(verify); super.dispose(); } @override Widget build(BuildContext context) { return Container( decoration: BoxDecoration( color: const Color(0xFF823CE0), boxShadow: [ BoxShadow( color: Colors.black, offset: Offset(0, 3.h), blurRadius: 20.r, spreadRadius: 1.0, ), ], ), height: 85.h, child: Row( mainAxisAlignment: MainAxisAlignment.start, children: [ const RSizedBox(width: 31.0), GestureDetector( child: Images.keyboard, onTap: () => {}, ), const RSizedBox(width: 12.0), Flexible( child: Container( height: 51.h, padding: REdgeInsets.only(left: 21.0, right: 21.0), decoration: BoxDecoration( color: Colors.white, borderRadius: BorderRadius.circular(9).r, ), child: TextField( controller: _controller, focusNode: _focus, style: TextStyles.mediumGray3315, maxLength: 120, maxLines: null, maxLengthEnforcement: MaxLengthEnforcement.enforced, decoration: InputDecoration( hintText: '请输入你想说的内容', hintStyle: TextStyles.mediumGray9915, counterText: '', border: InputBorder.none, ), ), ), ), const RSizedBox(width: 12.0), Container( width: 141.w, height: 51.h, decoration: BoxDecoration( color: const Color(0xFFFFEC62), borderRadius: BorderRadius.circular(9.0).r, ), child: ElevatedButton( onPressed: clickable ? () { if (_controller.text.length < 5) { } else { _focus.unfocus(); widget.onPressed?.call(_controller.text); _controller.text = ''; } } : null, style: ButtonStyle( elevation: MaterialStateProperty.all(0), backgroundColor: MaterialStateProperty.all(Colors.transparent), ), child: Text( '发送', style: TextStyle( fontSize: 18.sp, color: const Color(0xFF823CE0), fontWeight: FontWeight.w500, ), ), ), ), const RSizedBox(width: 30), ], ), ); } }