[英]Connect scrollcontroller to multiple screens using getx - flutter
我的應用程序上有四個屏幕,第一個使用列表視圖顯示消息列表,第二個有一個帶有發送消息輸入的按鈕,第三個屏幕將第一個和第二個屏幕連接在一起,第四個屏幕是我的 getx controller 屏幕,其中我的卷軸 controller 已創建
第四屏
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將第四個屏幕中的 scrollcontroller 連接到第一個屏幕中的列表視圖,它仍然說它沒有客戶端並且 animateTo 不起作用。 那么有沒有辦法將它正確連接到我的列表視圖,以便 function 很好。
如果您需要更多解釋,請告訴我。
嘗試這個
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!");
}});
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.