flutter - TabBar 阻碍 AppBar 主体
问题描述
我有一个可重用的 AppBar 小部件,我在整个应用程序中都使用了它。在我在 AppBar 底部添加 tabBar 之前,它大部分时间都可以正常工作。TabBar 会上升并阻止大部分 AppBar。我不知道如何解决这个问题。
这是不添加 TabBar 的样子
当我添加一个 tabBar 它看起来像这样
我的 AppBar 代码
class MySearchBar extends StatefulWidget implements PreferredSizeWidget {
final List<Widget> additional;
final BuildContext? myContext;
final PreferredSizeWidget? bottom;
const MySearchBar({Key? key, this.bottom, this.additional = const [], this.myContext}) : super(key: key);
@override
Size get preferredSize => const Size.fromHeight(kToolbarHeight);
@override
_MySearchBarState createState() => _MySearchBarState();
}
class _MySearchBarState extends State<MySearchBar> {
var authRef = Get.find<AuthController>();
var cartRef = Get.find<CartController>();
int amount = 0;
List<Widget> orig = [];
@override
void initState() {
super.initState();
orig.add(
//Some Kind of widget here for actions
);
if (widget.additional.isNotEmpty) {
orig.addAll(widget.additional);
} else {
orig.add(SizedBox(
width: 5,
));
}
}
@override
Widget build(BuildContext context) {
return AppBar(
leading: (widget.myContext!=null)? IconButton(onPressed: (){
Navigator.of(widget.myContext!, rootNavigator: true).pop(widget.myContext);
}, icon: Icon(Icons.arrow_back)): null,
backwardsCompatibility: false,
title: InkWell(
child: Container(
margin: EdgeInsets.all(10),
width: Get.width * 0.7,
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(15),
),
child: InkWell(
child: MyTextField(
enable: false,
border: InputBorder.none,
prefixIcon: Icon(Icons.search),
label: "Search Something".tr,
),
),
),
onTap: () {
pushNewScreen(context, screen: SearchScreen(), withNavBar: false);
},
),
actions: orig,
bottom: widget.bottom,
);
}
}
我使用 TabBar 的屏幕
class FollowerAndFollowingScreenMain extends StatefulWidget {
final bool follower;
final String id;
const FollowerAndFollowingScreenMain(
{Key? key, required this.id, required this.follower})
: super(key: key);
@override
_FollowerAndFollowingScreenMainState createState() =>
_FollowerAndFollowingScreenMainState();
}
class _FollowerAndFollowingScreenMainState extends State<FollowerAndFollowingScreenMain> {
@override
Widget build(BuildContext context) {
return DefaultTabController(
length: 2,
initialIndex: (widget.follower) ? 0 : 1,
child: Scaffold(
appBar: MySearchBar(
myContext: context,
bottom: TabBar(
tabs: [
Tab(
text: "Following".tr,
),
Tab(
text: "Followers".tr,
),
],
),
),
body: _myBody(),
),
);
}
_myBody() {
return TabBarView(children: [
FollowingAndFollowersScreen(
followers: true,
id: widget.id,
),
FollowingAndFollowersScreen(
followers: false,
id: widget.id,
),
]);
}
}
解决方案
尝试向应用栏添加高度,如下所示:
appBar: PreferredSize(
preferredSize: Size.fromHeight(50.0), // here the desired height
child: AppBar(
// ...
)
),
body: // ...
推荐阅读
- angularjs - 用于刷新 $rootScope 的 Angular 1.6.6 捕获事件
- javascript - 传单搜索过滤器仅完全匹配
- javascript - Discord:使用命令从数组中获取数据
- sql - 使用 bigquery 查找特定日期的整体数据
- amazon-web-services - 如何从 ec2 安全组授予对 s3 存储桶的完全访问权限
- google-cloud-platform - 如何在谷歌云中识别组织的所有者
- rust - 在“if let Some(ref mut x) = option”和“if let Some(x) = option.as_mut()”中匹配可变选项引用有什么区别?
- c - 为什么在 asm 中“guarded do”比“jump to middle”好
- apache-spark - 如何在 SparkSQL 中收集“视图”的统计信息?
- discord.py - discord.py:命名执行命令的通道