如何在Flutter中在选项卡上方显示一个小部件?

huangapple go评论59阅读模式
英文:

How to display a widget above a tab in Flutter?

问题

我有一个底部选项卡栏。

当满足某些条件时,我需要在给定选项卡上方显示一个小部件,如下所示:

如何在Flutter中在选项卡上方显示一个小部件?

为了简单起见,我在这里显示一个Text("ALERT"),但在实际应用中,它将是一个可点击的小部件。

我该如何做呢?

英文:

I have a bottom tab bar.

When some conditions are met, I need to display a widget abode a given tab as follows:

如何在Flutter中在选项卡上方显示一个小部件?

For simplicity, I display a Text("ALERT") here, but in the real app it'll be a tapable widget.

How can I do that?

答案1

得分: 0

只需将您拥有的任何小部件(例如,在您的情况下是Text小部件)包装在一个GestureDetector小部件中,如下所示:

GestureDetector(
  onTap: () {
    setState(() {
      // 点击时发生的操作
    });
  },
),
英文:

Just wrap whatever widget you have (in your case Text widget for example) with a GestureDetector widget like this:

GestureDetector(
        onTap: () {
          setState(() {
            // What happens with the tap
          });
        },
      ),

答案2

得分: 0

  1. 使用一个 Column,先插入小部件,然后在最后插入选项卡栏:但这会使您的底部应用看起来比预期的要高。这会给您:

如何在Flutter中在选项卡上方显示一个小部件?

  1. 添加 extendBody:true,使实际选项卡栏上方的区域变为透明,从而去除不需要的区域:

如何在Flutter中在选项卡上方显示一个小部件?

Scaffold(
  extendBody: true,
  bottomNavigationBar: Column(
    mainAxisSize: MainAxisSize.min,
    children: [
      Chip(
        label: Text("Draft pending"),
        backgroundColor: Colors.amber,
      ),
      bottomTabBar,
    ]
  )
  // 哈哈哈
),
英文:

I finally found a solution in two steps:

  1. Use a Column and insert the widget first, and the tabbar last: but this makes your bottom app appears taller than what one wants. This gives you:

如何在Flutter中在选项卡上方显示一个小部件?

  1. Add extendBody:true which makes the area above the actual TabBar transparent so that removes the unwanted area:

如何在Flutter中在选项卡上方显示一个小部件?

Scaffold(
  extendBody: true,
  bottomNavigationBar: Column(
    mainAxisSize: MainAxisSize.min,
    children: [
      Chip(
        label: Text("Draft pending"),
        backgroundColor: Colors.amber,
      ),
      bottomTabBar,
    ]
  )
  // blah blah
),

huangapple
  • 本文由 发表于 2023年7月3日 06:22:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/76601007.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定