【问题标题】:Connect scrollcontroller to multiple screens using getx - flutter使用 getx 将滚动控制器连接到多个屏幕 - 颤振
【发布时间】:2021-11-29 18:13:19
【问题描述】:

我的应用程序有四个屏幕,第一个使用列表视图显示消息列表,第二个有一个带有发送消息的输入的按钮,第三个屏幕将第一个和第二个屏幕连接在一起,第四个屏幕是我的 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 不起作用。那么有没有办法将它正确连接到我的列表视图以便正常运行。

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

【问题讨论】:

    标签: android flutter dart flutter-getx scrollcontroller


    【解决方案1】:

    试试这个

    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!");
            }});
          }
    

    【讨论】:

    • 它没有工作仍然说它没有客户
    猜你喜欢
    • 1970-01-01
    • 2021-03-02
    • 2022-01-05
    • 2021-06-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-01
    • 2021-06-26
    相关资源
    最近更新 更多