Flutter textfield on subjected不工作

lx0bsm1f  于 2023-05-29  发布在  Flutter
关注(0)|答案(1)|浏览(153)

我试图使它,当按钮被按下时,它打印一个从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,
                      ),
                    ),
                  ],
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}
zpqajqem

zpqajqem1#

在Pressed方法上没有触发任何内容
图标按钮(按下时:(){},图标:const Icon(Icons.send_rounded,color:颜色.灰色,),),
当我添加打印时,它打印

IconButton(
                      onPressed: () {
                        debugPrint('Hey, im printing debugPrint');
                      },
                      icon: const Icon(
                        Icons.send_rounded,
                        color: Colors.grey,
                      ),
                    ),

相关问题