-1

我的应用程序有四个屏幕,第一个使用列表视图显示消息列表,第二个有一个带有发送消息的输入的按钮,第三个屏幕将第一个和第二个屏幕连接在一起,第四个屏幕是我的 getx 控制器屏幕,其中我的滚动控制器已创建

第四屏

class ScrollToTopController extends GetxController {
ScrollController msgScroll = ScrollController();
}

第一个屏幕

final ScrollToTopController sController = Get.put(ScrollToTopController());
//...
Obx(() =>  ListView.builder(
              controller: sController.msgScroll,
              itemCount: chats.length + 1,
              shrinkWrap: true,
              padding: EdgeInsets.only(bottom: 50),
              physics: ScrollPhysics(),
              itemBuilder: (context, index) {
///...

第二屏

//Button that clicks on this Future below
sendChatData() async {
    if (msg.text == '' && images == null) {
      return;
    }
//Sending chat data to my database and after that do this
if (sController.msgScroll.hasClients) {
      sController.msgScroll.animateTo(0, duration: Duration(milliseconds: 700), curve: Curves.easeInOut);
      print("This has client!");
    } else {
      print("This has no client!");
    }
}
//But it always says it doesn't have clients

第三屏

//inside initState
if (sController.msgScroll.hasClients) {
      sController.msgScroll.animateTo(0, duration: Duration(milliseconds: 700), curve: Curves.easeInOut);
      print("This has client!");
    } else {
      print("This has no client!");
    }
//...

//Inside body
Stack(
   children: [
      chatMessages(context, uController, refresh), //First Screen
      ChatBottomInput(cData: widget.chatData),  //Second Screen
    ],
   ),

但是现在遇到的问题是,如果我使用sController.msgScroll将第四个屏幕中的滚动控制器连接到第一个屏幕中的列表视图,它仍然说它没有客户端并且 animateTo 不起作用。那么有没有办法将它正确连接到我的 listview 以便正常运行。

如果您需要更多解释,请告诉我。

4

1 回答 1

0

尝试这个

void initState() {
        super.initState();

        WidgetsBinding.instance.addPostFrameCallback((_) {
    if (sController.msgScroll.hasClients) {
          sController.msgScroll.animateTo(0, duration: Duration(milliseconds: 700), curve: Curves.easeInOut);
          print("This has client!");
        } else {
          print("This has no client!");
        }});
      }
于 2021-10-11T03:56:38.713 回答