Flutter文本字段onSubmitted不起作用

huangapple go评论55阅读模式
英文:

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 &#39;package:flutter/material.dart&#39;;
import &#39;package:flutter_spinkit/flutter_spinkit.dart&#39;;
import &#39;package:tsa_softwaredev/constants/const.dart&#39;;
import &#39;package:tsa_softwaredev/services/asset_manager.dart&#39;;
import &#39;package:tsa_softwaredev/services/services.dart&#39;;
import &#39;package:tsa_softwaredev/widgets/chat_widget.dart&#39;;
class ChatScreen extends StatefulWidget {
const ChatScreen({Key? key}) : super(key: key);
@override
State&lt;ChatScreen&gt; createState() =&gt; _ChatScreenState();
}
class _ChatScreenState extends State&lt;ChatScreen&gt; {
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(&quot;ChatGPT&quot;),
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][&quot;msg&quot;].toString(),
chatIndex: int.parse(chatMessages[index][&quot;chatIndex&quot;].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(&quot;sent&quot;);
},
decoration: const InputDecoration.collapsed(
hintText: &quot;How can I help you?&quot;,
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(&#39;Hey, im printing debugPrint&#39;);
},
icon: const Icon(
Icons.send_rounded,
color: Colors.grey,
),
),

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年5月29日 01:06:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/76352628.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定