ChatApp/lib/tools/HomePage.dart
2024-06-15 14:39:02 +08:00

161 lines
4.6 KiB
Dart
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'HomeModel.dart';
class Homepage extends StatefulWidget {
const Homepage({super.key});
@override
State<Homepage> createState() => _HomepageState();
}
class _HomepageState extends State<Homepage> {
late StreamSubscription subscription;
final HomeModel viewModel = HomeModel();
final TextEditingController _controller1 = TextEditingController();
String text = "";
String data = '';
Future<void> chat() async {
viewModel.chat(text,'Assistant');
}
@override
void initState() {
// TODO: implement initState
super.initState();
// 监听输入变化
_controller1.addListener(() {
print(_controller1.text);
});
subscription = viewModel.streamController.stream.listen((newData) {
String code = newData['code'];
if (code.isNotEmpty) {
if (code == "chat") {
//有数据
data = newData['data'];
setState(() {
});
//EasyLoading.showToast(data);
print("data" + data.toString());
}
} else {
EasyLoading.dismiss();
}
});
}
void _textFieldChanged1(String str) {
text = str;
}
@override
void dispose() {
// TODO: implement dispose
subscription.cancel();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text("聊天"),
),
body: Container(
child: Column(
children: [
Text(data),
Container(
alignment: Alignment.center,
margin: const EdgeInsets.only(top: 100),
padding: EdgeInsets.only(left: 21),
height: 48,
width: MediaQuery.of(context).size.width - 60,
decoration: new BoxDecoration(
//设置四周圆角 角度
borderRadius: const BorderRadius.all(Radius.circular(24.0)),
border: Border.all(color: const Color(0xFFCACACA), width: 1),
//设置四周边框
),
child: TextField(
controller: _controller1,
keyboardType: TextInputType.number,
decoration: const InputDecoration.collapsed(hintText: '输入内容', hintStyle: TextStyle(fontSize: 16.0, color: Color(0xFF999999))),
textAlign: TextAlign.left,
//文本对齐方式
//最大长度设置此项会让TextField右下角有一个输入数量的统计字符串
maxLines: 1,
//最大行数
style: const TextStyle(fontSize: 16.0, color: Color(0xFF999999)),
//输入文本的样式
onChanged: _textFieldChanged1,
autofocus: false,
inputFormatters: [
LengthLimitingTextInputFormatter(255),
//最大长度
]),
),
Container(
margin: const EdgeInsets.only(top: 17),
height: 48,
width: MediaQuery.of(context).size.width - 60,
child: _LoginButton(),
),
],
),
),
);
}
_LoginButton() {
return Container(
height: 35,
alignment: Alignment.center,
decoration: const BoxDecoration(
//渐变
gradient:
LinearGradient(colors: [Color(0xFF52B5FF), Color(0xFF6591FB)], begin: Alignment.centerLeft, end: Alignment.centerRight),
//设置四周圆角 角度
borderRadius: BorderRadius.all(Radius.circular(24)),
//设置四周边框
),
child: SizedBox(
width: double.infinity,
height: double.infinity,
child: ElevatedButton(
style: ButtonStyle(
elevation: MaterialStateProperty.all(0), //阴影
backgroundColor: MaterialStateProperty.all(Color(0x00ffffff)),
shape: MaterialStateProperty.all(RoundedRectangleBorder(borderRadius: BorderRadius.circular(3.3))),
//设置水波纹颜色
overlayColor: MaterialStateProperty.all(Color(0x4fffffff)),
),
onPressed: () {
chat();
},
child: Text(
"调用",
style: TextStyle(
color: Colors.white,
fontSize: 20,
fontWeight: FontWeight.w600,
),
),
),
),
);
}
}