【问题标题】:Flutter Layout: Add a Text above a gridFlutter 布局:在网格上方添加文本
【发布时间】:2019-01-14 22:08:50
【问题描述】:

在我的程序中,我重用了 flutter_gallery 演示中所做的部分工作:https://github.com/flutter/flutter/blob/master/examples/flutter_gallery/lib/demo/pesto_demo.dart

更准确地说,我正在重用创建RecipeGridPage 的方法。 它工作正常,但我现在想在网格上方添加一个文本小部件。 我用过这样的东西:

Column(
  children: <Widget>[
    Text('BlaBlaBla'),
    RecipeGridPage(recipes: kPestoRecipes),
  ],
)

但我收到以下错误: “RenderCustomMultiChildLayoutBox 对象在布局期间被赋予了无限大小”。

我应该在哪个小部件中包装 RecipeGridPage 以便能够在网格上方添加单个文本?

谢谢!

【问题讨论】:

  • 试试 SingleChildScrollView

标签: flutter


【解决方案1】:

尝试使用Stack,只需根据需要更改填充:

    Stack(
            children: <Widget>[
              Text('BlaBlaBla'),
              Padding(
                padding: EdgeInsets.all(20.0),
                child: RecipeGridPage(recipes: kPestoRecipes),
              )
            ],
          ),

更新

这是您可以尝试的另一种解决方案,您必须在该文件的第 97 行添加标题:https://github.com/flutter/flutter/blob/master/examples/flutter_gallery/lib/demo/pesto_demo.dart

这是一个基本示例:

      @override
      Widget build(BuildContext context) {
        return Scaffold(
          body: CustomScrollView(
            slivers: <Widget>[
              _buildAppBar(),
              _buildHeader(),
              _buildBody(),
            ],
          ),
        );
      }

      SliverToBoxAdapter _buildHeader() {
        return SliverToBoxAdapter(
          child: Text("Header"),
        );
      }

      SliverAppBar _buildAppBar() {
        return SliverAppBar(
          title: Text("Title"),
        );
      }

      SliverList _buildBody() {
        return SliverList(
          delegate: SliverChildBuilderDelegate(
            (context, index) {
              return ListTile(title: Text("item :$index"));
            },
            childCount: 20,
          ),
        );
      } 

【讨论】:

  • 我试过了,但填充区域与文本的一部分重叠。我可以在 padding 上玩,但我想有一个解决方案可以让 Text 小部件在顶部,滚动网格占据下面的所有剩余空间。
  • 检查还有更多解决方案,其中之一是在RecipeGridPage 的CustomScrollView 中添加一个SliverPersistentHeader,但您将不得不编写更多代码。
  • 检查我更新的答案,忘记你问题的专栏。
猜你喜欢
  • 2017-10-26
  • 1970-01-01
  • 2019-01-25
  • 1970-01-01
  • 2015-07-29
  • 2020-07-16
  • 1970-01-01
  • 2016-03-20
  • 1970-01-01
相关资源
最近更新 更多