[英]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.obs
到1.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.
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.