flutter - 错误 [Get] 检测到 GetX 的不当使用。使用底部导航栏

标签 flutter dart flutter-layout flutter-getx

我正在尝试实现一个bottomNavigationBar,但是我无法完成它,我正在使用Get 来处理应用程序的路由和状态。
我是新手,但阅读文档我仍然不明白
这是主要的小部件。

Widget build(BuildContext context) {
return SafeArea(
  child: Scaffold(
      appBar: AppBar(
        backgroundColor: AppColors.black,
        title: Center(
          child: CommonAssetImage(
            asset: 'logo.png',
            color: AppColors.white,
            height: 30,
          ),
        ),
      ),
      body: BodyTabsScreen(),
      bottomNavigationBar: HomeScreenBottomNavigatorBar()),
);
}
然后,我有这个小部件,可以调用其他小部件。在这个小部件中,我使用了 Obs。
class HomeScreenBottomNavigatorBar extends StatelessWidget {
  const HomeScreenBottomNavigatorBar({Key key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Material(
      elevation: 10,
      color: AppColors.white,
      child: Container(
        height: 60,
        padding: const EdgeInsets.symmetric(horizontal: 27),
        color: AppColors.white,
        child: Obx(() {
          return Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              TabsScreenBottomNavigationTab(
                  isActive: true,
                  label: 'Buy',
                  icon: Icons.home,
                  onTap: () {}),
              TabsScreenBottomNavigationTab(
                  label: 'My account',
                  // icon: IkramIcons.user,
                  // iconSize: 20,
                  icon: (Icons.home),
                  onTap: () {}),
            ],
          );
        }),
      ),
    );

  }
}
class TabsScreenBottomNavigationTab extends StatelessWidget {
  final String label;
  final IconData icon;
  final Widget image;
  final VoidCallback onTap;
  final bool isActive;
  final double iconSize;
  const TabsScreenBottomNavigationTab({
    Key key,
    this.label,
    this.icon,
    this.image,
    this.onTap,
    this.isActive,
    this.iconSize = 20,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    final _inactiveTextStyle = Theme.of(context).textTheme.bodyText2;
    final _activeTextStyle =
        _inactiveTextStyle.copyWith(color: AppColors.white);
    const _commonDuration = Duration(milliseconds: 200);
    final _availableSpace = MediaQuery.of(context).size.width - 27 * 2;
    final _inactiveWidth = _availableSpace * .2;
    final _activeWidth = _availableSpace * .35;
    return AnimatedContainer(
      duration: _commonDuration,
      width: isActive ? _activeWidth : _inactiveWidth,
      height: 35,
      child: Material(
        color: Colors.transparent,
        shape: const StadiumBorder(),
        clipBehavior: Clip.antiAlias,
        child: AnimatedContainer(
          duration: _commonDuration,
          child: Material(
            color: Colors.transparent,
            child: InkWell(
              onTap: onTap,
              child: AnimatedDefaultTextStyle(
                style: isActive ? _activeTextStyle : _inactiveTextStyle,
                duration: _commonDuration,
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    if (icon != null)
                      Icon(
                        icon,
                        size: iconSize,
                        color: isActive ? AppColors.white : AppColors.black,
                      ),
                    if (image != null) image,
                    if (isActive)
                      Container(
                        margin: const EdgeInsets.only(left: 8),
                        child: Text(label),
                      )
                  ],
                ),
              ),
            ),
          ),
        ),
      ),
    );
  }
}

最佳答案

当您使用 Obx 时,Getx 将始终抛出该错误。或 Getx小部件而不插入该小部件的可观察变量。因此,如果您不尝试根据位于扩展 GetxController 的类中的变量的更新值重建小部件。 ,然后不要使用 Getx 小部件。
如果您只是想使用 Getx用于路由,然后确保更改您的 MaterialAppGetMaterialApp并像这样定义你的路线。

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return GetMaterialApp(
      home: Page1(),
      getPages: [
       GetPage(name: Page1.id, page: () =>  Page1()), // add: static const id = 'your_page_name'; on each page to avoid using raw strings for routing
       GetPage(name: Page2.id, page: () =>  Page2()),
      ],
    );
  }
}
然后在 onTap底部导航栏的只是使用Get.to(Page2());

关于flutter - 错误 [Get] 检测到 GetX 的不当使用。使用底部导航栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66377555/

相关文章:

flutter - 在调用StreamController#close()时未触发doOnCancel()

pdf - 我想从PDF文件中获取总页数

ios - Firebase 云消息传递仅在发布时才起作用

flutter - 在 initState 中请求 TextField 焦点

android - flutter - 底部溢出像素数

flutter - 在这种情况下,如何在我点击_card时打开新路线/页面?

flutter - 如何集成flutter dotenv?

firebase - Flutter Firebase-如何将用户uid添加到文档

flutter - 分段条滚动条问题

flutter - 如何为动画中的主题切换添加动画?