【问题标题】:Flutter TextField Hint Text Not Align CenterFlutter TextField 提示文本不居中对齐
【发布时间】:2021-07-23 08:39:09
【问题描述】:
                                      Container(
                                        alignment: Alignment.center,
                                        decoration: BoxDecoration(
                                          borderRadius: BorderRadius.only(
                                              topLeft: Radius.circular(5.0),
                                              bottomLeft: Radius.circular(5.0)
                                          ),
                                          color: Colors.white,
                                        ),
                                        padding: EdgeInsets.only(left: 15.w, right: 15.w),
                                        child: TextField(
                                          maxLines: 1,
                                          style: TextStyle(
                                            fontSize: 12, //This line!!!!!!!!!
                                            textBaseline: TextBaseline.alphabetic,
                                          ),
                                          decoration: InputDecoration(
                                            border: InputBorder.none,
                                            hintText: configNotifier.translationObject["SearchOrder"],
                                            hintStyle: TextStyle(
                                              color: fromCSSColor("#808080"),
                                              textBaseline: TextBaseline.alphabetic
                                            ),
                                          ),
                                        ),
                                      )

在我添加“fontSize: 12”之前

在我添加“fontSize: 12”之后

如何将字体大小应用于垂直居中对齐的文本字段?

备注:我尝试过 "textAlignVertical: TextAlignVertical.center" 和提示->textstyle "height:1.0" 不起作用

========编辑1===========

IOS 设备可以正常工作,但不能在三星 Galaxy Tab A(8.0",2019 型号为 SM-T295C)上运行。

========编辑2===========

带有父小部件

Container(
    margin: EdgeInsets.only(left: 70, right: 70),
    height: 50.h,
    child: Row(
      crossAxisAlignment: CrossAxisAlignment.center,
      children: [
        Expanded(
          child: Container(
            alignment: Alignment.center,
            decoration: BoxDecoration(
              borderRadius: BorderRadius.only(
                  topLeft: Radius.circular(5.0),
                  bottomLeft: Radius.circular(5.0)
              ),
              color: Colors.white,
            ),
            padding: EdgeInsets.only(left: 15.w, right: 15.w),
            child: TextField(
              maxLines: 1,
              style: TextStyle(
                fontSize: 12, //This line!!!!!!!!!
                textBaseline: TextBaseline.alphabetic,
              ),
              decoration: InputDecoration(
                border: InputBorder.none,
                hintText: configNotifier.translationObject["SearchOrder"],
                hintStyle: TextStyle(
                    color: fromCSSColor("#808080"),
                    textBaseline: TextBaseline.alphabetic
                ),
              ),
            ),
          ),
        ),
        GestureDetector(
            onTap: (){
              //...
            },
            child: Container(
                width: 100.w,
                height: 50.h,
                margin: EdgeInsets.only(right: 10),
                decoration: BoxDecoration(
                  borderRadius: BorderRadius.circular(5.0),
                  color: fromCSSColor("#000000"),
                ),
                child: Center(
                  child: Text(
                    "Submit",
                    style: TextStyle(
                      color: fromCSSColor("#000000"),
                      fontSize: 12
                    ),
                    textAlign: TextAlign.center,
                  ),
                )
            )
        ),
        GestureDetector(
            onTap: (){
              //...
            },
            child: Container(
              width: 65.w,
              height: 50.h,
              decoration: BoxDecoration(
                borderRadius: BorderRadius.circular(5.0),
                color: fromCSSColor("#000000"),
              ),
              child: Center(
                child: Container(
                  width: 41.w,
                  height: 41.h,
                  alignment: Alignment.center,
                  child: Image.asset('assets/images/icon_scan_white.png', fit: BoxFit.contain),
                ),
              ),
            )
        )
      ],
    )
)

【问题讨论】:

  • 我试过了。无论字体大小如何,它对我来说都显示垂直居中。我正在 Android 手机上进行测试。
  • @JigarPatel 会不会是设备的问题?我正在测试型号为 SM-T295C 的三星 Galaxy Tab A(8.0",2019)
  • 似乎不太可能。我的是三星 Galaxy S7。
  • @JigarPatel 设备区域设置如何?
  • 不确定。

标签: flutter flutter-layout


【解决方案1】:

尝试在 TextField 中设置 textalign.center,如下所示

TextField(
   textAlign : TextAlign.center,
),

你的屏幕看起来像这样-

【讨论】:

  • 它将使文本水平居中但不垂直居中。
  • 你想要什么可以上传图片一样
  • 喜欢图片'在我添加“fontSize: 12”之前'和图片'在我添加“fontSize: 12”'之后字体大小
【解决方案2】:

试试contentPadding 里面的装饰。并且不需要在Expanded Container 上使用padding

import 'package:flutter/material.dart';

class CounterList extends StatefulWidget {
  @override
  _CounterListState createState() => _CounterListState();
}

class _CounterListState extends State<CounterList> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        backgroundColor: Colors.red,
        body: Padding(
          padding: const EdgeInsets.all(8.0),
          child: ListView(
            children: [
              CustomField(
                fontSize: 17,
              ),
              SizedBox(
                height: 22,
              ),
              CustomField(
                fontSize: 37,
              ),
              SizedBox(
                height: 20,
              ),
              CustomField(
                fontSize: 11,
              ),
              SizedBox(
                height: 20,
              ),
              CustomField(
                fontSize: 22,
              ),
            ],
          ),
        ));
  }
}

class CustomField extends StatelessWidget {
  final double fontSize;
  CustomField({
    Key? key,
    required this.fontSize,
  }) : super(key: key);
  @override
  Widget build(BuildContext context) {
    return Container(
        margin: EdgeInsets.only(left: 70, right: 70),
        height: 50,
        child: Row(
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            Expanded(
              child: Container(
                alignment: Alignment.center,
                decoration: BoxDecoration(
                  borderRadius: BorderRadius.only(
                      topLeft: Radius.circular(5.0),
                      bottomLeft: Radius.circular(5.0)),
                  color: Colors.white,
                ),
                // padding: EdgeInsets.only(left: 15.w, right: 15.w),

                child: TextField(
                  maxLines: 1,
                  style: TextStyle(
                    fontSize: fontSize, //This line!!!!!!!!!
                    textBaseline: TextBaseline.alphabetic,
                  ),
                  decoration: InputDecoration(
                    contentPadding: EdgeInsets.only(left: 15, right: 15),
                    border: InputBorder.none,
                    hintText: "search",
                    // hintText: configNotifier.translationObject["SearchOrder"],
                    hintStyle: TextStyle(
                      // color: fromCSSColor("#808080"),
                      color: Colors.grey,
                      textBaseline: TextBaseline.alphabetic,
                    ),
                  ),
                ),
              ),
            ),
            GestureDetector(
                onTap: () {
                  //...
                },
                child: Container(
                    width: 100,
                    height: 50,
                    margin: EdgeInsets.only(right: 10),
                    decoration: BoxDecoration(
                      borderRadius: BorderRadius.circular(5.0),
                      // color: fromCSSColor("#000000"),
                      color: Colors.black,
                    ),
                    child: Center(
                      child: Text(
                        "Submit",
                        style: TextStyle(
                          // color: fromCSSColor("#000000"),
                          color: Colors.black,
                          fontSize: 12,
                        ),
                        textAlign: TextAlign.center,
                      ),
                    ))),
            GestureDetector(
                onTap: () {
                  //...
                },
                child: Container(
                  width: 65,
                  height: 50,
                  decoration: BoxDecoration(
                    borderRadius: BorderRadius.circular(5.0),
                    // color: fromCSSColor("#000000"),
                    color: Colors.black,
                  ),
                  child: Center(
                    child: Container(
                      width: 41,
                      height: 41,
                      alignment: Alignment.center,
                      child: Image.asset(
                        // 'assets/images/icon_scan_white.png',
                        "assets/me.jpg",
                        fit: BoxFit.contain,
                      ),
                    ),
                  ),
                ))
          ],
        ));
  }
}

【讨论】:

  • 与“添加“fontSize: 12”后的结果相同”文本仍然不是垂直居中。
  • 尝试flutter clean 并重新构建/再次运行
  • 清理后你没有得到你的结果,你可以在单独的屏幕上检查这个小部件,你可以分享父小部件
  • 请查看编辑 2 @Yeasin Sheikh
猜你喜欢
  • 2019-02-19
  • 2019-11-17
  • 2020-11-25
  • 2023-04-04
  • 1970-01-01
  • 2016-12-25
  • 2018-07-27
  • 2019-07-23
  • 1970-01-01
相关资源
最近更新 更多