我使用 flutter 和 getx 状态管理与绑定制作了一个入门页面,
当我尝试设置可见时,用户界面没有更新,所以当我在最后一页时,跳过和下一个图标没有消失,并且开始按钮没有出现,当我使用 Obx 时,它说
[Get] the improper use of a GetX has been detected.
You should only use GetX or Obx for the specific widget that will be updated.
If you are seeing this error, you probably did not insert any observable variables into GetX/Obx
or insert them outside the scope that GetX considers suitable for an update
(example: GetX => HeavyWidget => variableObservable).
If you need to update a parent widget and a child widget, wrap each one in an Obx/GetX.
我尝试使用 onboarc 控制器中的 bool 设置可见,如下所示
//button get started
Visibility(
visible: controller.isLastPage == true,
还有这个
//skip and next button
Visibility(
visible: controller.isLastPage == false,
这是我在 pageview.builder 中进行更改的方式
onPageChanged: (index) {
controller.isLastPage = index == 1;
},
就像这里我如何在控制器中使用
bool isLastPage = false;
void nextPage() {
currentPage.value++;
// i want to see the value of it
print(isLastPage);
if (currentPage.value >= 3) {
// Jika sudah sampai halaman terakhir, pindah ke halaman selanjutnya
Get.offAllNamed('/home');
} else {
pageController.nextPage(duration: Duration(milliseconds: 300), curve: Curves.ease);
isLastPage = true;
}
}
这里是我如何编写入门代码以供参考
Expanded(
child: PageView.builder(
controller: controller.pageController,
itemCount: pageContent.length,
itemBuilder: (context, index) {
print(index);
return Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Image.asset(
pageContent[index]['image'],
fit: BoxFit.fill,
width: 450,
height: 600,
),
SizedBox(height: 20),
Text(
pageContent[index]['header'],
style: onboardingHeaderTextStyle,
textAlign: TextAlign.center,
),
SizedBox(height: 10),
Text(
pageContent[index]['subheader'],
maxLines: 3,
style: onboardingSubHeaderTextStyle,
textAlign: TextAlign.center,
),
SizedBox(height: 5),
],
);
},
onPageChanged: (index) {
controller.isLastPage = index == 1;
},
),
),
将您的 Visibility 小部件放入 Obx 小部件中并尝试一下,因为每当值发生更改时,Obx 都会在 Getx 附带时相应地更新小部件