首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PreferredSize和GetX的使用

PreferredSize和GetX的使用
EN

Stack Overflow用户
提问于 2021-11-14 08:09:19
回答 2查看 817关注 0票数 1

我的AppBar具有git底部属性和执行BottomOfAppBar类,但是在这个类中忽略了preferredSize

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

以下是代码:

代码语言:javascript
复制
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高度)。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-11-14 09:04:24

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

  • 在这里所做的尝试是在显示小部件之前进行检查,也许正确的位置将出现在"bottomAppBar: check here“中,
  • 注意不要检查构建方法中的布尔值,因为这是一种反复重新构建小部件的糟糕方法。

类BottomOfAppBar用PreferredSizeWidget (最终的TabBarController控制器;BottomOfAppBar({Key?键,必需的this.tabs,this.controller}:super( key : key);final List选项卡;tabbarenable2 =Get.find(标记:'tabbarenable');@override (BuildContext上下文){返回Obx( () => !tabbarenable2.value )?SizedBox():PreferredSize( preferredSize: const Size.fromHeight(28.0) ),ColoredBox(颜色: Colors.white,child: Column:[ TabBar( labelColor: Colors.purple100,indicatorColor: Colors.purple,isScrollable: true ),labelPadding: const EdgeInsets.only(左: 8.0),标签:标签) ],);@覆盖大小获取preferredSize => tabbarenable2.value?Size.fromHeight(28.0):Size.fromHeight(0.0);}

票数 1
EN

Stack Overflow用户

发布于 2021-11-15 15:45:33

我删除了BottomOfAppBar类,并将代码移到Scaffold。用0bx()包装,用controller.tabbarenable.value更改检查机制,非常感谢@muhamed

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69961336

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档