【问题标题】:Flutter: How to place a button in the middle of 2 containers?Flutter:如何在 2 个容器中间放置一个按钮?
【发布时间】:2019-07-18 10:33:06
【问题描述】:

UI Layout

如果紫色区域位于展开的小部件内,我将如何定位按钮?我已经通过为紫色容器设置一个固定高度来实现该 UI,但是如果高度是可变的,则无法理解如何实现相同的效果,具体取决于内容。

我可以通过使用 Stack 和 Positioned 小部件来接近,但按钮并不是真正可点击的。如果有人能给我大致了解如何实现预期目标,我将不胜感激。

这是我的尝试(演示案例)

  @override
  Widget build(BuildContext context) {
    return Column(
      mainAxisAlignment: MainAxisAlignment.start,
      crossAxisAlignment: CrossAxisAlignment.stretch,
      children: <Widget>[
        Expanded(
          flex: 1,
          child: SingleChildScrollView(
              child: Column(
                children: <Widget>[
                  Container(
                    decoration: BoxDecoration(
                      color: Colors.redAccent,
                    ),
                    child: Padding(
                      padding: const EdgeInsets.all(20.0),
                      child: Stack(
                          children: <Widget> [
                            Padding(
                              padding: const EdgeInsets.only(bottom: 40.0),
                              child: Column(
                                crossAxisAlignment: CrossAxisAlignment.start,
                                children: <Widget>[
                                  Icon(
                                      Icons.book,
                                      color: Colors.white,
                                      size: 40.0
                                  ),
                                  Container(
                                    width: 90.0,
                                    child: new Divider(color: Colors.white),
                                  ),
                                  Text(
                                    "Some random text -",
                                    style: TextStyle(color: Colors.white, fontSize: 25.0),
                                  ),
                                  Padding(
                                    padding: const EdgeInsets.only(top: 8.0, right: 70.0),
                                    child: Row(
                                      children: <Widget>[
                                        Flexible(
                                          child: Text(
                                            'More random text that can vary a lot!',
                                            style: TextStyle(
                                              color: Colors.white,
                                              fontSize: 16.0,
                                              fontWeight: FontWeight.bold,
                                            ),
                                          ),
                                        ),
                                      ],
                                    ),
                                  ),
                                  Padding(
                                    padding: const EdgeInsets.only(top: 8.0),
                                    child: Row(
                                      children: <Widget>[
                                        Text(
                                          'Random text ',
                                          style: TextStyle(
                                            color: Colors.white,
                                            fontSize: 16.0,
                                            fontWeight: FontWeight.bold,
                                          ),
                                        ),
                                      ],
                                    ),
                                  ),
                                  Padding(
                                    padding: const EdgeInsets.only(top: 8.0),
                                    child: Row(
                                      children: <Widget>[
                                        Text(
                                          'Random',
                                          style: TextStyle(
                                            color: Colors.white,
                                            fontSize: 16.0,
                                            fontWeight: FontWeight.bold,
                                          ),
                                        ),
                                      ],
                                    ),
                                  ),
                                ],
                              ),
                            ),
                            Positioned(
                              bottom: 0.0,
                              left: MediaQuery.of(context).size.width - 100.0,
                              child: FractionalTranslation(
                                translation: const Offset(0.0, 0.8),
                                child: FloatingActionButton(
                                  backgroundColor: Colors.white,
                                  foregroundColor: Colors.redAccent,
                                  child: new Icon(
                                      Icons.map
                                  ),
                                  onPressed: () {

                                  },
                                ),
                              ),
                            ),
                          ]
                      ),
                    ),
                  ),
                  Column(
                    mainAxisAlignment: MainAxisAlignment.start,
                    crossAxisAlignment: CrossAxisAlignment.stretch,
                    children: <Widget>[
                      Padding(
                        padding: const EdgeInsets.only(top: 8.0, left: 8.0),
                        child: Row(
                          children: <Widget>[
                            Flexible(
                              child: Text(
                                  "Random text",
                                  style: new TextStyle(
                                      fontFamily: 'Raleway',
                                      fontSize: 16.0,
                                      color: Colors.black38
                                  )
                              ),
                            ),
                          ],
                        ),
                      ),
                      Padding(
                        padding: const EdgeInsets.all(8.0),
                        child: Text(
                            "Random text - long text",
                            style: new TextStyle(
                                fontFamily: 'Raleway',
                                fontSize: 18.0
                            )
                        ),
                      ),
                    ],
                  )
                ],
              )
          ),
        ),
      ],
    );

【问题讨论】:

  • 你可以用你的代码更新问题,看看我们能如何帮助你:)
  • @diegoveloper 我已经更改了我的应用程序的逻辑,但将创建一个测试用例来展示我的成就。
  • 好的,听起来不错:)
  • @diegoveloper 我发布了代码。如果您有任何意见,请告诉我。
  • 不知道这是否有帮助,但这是我的快速提示:您可以简单地将 FAB 向下偏移一半高度 (Offset(0, 0.5)),然后将其放置在紫色小部件的底部。

标签: flutter flutter-layout


【解决方案1】:

解释我在 cmets 中描述的内容:

您可以使用 FractionalTranslation 小部件将您的 FAB 向下移动一半。

FractionalTranslation(translation: Offset(0, .5), child: FloatingActionButton(...))

这需要您事先将其放置在紫色区域的底部(参考您的屏幕截图),但我假设您已经弄清楚了。

【讨论】:

  • 它有效,但我怀疑硬编码的偏移量是否能在不同类型的屏幕分辨率下完美运行。
  • @DhyanV 这是一个相对偏移量,即无论屏幕分辨率如何,它总是会偏移一半。
  • 今天我注意到一个不同的问题,按钮底部没有响应点击(悬挂在容器外的部分)
猜你喜欢
  • 2010-10-16
  • 1970-01-01
  • 2020-07-16
  • 2023-01-19
  • 2016-06-24
  • 1970-01-01
  • 2011-01-16
  • 2013-06-14
相关资源
最近更新 更多