【问题标题】:How to position this text in container in Flutter?如何在 Flutter 的容器中定位此文本?
【发布时间】:2020-01-26 11:49:11
【问题描述】:

我正在将我用 Kotlin 编写的应用程序重写为 Flutter,但我正在为简单的布局而苦苦挣扎。

我正在尝试从 Gmail 重新创建布局,但我不知道如何定位某些内容。

这是来自 Kotlin 应用的图片:

这是来自我的 Flutter 应用的图片:

我说的是“odebrane”文本。我希望它与我的 Kotlin 应用程序中的完全一样。我希望其中的文本居中,并且我希望它与“凭证”文本的高度相同。有谁能帮助我吗?

代码:

import 'package:flutter/material.dart';

class GeneratedMailCouponScreen extends StatelessWidget {
  final String couponImage;

  GeneratedMailCouponScreen({Key key, @required this.couponImage}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
      body: SafeArea(
        child: Container(
          padding: EdgeInsets.all(16.0),
          child: Column(
            children: [
              Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  Row(
                    children: [
                      Icon(
                        Icons.arrow_back
                      )
                    ],
                  ),
                  Row(
                    children: [
                      Icon(
                        Icons.archive
                      ),
                      SizedBox(width: 5.0),
                      Icon(
                        Icons.delete
                      ),
                      SizedBox(width: 5.0),
                      Icon(
                        Icons.mail
                      ),
                      SizedBox(width: 5.0),
                      Icon(
                        Icons.more_vert
                      )
                    ],
                  )
                ],
              ),
              SizedBox(height: 16.0),
              Row(
                children: [
                  Text('Voucher', style: TextStyle(color: Colors.black, fontSize: 20.0)),
                  SizedBox(width: 16.0,),
                  Container(
                    color: Colors.grey[300],
                    width: 50.0,
                    height: 8.0,
                    child: Center(
                      child: Text('Odebrane', style: TextStyle(color: Colors.black, fontSize: 8.0),),
                    )
                  )
                ],
              )
            ],
          ),
        ),
      ),
    );
  }
}

【问题讨论】:

  • 将Center标签改为Padding,内边距值为2或4
  • 您是否尝试过FitterBox 使此文本在 Container 中居中?

标签: flutter dart


【解决方案1】:

在设备上测试后,这是我的解决方案
您只需要更改几行代码

// from
Container(
  color: Colors.grey[300],
  width: 50.0,
  height: 8.0,
  child: Center(
    child: Text('Odebrane', style: TextStyle(color: Colors.black, fontSize: 8.0),),
     )
   )

// to
Container(
      color: Colors.grey[300],
      child: Padding(
            padding: EdgeInsets.fromLTRB(4, 2, 4, 2),
             child: Text('Odebrane', style: TextStyle(color: Colors.black, fontSize: 10.0),),
      )
   )

【讨论】:

  • 那么“Odebrane”文本与“Voucher”文本的高度如何?
  • 你可以尝试设置X = 20-4*2(20是Voucher fontsize,4*2是padding top and bottom)
  • 我该怎么做?你能帮我解决这个问题吗?
  • 只需将 Odebrane Text 的 fontSize 设置为 12 (20-4*2)。有什么问题?
猜你喜欢
  • 2022-12-17
  • 2015-06-17
  • 2021-07-18
  • 2021-04-13
  • 2013-06-22
  • 1970-01-01
  • 1970-01-01
  • 2021-12-11
  • 1970-01-01
相关资源
最近更新 更多