dreampad/lib/app/modules/home/widgets/chat_input_widget.dart

132 lines
3.8 KiB
Dart
Raw Permalink Normal View History

2023-11-28 10:44:58 +08:00
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});
final Function(String)? onPressed;
@override
// ignore: library_private_types_in_public_api
_ChatInputWidgetState createState() => _ChatInputWidgetState();
}
class _ChatInputWidgetState extends State<ChatInputWidget> {
final TextEditingController _controller = TextEditingController();
final FocusNode _focus = FocusNode();
late bool clickable = false;
void verify() {
final String input = _controller.text;
bool clickabled = true;
if (input.isEmpty || input.length < 5 || input.length > 120) {
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,
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),
],
),
);
}
}