Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Chat with GPT'),
//加入返回按鈕
leading: IconButton(
icon: Icon(Icons.arrow_back),
onPressed: () {
//跳转到成绩页面
Navigator.push(context, MaterialPageRoute(builder: (context) {
return HomePage();
}));
},
),
actions: [
IconButton(
icon: Icon(Icons.settings),
onPressed: () {
showDialog(
context: context,
builder: (context) {
return Scaffold(
appBar: AppBar(
title: Text('设置'),
actions: [
IconButton(
icon: const Icon(Icons.search),
onPressed: () {
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
content: FloatingActionButton(
child: Icon(Icons.search),
tooltip: '搜索语句',
onPressed: () => showSearch(
context: context,
delegate: SearchPage(
items: _stringList,
searchLabel: '搜索语句',
suggestion: Center(
child: Text('查询你要注入的语句'),
),
failure: Center(
child: Text('什么都没找到哦 :('),
),
filter: (person) => [
person['act'],
person['prompt'],
],
builder: (person) => ListTile(
title: Text(person['act']!),
subtitle:
Text(person['prompt']!),
onTap: () {
Navigator.pop(
context, person);
},
),
),
),
),
);
}).then((value) {
if (value != null) {
setState(() {
_filteredStringList = [value];
_selectedIndex = 0;
});
}
});
},
),
],
),
body: ListView.builder(
itemCount: _filteredStringList.length,
itemBuilder: (BuildContext context, int index) {
String act = _filteredStringList[index]['act']!;
String prompt =
_filteredStringList[index]['prompt']!;
return RadioListTile(
title: Text('行为: $act'),
subtitle: Text('语句: $prompt'),
value: index,
groupValue: _selectedIndex,
onChanged: (int? value) {
setState(() {
_selectedIndex = value!;
});
},
);
},
));
});
},
),
],
),
body: Chat(
messages: _messages,
onAttachmentPressed: _handleAttachmentPressed,
onMessageTap: _handleMessageTap,
onPreviewDataFetched: _handlePreviewDataFetched,
onSendPressed: _handleSendPressed,
showUserAvatars: true,
showUserNames: true,
user: _user,
),
);
}
void _addMessage(types.Message message) {
setState(() {
_messages.insert(0, message);
});
}
void _handleAttachmentPressed() {
showModalBottomSheet(
context: context,
builder: (BuildContext context) => SafeArea(
child: SizedBox(
height: 144,
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
TextButton(
onPressed: () {
Navigator.pop(context);
_handleImageSelection();
},
child: const Align(
alignment: AlignmentDirectional.centerStart,
child: Text('Photo'),
),
),
TextButton(
onPressed: () {
Navigator.pop(context);
_handleFileSelection();
},
child: const Align(
alignment: AlignmentDirectional.centerStart,
child: Text('File'),
),
),
TextButton(
onPressed: () => Navigator.pop(context),
child: const Align(
alignment: AlignmentDirectional.centerStart,
child: Text('Cancel'),
),
),
],
),
),
),
);
} 为什么单选框选中后不能及时选中呢内容:可能是因为单选框的状态没有及时更新。可以尝试在onChanged回调中调用setState来更新状态,例如:
onChanged: (int? value) {
setState(() {
_selectedIndex = value!;
});
},