diff --git a/android/app/ChatApp.jks b/android/app/ChatApp.jks new file mode 100644 index 0000000..b02015a Binary files /dev/null and b/android/app/ChatApp.jks differ diff --git a/android/app/build.gradle b/android/app/build.gradle index 51d96e8..8fecd02 100644 --- a/android/app/build.gradle +++ b/android/app/build.gradle @@ -28,6 +28,16 @@ android { compileSdk = flutter.compileSdkVersion ndkVersion = flutter.ndkVersion + signingConfigs { + //签名的配置 + signConfig { + storeFile file("ChatApp.jks") + storePassword '123456' + keyAlias 'ChatApp' + keyPassword '123456' + } + } + compileOptions { sourceCompatibility = JavaVersion.VERSION_1_8 targetCompatibility = JavaVersion.VERSION_1_8 @@ -47,8 +57,14 @@ android { buildTypes { release { // TODO: Add your own signing config for the release build. - // Signing with the debug keys for now, so `flutter run --release` works. - signingConfig = signingConfigs.debug + signingConfig signingConfigs.signConfig //打包命令行:gradlew assembleRelease + proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' + //关闭混淆 + minifyEnabled false //删除无用代码 + shrinkResources false //删除无用资源 + } + debug { + signingConfig signingConfigs.signConfig } } } diff --git a/android/app/src/main/AndroidManifest.xml b/android/app/src/main/AndroidManifest.xml index 081800b..60d36b0 100644 --- a/android/app/src/main/AndroidManifest.xml +++ b/android/app/src/main/AndroidManifest.xml @@ -1,8 +1,11 @@ + + + + android:icon="@mipmap/ic_logo"> { late StreamSubscription subscription; - final HomeModel viewModel = HomeModel(); - final TextEditingController _controller1 = TextEditingController(); + final HomeModel viewModel = HomeModel(); + final TextEditingController _chatController = TextEditingController(); + final ScrollController _scrollController = ScrollController(); String text = ""; String data = ''; + List list = []; + Future chat() async { - viewModel.chat(text,'Assistant'); + EasyLoading.show(status: 'loading...'); + viewModel.chat(text, 'Assistant'); } + void _textFieldChanged(String str) { + text = str; + } @override void initState() { // TODO: implement initState super.initState(); // 监听输入变化 - _controller1.addListener(() { - print(_controller1.text); + _chatController.addListener(() { + print(_chatController.text); }); subscription = viewModel.streamController.stream.listen((newData) { String code = newData['code']; if (code.isNotEmpty) { - if (code == "chat") { + if (code == "chat") { //有数据 - data = newData['data']; - - setState(() { - - }); + data = newData['data']; + list.add(Message(data, false)); + setState(() {}); + Future.delayed(Duration(milliseconds: 500), () { + _scrollController.jumpTo(_scrollController.position.maxScrollExtent); + }); //EasyLoading.showToast(data); print("data" + data.toString()); } + EasyLoading.dismiss(); } else { EasyLoading.dismiss(); } }); } - void _textFieldChanged1(String str) { - text = str; - } @override void dispose() { // TODO: implement dispose + _chatController.dispose(); subscription.cancel(); super.dispose(); } - @override Widget build(BuildContext context) { return Scaffold( @@ -90,6 +94,7 @@ class _HomepageState extends State { alignment: Alignment.topCenter, child: ListView.builder( // reverse: true, + controller: _scrollController, itemCount: list.length, itemBuilder: (Context, index) { return _item(index); @@ -107,6 +112,7 @@ class _HomepageState extends State { margin: EdgeInsets.only(left: 16), child: TextField( controller: _chatController, + onChanged: _textFieldChanged, decoration: InputDecoration( border: InputBorder.none, // 移除非聚焦状态下的边框 enabledBorder: InputBorder.none, // 移除获得焦点但未输入内容时的边框 @@ -117,16 +123,20 @@ class _HomepageState extends State { ), GestureDetector( onTap: () { - list.add(Message(chatText, true)); + chat(); + list.add(Message(text, true)); _chatController.clear(); + Future.delayed(Duration(milliseconds: 200), () { + _scrollController.jumpTo(_scrollController.position.maxScrollExtent); + }); setState(() {}); }, child: Container( width: 70, height: 50, alignment: Alignment.center, - decoration: BoxDecoration( - color: Color(0xFF006CFF), borderRadius: BorderRadius.all(Radius.circular(8.0))), + decoration: + BoxDecoration(color: Color(0xFF006CFF), borderRadius: BorderRadius.all(Radius.circular(8.0))), child: Text( "发送", style: TextStyle(color: Colors.white), @@ -148,25 +158,37 @@ class _HomepageState extends State { child: !list[index].isMe ? Row( mainAxisAlignment: MainAxisAlignment.start, + crossAxisAlignment: CrossAxisAlignment.start, children: [ Container( width: 45, height: 45, - color: Color(0xFFD8D8D8), - margin: EdgeInsets.only(left: 16), - ), - Container( - margin: EdgeInsets.symmetric(horizontal: 16.0, vertical: 4.0), - padding: EdgeInsets.all(12.0), - decoration: BoxDecoration( - color: Color(0xFFECECEC), - borderRadius: BorderRadius.circular(16.0), + margin: EdgeInsets.only(left: 16, top: 4), + child: ClipRRect( + borderRadius: BorderRadius.circular(10), + child: Image( + fit: BoxFit.cover, + image: AssetImage('assets/images/avatar2.png'), + ), ), - child: Text( - list[index].text, - style: TextStyle( - fontSize: 16, - color: Colors.black, + ), + ConstrainedBox( + constraints: BoxConstraints( + maxWidth: MediaQuery.of(context).size.width - 150, // 确保不超过屏幕宽度 + ), + child: Container( + margin: EdgeInsets.symmetric(horizontal: 16.0, vertical: 4.0), + padding: EdgeInsets.all(12.0), + decoration: BoxDecoration( + color: Color(0xFFECECEC), + borderRadius: BorderRadius.circular(16.0), + ), + child: Text( + list[index].text, + style: TextStyle( + fontSize: 16, + color: Colors.black, + ), ), ), ), @@ -174,27 +196,39 @@ class _HomepageState extends State { ) : Row( mainAxisAlignment: MainAxisAlignment.end, + crossAxisAlignment: CrossAxisAlignment.start, children: [ - Container( - margin: EdgeInsets.symmetric(horizontal: 16.0, vertical: 4.0), - padding: EdgeInsets.all(12.0), - decoration: BoxDecoration( - color: Color(0xFF006CFF), - borderRadius: BorderRadius.circular(16.0), + ConstrainedBox( + constraints: BoxConstraints( + maxWidth: MediaQuery.of(context).size.width - 150, // 确保不超过屏幕宽度 ), - child: Text( - list[index].text, - style: TextStyle( - fontSize: 16, - color: Colors.white, + child: Container( + margin: EdgeInsets.symmetric(horizontal: 16.0, vertical: 4.0), + padding: EdgeInsets.all(12.0), + decoration: BoxDecoration( + color: Color(0xFF006CFF), + borderRadius: BorderRadius.circular(16.0), + ), + child: Text( + list[index].text, + style: TextStyle( + fontSize: 16, + color: Colors.white, + ), ), ), ), Container( width: 45, height: 45, - color: Color(0xFFD8D8D8), - margin: EdgeInsets.only(right: 16), + margin: EdgeInsets.only(right: 16, top: 4), + child: ClipRRect( + borderRadius: BorderRadius.circular(10), + child: Image( + fit: BoxFit.cover, + image: AssetImage('assets/images/avatar1.png'), + ), + ), ), ], ), diff --git a/pubspec.yaml b/pubspec.yaml index 63a5555..1117062 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -42,53 +42,15 @@ dev_dependencies: flutter_test: sdk: flutter - # The "flutter_lints" package below contains a set of recommended lints to - # encourage good coding practices. The lint set provided by the package is - # activated in the `analysis_options.yaml` file located at the root of your - # package. See that file for information about deactivating specific lint - # rules and activating additional ones. flutter_lints: ^3.0.0 build_runner: ^2.4.11 json_serializable: ^6.8.0 -# For information on the generic Dart part of this file, see the -# following page: https://dart.dev/tools/pub/pubspec -# The following section is specific to Flutter packages. flutter: - # The following line ensures that the Material Icons font is - # included with your application, so that you can use the icons in - # the material Icons class. uses-material-design: true + assets: + - assets/ + - assets/images/ - # To add assets to your application, add an assets section, like this: - # assets: - # - images/a_dot_burr.jpeg - # - images/a_dot_ham.jpeg - - # An image asset can refer to one or more resolution-specific "variants", see - # https://flutter.dev/assets-and-images/#resolution-aware - - # For details regarding adding assets from package dependencies, see - # https://flutter.dev/assets-and-images/#from-packages - - # To add custom fonts to your application, add a fonts section here, - # in this "flutter" section. Each entry in this list should have a - # "family" key with the font family name, and a "fonts" key with a - # list giving the asset and other descriptors for the font. For - # example: - # fonts: - # - family: Schyler - # fonts: - # - asset: fonts/Schyler-Regular.ttf - # - asset: fonts/Schyler-Italic.ttf - # style: italic - # - family: Trajan Pro - # fonts: - # - asset: fonts/TrajanPro.ttf - # - asset: fonts/TrajanPro_Bold.ttf - # weight: 700 - # - # For details regarding fonts from package dependencies, - # see https://flutter.dev/custom-fonts/#from-packages