3

我正在尝试实现一个bottomNavigationBar,但我无法完成它,我正在使用Get 来处理应用程序的路由和状态。

我是 Flutter 新手,但阅读文档我仍然不明白

这是主要的小部件。

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),
                      )
                  ],
                ),
              ),
            ),
          ),
        ),
      ),
    );
  }
}
4

2 回答 2

3

Obx当您使用或Getx小部件而不插入该小部件的可观察变量时,Getx 将始终抛出该错误。因此,如果您不尝试根据存在于 exends 的类中的变量的更新值来重建小部件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());

于 2021-02-26T00:40:13.237 回答
1

只需像这样删除包装您的 Row 小部件的 Obx 小部件:

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: 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: () {}),
            ],
          );
      ),
    );

  }
}

为什么?因为您没有在小部件树中使用任何可观察(obs/Rx)变量,这会在值更改时触发重建。因此,GetX 抱怨是有充分理由的。

于 2021-02-27T15:39:39.490 回答