【问题标题】:What is the best flutter widget to use to achieve this kinda layout?用于实现这种布局的最佳颤振小部件是什么?
【发布时间】:2020-11-10 15:25:07
【问题描述】:

这是我要实现的 UI,我上周才开始玩 Flutter,所以我不是很流畅。我想知道实现这种布局的最佳方法是什么。

这是我通过堆栈和列得到的:

这是代码:

Column(
  children: <Widget>[
    Expanded(
      child: Container(
        constraints: BoxConstraints.expand(height: 150),
        color: Colors.blue,
        child: Stack(
          overflow: Overflow.visible,
          children: <Widget>[
            Positioned(
              bottom: -40,
              left: MediaQuery.of(context).size.width * 0.4,
              // alignment: Alignment.bottomCenter,
              child: Container(
                constraints: BoxConstraints.expand(height: 80, width: 80),
                color: Colors.green,
              ),
            ),
          ],
        ),
      ),
      flex: 3,
    ),
    Expanded(
      child: Container(
        margin: EdgeInsets.all(15.0),
        color: Colors.red,
      ),
      flex: 7,
    )
  ],
);

【问题讨论】:

  • 我知道堆栈可以与溢出一起使用,但这感觉有点像 hack。
  • 它不应该让人觉得是 hack,因为它不是。
  • 我遇到堆栈的一个问题是,我无法阻止包含名称 (David Beckham) 和模型/超级开始的文本小部件的白色容器被扩展为完整父级跨度>
  • 您到底面临什么问题?图片中的布局方面不止一种。
  • 每当我使用堆栈、容器和列的组合时,我都没有得到想要的结果,加号、配置文件和消息图标要么显示在白色背景容器下方,要么根本不会溢出.

标签: flutter flutter-layout


【解决方案1】:

我制作了类似于您提供的带有虚拟图像的图像和一些图标,您可以根据需要更改图标,使用媒体查询大小而不是固定的填充顶部

class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text(widget.title),
        ),
        body: SingleChildScrollView(
            child: Column(
          children: [
            Stack(children: [
              Container(
                child: FittedBox(
                    fit: BoxFit.fill,
                    child: Image.network('https://picsum.photos/300?image=10')),
                height: 150,
                width: double.infinity,
              ),
              Row(
                mainAxisAlignment: MainAxisAlignment.spaceAround,
                children: [
                  Padding(
                    padding: const EdgeInsets.only(top: 75),
                    child: MaterialButton(
                      onPressed: () {},
                      elevation: 2.0,
                      color: Colors.red,
                      child: Icon(
                        Icons.message,
                        size: 27.0,
                      ),
                      padding: EdgeInsets.all(20),
                      shape: CircleBorder(),
                    ),
                  ),
                  Padding(
                    padding: const EdgeInsets.only(top: 75),
                    child: Column(children: [
                      CircleAvatar(
                        radius: 55.0,
                        backgroundImage:
                            NetworkImage('https://i.pravatar.cc/150?img=54'),
                        backgroundColor: Colors.transparent,
                      ),
                      Text("David Beckham"),
                      Text("Model/SuperStar")
                    ]),
                  ),
                  Padding(
                    padding: const EdgeInsets.only(top: 75),
                    child: MaterialButton(
                      onPressed: () {},
                      elevation: 2.0,
                      color: Colors.blue,
                      child: Icon(
                        Icons.add,
                        size: 27.0,
                      ),
                      padding: EdgeInsets.all(20),
                      shape: CircleBorder(),
                    ),
                  ),
                ],
              ),
            ]),
            Container(height: 100, color: Colors.red),
            Container(height: 100, color: Colors.green),
            Container(height: 100, color: Colors.yellow),
            Container(height: 100, color: Colors.blue),
          ],
        )));
  }
}

【讨论】:

  • 感谢更多我正在寻找的东西,我不知道您也可以使用填充来定位小部件,就像在 css 中一样。
  • 有一个叫做Positioned的东西,如果你想要api.flutter.dev/flutter/widgets/Positioned-class.html@jeffdropsit
  • 谢谢,我看到了。只是在我发布的示例中,内容总是溢出到红色小部件下方。
  • 好的,我提供的代码没有问题,如果有请告诉我@jeffdropsit
  • 感谢您的提问,我怀疑问题出在代码上,但既然您提到了它,我仍在努力让容器填充行小部件下方的空白区域。我的意思是,只要我在该行小部件下方添加一个容器,它就会隐藏图标或弄乱视图..
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-09
  • 1970-01-01
相关资源
最近更新 更多