简体   繁体   English

如何使用 flutter GetX 将“selectedIndex”的值从一个 dart 文件更改为另一个 dart 文件?

[英]How to change the value of 'selectedIndex' from one dart file to the other dart file using flutter GetX?

I have my custom Bottom Navigation Bar in one dart file, ie bottomnavbar.dart .我在一个 dart 文件中有我的自定义Bottom Navigation Bar ,即bottomnavbar.dart And I have list of multiple screens (or pages) in my home.dart file.我的home.dart文件中有多个screens (或页面)的列表。 I am using an .obs variable to store my selected index value.我正在使用.obs variable来存储我选择的索引值。
code from home.dart file:来自home.dart文件的代码:

var selectedIndex = 0.obs;
final screen = [
  const Page1(),
  const Page2(),
  const Page3(),
  const Page4(),
  const Page5(),
];
...
body: screen[selectedIndex.value],
...

Even if I change the variable value (like 0.obs to 1.obs ), page not changing, why??即使我改变了变量值(比如0.obs1.obs ),页面也没有改变,为什么?

next of, In my bottomnavbar.dart file, I have extracted and made a widget for my nav bar 'items' .接下来,在我的bottomnavbar.dart文件中,我提取并为我的nav bar 'items'制作了一个小部件。 And I have tried to wrap the item widget with Obx:我试图用 Obx 包装项目小部件:

Widget bnbItems(String image, int index, double height) {
return Obx(
  () => InkWell(
    splashColor: Theme.of(context).brightness == Brightness.dark
        ? Colors.white.withOpacity(0.5)
        : Colors.pink.withOpacity(0.5),
    enableFeedback: true,
    onTap: () => setState(() {
      selectedIndex.value = index;
      _controller.animateTo(index / 4);
      // print(selectedIndex);
    }),
    child: Container(
      alignment: Alignment.center,
      width: 50,
      height: 50,
      child: Padding(
        padding: const EdgeInsets.only(top: 5.0),
        child: Image.asset(
          image,
          height: height,
        ),
      ),
    ),
  ),
);}

and I am getting this error:我收到此错误:

[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.

Can anyone give me the solution with some code and explanation?谁能给我一些代码和解释的解决方案? And also how will I be able to set a particular screen as the initial screen?以及如何将特定屏幕设置为初始屏幕?

why you are using setState in GetX structure?为什么在 GetX 结构中使用 setState?

Try this code for onTap()为 onTap() 尝试此代码

 onTap: () {
  selectedIndex.value = index;
  _controller.animateTo(index / 4);
  // print(selectedIndex);
},

to set initial screen use index no of that screen in var selectedIndex = 0.obs;var selectedIndex = 0.obs; instead of 0.而不是 0。

Replace on tap with this code用此代码替换点击

onTap: () {
   selectedIndex.value = 1; // page index you want to view
},

then remove Obx(()=> on bnbItems widget然后删除Obx(()=>

Widget bnbItems(String image, int index, double height) {
  return InkWell(
    splashColor: Theme.of(context).brightness == Brightness.dark
        ? Colors.white.withOpacity(0.5)
        : Colors.pink.withOpacity(0.5),
    enableFeedback: true,
    onTap: () {
       selectedIndex.value = 1; // page index you want to view
    },
    child: Container(
      alignment: Alignment.center,
      width: 50,
      height: 50,
      child: Padding(
        padding: const EdgeInsets.only(top: 5.0),
        child: Image.asset(
          image,
          height: height,
        ),
      ),
    ),
);}

then use Obx(()=> wrapper on the body's widget然后在主体的小部件上使用Obx(()=>包装器

body: Obx(() => screen[selectedIndex.value]),

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从另一个 dart 文件的其他 dart 文件更改选定的索引值? - How to change selected index value from other dart file of another dart file? 如何从其他 dart 文件中传递值? - How to pass the value from other dart file? 在其他Dart文件中颤振更改布尔值(使用setState) - Flutter change bool (with setState) in other dart file 如何在 flutter 中从一个 dart 文件访问 static 列表到另一个文件? - How to access the static list from one dart file to another in flutter? 如何将多个 dart 文件中的字符串传递给 flutter 中的一个 dart 文件? - How to pass strings from multiple dart files and get it to one dart file in flutter? 如何从 javascript 到 flutter dart 文件进行通信? - How to communicate from javascript to flutter dart file? 如何将其他 dart 文件导入 flutter 中的有状态小部件? - How can I import other dart file to stateful widget in flutter? Flutter 访问其他 dart 文件中的变量 - Flutter access variable in other dart file 从另一个.dart文件导入变量到flutter中的.dart文件 - Import variables from another .dart file to .dart file in flutter 将快照数据从一个 dart 文件传递到 flutter 项目中的另一个文件 - Pass Snapshot Data from One dart file to another in flutter project
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM