forked from MannaYang/ChatGPT-Flutter-Web
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmulti_modal_input.dart
236 lines (219 loc) · 9.16 KB
/
multi_modal_input.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:frontend/gen/strings.g.dart';
import 'package:frontend/pages/chat/model/chat_attach_info.dart';
import 'package:frontend/pages/chat/provider/chat_provider.dart';
import 'package:frontend/service/ext/ext.dart';
import 'package:frontend/service/file/file_model.dart';
import 'package:frontend/service/file/file_provider.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:record/record.dart';
import 'multi_modal_audio.dart';
import 'multi_modal_file.dart';
///
/// Multi-Modal-Input
/// text / doc / image / audio
///
class MultiModalInput extends ConsumerStatefulWidget {
const MultiModalInput(this.controller, this.handInput,
{this.hintStr, this.maxWidth = 752, super.key});
final TextEditingController controller;
final String? hintStr;
final double maxWidth;
final Function(String, List<ChatAttachInfo>) handInput;
@override
ConsumerState<ConsumerStatefulWidget> createState() {
return _MultiModalInputState();
}
}
class _MultiModalInputState extends ConsumerState<MultiModalInput> {
late FocusNode _focusNode;
late final record = AudioRecorder();
final ScrollController _scrollController = ScrollController();
///Attach file list
late final attachList = <FileInfo>[];
///Current input str
var _inputStr = '';
Color iconColor(bool allowInput) {
return allowInput
? Theme.of(context).colorScheme.secondary
: Theme.of(context).colorScheme.outline;
}
double get maxInputHeight => 168;
double get minInputHeight => 56;
var modalHeight = 0.0;
///
/// Calculate input height
/// [TextPainter.maxWidth] Dynamically calculates the width based on your text characters
///
(double, double) inputHeight(String value) {
final textPainter = TextPainter(
text: TextSpan(text: value, style: const TextStyle(height: 22)),
textDirection: TextDirection.ltr,
maxLines: null)
// ..layout(maxWidth: widget.maxWidth - 16 * 2 - 128);
..layout(maxWidth: 400);
final lineList = textPainter.computeLineMetrics();
final lines = lineList.length;
var lineHeight = lineList.fold(0.0,
(previousValue, element) => previousValue.toDouble() + element.height);
final totalHeight = minInputHeight * (lines == 0 ? 1 : lines);
if (totalHeight > 152) return (152, lineHeight * lines);
return (totalHeight, lineHeight * lines);
}
@override
void initState() {
super.initState();
_focusNode = FocusNode();
widget.controller.addListener(() {
if (modalHeight == maxInputHeight) return;
var (value1, _) = inputHeight(widget.controller.text);
if (modalHeight == value1) return;
Future.delayed(Durations.short1, () {
ref.read(modalInputProvider.notifier).updateHeight(value1);
});
});
}
@override
void dispose() {
_focusNode.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
modalHeight = ref.watch(modalInputProvider);
final allowInput = ref.watch(allowInputProvider);
final allowRecord = ref.watch(allowRecordProvider);
ref.listen(attachFileProvider, (previous, next) {
attachList.clear();
attachList.addAll(next);
});
return AnimatedContainer(
duration: Durations.medium3,
curve: Curves.easeInOut,
constraints: BoxConstraints(
minWidth: 360,
maxWidth: widget.maxWidth,
maxHeight: maxInputHeight),
height: modalHeight,
child: Material(
elevation: 0,
borderRadius:
BorderRadius.circular(modalHeight == minInputHeight ? 26 : 8),
child: Row(crossAxisAlignment: CrossAxisAlignment.start, children: [
Padding(
padding:
const EdgeInsets.symmetric(horizontal: 16, vertical: 16),
child: Image.asset('assets/images/ic_chat.png',
width: 24, height: 24)),
///
/// When maxLines: null,it will occur
///
/// TextField Line break problem in Chinese input on the web page
/// https://github.com/flutter/flutter/issues/140728
///
/// Multiline TextField misbehaves on korean inputs only on web
/// https://github.com/flutter/flutter/issues/134797
Expanded(
child:
// TextField(
// focusNode: _focusNode,
// maxLines: null,
// scrollController: _scrollController,
// controller: widget.controller,
// textInputAction: TextInputAction.done,
// keyboardType: TextInputType.multiline,
// expands: true,
// style: style18,
// onSubmitted: (str) => handleInputStr(allowInput),
// enabled: allowInput && !allowRecord,
// onChanged: (str) => _inputStr = str,
// decoration: InputDecoration(
// border: InputBorder.none,
// hintText: allowRecord
// ? t.app.text_field_hint_record
// : t.app.text_field_hint,
// counter: const SizedBox(width: 0, height: 0),
// isCollapsed: true,
// hoverColor: const Color(0xFFCAC4D0),
// contentPadding: const EdgeInsets.only(top: 18),
// )),
TextFormField(
focusNode: _focusNode,
maxLines: null,
scrollController: _scrollController,
controller: widget.controller,
textInputAction: TextInputAction.done,
keyboardType: TextInputType.multiline,
expands: true,
style: style18,
onFieldSubmitted: (str) => handleInputStr(allowInput),
enabled: allowInput && !allowRecord,
onChanged: (str) => _inputStr = str,
decoration: InputDecoration(
border: InputBorder.none,
hintText: allowRecord
? t.app.text_field_hint_record
: t.app.text_field_hint,
counter: const SizedBox(width: 0, height: 0),
isCollapsed: true,
hoverColor: const Color(0xFFCAC4D0),
contentPadding: const EdgeInsets.only(top: 18),
))),
Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
Expanded(
child: SizedBox(
width: 128,
child: MultiModalFile(
maxInputHeight, minInputHeight))),
Container(
width: 128,
height: 48,
alignment: Alignment.bottomCenter,
padding: const EdgeInsets.only(right: 8, bottom: 8),
child: Row(
children: [
IconButton(
onPressed: () => uploadFile(),
icon: const Icon(Icons.attach_file_outlined),
tooltip: "1.Upload image\n2.Upload Vector docs",
),
MultiModalAudio(record),
IconButton(
onPressed: () => handleInputStr(allowInput),
icon: Icon(Icons.send,
color: iconColor(allowInput)))
],
)),
]),
// RawKeyboardListener(
// focusNode: _focusNode,
// onKey: (event) {
// // if (event.logicalKey == LogicalKeyboardKey.enter ||
// // event.logicalKey == LogicalKeyboardKey.numpadEnter) {
// // if (!event.isShiftPressed) handleInputStr(allowInput);
// // }
// },
// child: const SizedBox(width: 0, height: 0))
])));
}
void uploadFile() {
ref.read(uploadFileProvider.notifier).uploadFile();
}
///
/// Response send action
/// - [onSubmitted] - TextField
/// - [onPressed] - IconButton
/// - [onKey] - enter
///
void handleInputStr(bool allowInput) {
if (!allowInput) return;
var attachRes = attachList.map((e) =>
ChatAttachInfo(e.fileName!, e.resId!, e.fileName!.split(".").last));
widget.handInput(_inputStr, attachRes.toList());
widget.controller.clear();
_inputStr = '';
}
}