PreferredSize 和 GetX 用法
PreferredSize and GetX usage
我的 AppBar 有 git 底部属性并执行 BottomOfAppBar
class 但在这个 class.
中忽略 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 高度)
好的,试试下面的代码片段,如果它不起作用,请让我看看用法
- 我在这里做的尝试是在显示小部件之前进行检查,也许正确的位置是“bottomAppBar:[检查此处]”
注意不要检查构建方法中的布尔值,因为这是一遍又一遍地重建小部件的糟糕方法。
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);
}
我删除了 BottomOfAppBar class 并将代码移至 Scaffold Widget。用 0bx() 包装并用 controller.tabbarenable.value
更改检查机制 非常感谢@muhamed-jalal
我的 AppBar 有 git 底部属性并执行 BottomOfAppBar
class 但在这个 class.
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 高度)
好的,试试下面的代码片段,如果它不起作用,请让我看看用法
- 我在这里做的尝试是在显示小部件之前进行检查,也许正确的位置是“bottomAppBar:[检查此处]”
注意不要检查构建方法中的布尔值,因为这是一遍又一遍地重建小部件的糟糕方法。
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); }
我删除了 BottomOfAppBar class 并将代码移至 Scaffold Widget。用 0bx() 包装并用 controller.tabbarenable.value
更改检查机制 非常感谢@muhamed-jalal