首页 > 解决方案 > PreferredSize 和 GetX 的使用

问题描述

我的 AppBar 具有 git 底部属性和执行类,但在此类中BottomOfAppBar忽略了颤振。preferredSize

如果 TabBar 被禁用,我的目标是让 AppBar 的高度设置为 0px。(tabbarenable2 值是自己的主要目标,但我找不到动态设置 AppBar 高度的方法)

这是代码:

class BottomOfAppBar extends StatelessWidget with PreferredSizeWidget {
  final TabBarController controller;

  BottomOfAppBar({Key? key, required this.tabs, required this.controller})
      : super(key: key);
  final List<Widget> tabs;
  final tabbarenable2 = Get.find<RxBool>(tag: 'tabbarenable');

  @override
  Widget build(BuildContext context) {
    return Obx(
      () => PreferredSize(
        preferredSize: tabbarenable2.value
            ? const Size.fromHeight(28.0)
            : const Size.fromHeight(0.0),
        child: ColoredBox(
          color: Colors.white,
          child: Column(
            children: [
              tabbarenable2.value
                  ? TabBar(
                      labelColor: Colors.purple[100],
                      indicatorColor: Colors.purple,
                      isScrollable: true,
                      labelPadding: const EdgeInsets.only(left: 8.0),
                      tabs: tabs)
                  : const Text('noTabBar')
            ],
          ),
        ),
      ),
    );
  }

  @override
  Size get preferredSize =>
      tabbarenable2.value ? Size.fromHeight(28.0) : Size.fromHeight(0.0);
}

有什么帮助吗?

第一次启动一切似乎都很好:

在此处输入图像描述

导航到视频 ( tabbarenable= false)

在此处输入图像描述

它只是更正自己首先刷新页面(正确的tabBar高度)

在此处输入图像描述

标签: flutterdartflutter-getx

解决方案


好的,只需尝试以下代码段,如果它不起作用,请让我看看用法

  • 我在这里所做的尝试是在显示小部件之前进行检查,也许正确的位置将在“bottomAppBar:[检查这里]”
  • 注意不要检查 build 方法中的布尔值,因为这是一遍又一遍地重建小部件的坏方法。

    class BottomOfAppBar extends StatelessWidget with PreferredSizeWidget {
    final TabBarController controller;
    
    BottomOfAppBar({Key? key, required this.tabs, required this.controller})
    : super(key: key);
    final List<Widget> tabs;
    final tabbarenable2 = Get.find<RxBool>(tag: 'tabbarenable');
    
    @override
    Widget build(BuildContext context) {
      return Obx(
        () => !tabbarenable2.value
              ? SizedBox() : PreferredSize(
          preferredSize: const Size.fromHeight(28.0),
      child: ColoredBox(
        color: Colors.white,
        child: Column(
          children: [
            TabBar(
                    labelColor: Colors.purple[100],
                    indicatorColor: Colors.purple,
                    isScrollable: true,
                    labelPadding: const EdgeInsets.only(left: 8.0),
                    tabs: tabs)
          ],
        ),
      ),
    ),
    );
    }
    
    @override
    Size get preferredSize =>
    tabbarenable2.value ? Size.fromHeight(28.0) : Size.fromHeight(0.0);
    }
    

推荐阅读