【问题标题】:TabBarView which in column in SingleChildScrollView needs to wrap in a specified height container在 SingleChildScrollView 的列中的 TabBarView 需要包装在指定高度的容器中
【发布时间】:2020-07-30 10:34:34
【问题描述】:
  1. 我有一个页面,其中包含一个 TabBar、一些其他小部件和一个与上述 TabBar 相关的 TabBarView。构建实现如下所示。
  @override
  Widget build(BuildContext context) {
    return Container(
      decoration: BoxDecoration(color: defaultBackgroundColor),
      child: Stack(children: [
        Image.asset(
          'assets/background/around.jpg',
          width: MediaQuery.of(context).size.width,
          height: 400,
          fit: BoxFit.fill,
        ),
        SingleChildScrollView(
          child: Column(
            children: <Widget>[
              AroundHeader(),
              TabBar(
                controller: _tabController,
                unselectedLabelColor: Colors.white,
                indicatorSize: TabBarIndicatorSize.tab,
                labelColor: Colors.blue,
                indicator: BoxDecoration(
                  color: Colors.transparent,
                  border:
                      Border(bottom: BorderSide(color: Colors.blue, width: 3)),
                ),
                tabs: [
                  ...data.map(
                    (item) => Tab(
                      child: Container(
                        child: Align(
                          alignment: Alignment.center,
                          child: Text(item, style: titleStyle),
                        ),
                      ),
                    ),
                  ),
                ],
              ),
              SizedBox(height: 10),
              Padding(
                padding: const EdgeInsets.symmetric(vertical: commonMargin),
                child: Container(
                  height: MediaQuery.of(context).size.height + 300,
                  width: MediaQuery.of(context).size.width - 2 * commonMargin,
                  child: TabBarView(
                    controller: _tabController,
                    children: <Widget>[
                      AroundRecommend1(),
                      AroundRecommend2(),
                      AroundRecommend3(),
                      AroundRecommend4(),
                    ],
                  ),
                ),
              ),
            ],
          ),
        ),
      ]),
    );
  }
  1. 由于 TabBarView 中的内容可能高度可变,例如,AroundRecommend 是从 Column 构建的。
  2. 现在我必须手动计算 TabBarView 子项的最大高度。
  3. 我尝试用ExplandedSizedBox.expandIntrinsicHeight 等替换受约束的容器,但都失败了,但没有提供高度等异常。

【问题讨论】:

    标签: flutter


    【解决方案1】:

    您希望将小部件包装在 Expanded 中的列内,而不是 Column 本身

      Column(
          children: <Widget>[
            Expanded(
              child: Container(
                 height: 200,
                color: Colors.redAccent,
              ),
            ),
          ],
        );
    

    【讨论】:

    • 这行不通。因为我在 TabBarView 中有一些很大的高度内容,并且它引入了滚动行为,它更像是收缩包装或 sliver 行为。
    • 所以我想我很困惑,如果你能解释一下扩展是如何像收缩包装一样的?
    • 你还有这个问题吗
    猜你喜欢
    • 2020-06-15
    • 2021-11-11
    • 1970-01-01
    • 2019-07-05
    • 2020-01-03
    • 1970-01-01
    • 2021-01-18
    • 2021-10-02
    • 1970-01-01
    相关资源
    最近更新 更多