英文:
Flutter textfield onsubmitted not working
问题
当按下按钮时,我尝试使其打印从API获取的列表,代码没有显示任何错误,但当我尝试按下按钮时什么都没有发生。我尝试注释掉部分代码,并尝试仅进行调试打印以查看是否是API调用函数引起了问题,但当我按下按钮时仍然没有打印任何内容。您可以在文件的第85行看到代码。
import 'package:flutter/material.dart';
import 'package:flutter_spinkit/flutter_spinkit.dart';
import 'package:tsa_softwaredev/constants/const.dart';
import 'package:tsa_softwaredev/services/asset_manager.dart';
import 'package:tsa_softwaredev/services/services.dart';
import 'package:tsa_softwaredev/widgets/chat_widget.dart';
class ChatScreen extends StatefulWidget {
const ChatScreen({Key? key}) : super(key: key);
@override
State<ChatScreen> createState() => _ChatScreenState();
}
class _ChatScreenState extends State<ChatScreen> {
late TextEditingController textEditingController;
@override
void initState() {
textEditingController = TextEditingController();
super.initState();
}
@override
void dispose() {
textEditingController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
elevation: 2,
leading: Padding(
padding: const EdgeInsets.all(8),
child: Image.asset(AssetManager.openaiLogoImage),
),
title: const Text("ChatGPT"),
actions: [
IconButton(
onPressed: () async {
await Services.ShowModalSheet(context: context);
},
icon: const Icon(
Icons.more_vert_rounded,
color: Colors.white,
),
),
],
),
body: SafeArea(
child: Column(
children: [
Flexible(
child: ListView.builder(
itemCount: 6,
itemBuilder: (context, index) {
return ChatWidget(
msg: chatMessages[index]["msg"].toString(),
chatIndex: int.parse(chatMessages[index]["chatIndex"].toString()),
);
},
),
),
const SizedBox(height: 15),
Material(
color: cardColor,
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Row(
children: [
Expanded(
child: TextField(
style: const TextStyle(color: Colors.white),
controller: textEditingController,
onSubmitted: (value) {
debugPrint("sent");
},
decoration: const InputDecoration.collapsed(
hintText: "How can I help you?",
hintStyle: TextStyle(color: Colors.grey),
),
),
),
IconButton(
onPressed: () {},
icon: const Icon(
Icons.send_rounded,
color: Colors.grey,
),
),
],
),
),
),
],
),
),
);
}
}
如果您需要帮助解决按下按钮后没有打印列表的问题,请提供有关API调用的更多信息,以便我能够为您提供更多帮助。
英文:
I am trying to make it so that when the button is pressed it prints a list fetched from an API, and the code showed no errors but when I tried pressing the button nothing happened. I tried commenting out the section and trying just a debugprint to see if it was the API calling function that was causing the problem, but when I press the button it still doesn't print anything. You can see the code on Line 85 of the file.
import 'package:flutter/material.dart';
import 'package:flutter_spinkit/flutter_spinkit.dart';
import 'package:tsa_softwaredev/constants/const.dart';
import 'package:tsa_softwaredev/services/asset_manager.dart';
import 'package:tsa_softwaredev/services/services.dart';
import 'package:tsa_softwaredev/widgets/chat_widget.dart';
class ChatScreen extends StatefulWidget {
const ChatScreen({Key? key}) : super(key: key);
@override
State<ChatScreen> createState() => _ChatScreenState();
}
class _ChatScreenState extends State<ChatScreen> {
late TextEditingController textEditingController;
@override
void initState() {
textEditingController = TextEditingController();
super.initState();
}
@override
void dispose() {
textEditingController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
elevation: 2,
leading: Padding(
padding: const EdgeInsets.all(8),
child: Image.asset(AssetManager.openaiLogoImage),
),
title: const Text("ChatGPT"),
actions: [
IconButton(
onPressed: () async {
await Services.ShowModalSheet(context: context);
},
icon: const Icon(
Icons.more_vert_rounded,
color: Colors.white,
),
),
],
),
body: SafeArea(
child: Column(
children: [
Flexible(
child: ListView.builder(
itemCount: 6,
itemBuilder: (context, index) {
return ChatWidget(
msg: chatMessages[index]["msg"].toString(),
chatIndex: int.parse(chatMessages[index]["chatIndex"].toString()),
);
},
),
),
const SizedBox(height: 15),
Material(
color: cardColor,
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Row(
children: [
Expanded(
child: TextField(
style: const TextStyle(color: Colors.white),
controller: textEditingController,
onSubmitted: (value) {
debugPrint("sent");
},
decoration: const InputDecoration.collapsed(
hintText: "How can I help you?",
hintStyle: TextStyle(color: Colors.grey),
),
),
),
IconButton(
onPressed: () {},
icon: const Icon(
Icons.send_rounded,
color: Colors.grey,
),
),
],
),
),
),
],
),
),
);
}
}
答案1
得分: 0
在 onPressed 方法被触发时没有任何内容。
IconButton(
onPressed: () {},
icon: const Icon(
Icons.send_rounded,
color: Colors.grey,
),
),
当我添加打印语句后,它会打印出:
IconButton(
onPressed: () {
debugPrint('Hey, im printing debugPrint');
},
icon: const Icon(
Icons.send_rounded,
color: Colors.grey,
),
),
英文:
There is nothing being triggered onPressed method
IconButton(
onPressed: () {},
icon: const Icon(
Icons.send_rounded,
color: Colors.grey,
),
),
when i add print, it prints
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
IconButton(
onPressed: () {
debugPrint('Hey, im printing debugPrint');
},
icon: const Icon(
Icons.send_rounded,
color: Colors.grey,
),
),
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论