当文本容器大小随字体大小减小时,文本不设置在中心.如何解决这个问题是在集装箱尺寸以下进行的.

  Container(
              height: height,
              width: width,
              alignment: Alignment.center,
              decoration: _isSelected
                  ? BoxDecoration(
                color: Colors.transparent,
                border: Border.all(
                  width: 2,
                  color: Colors.black,
                ),
              )
                  : null,

              child: Align(
                alignment: Alignment.center,
                child: Text(
                  _displayText.isEmpty ? 'Enter Text' : _displayText,
                  textAlign: TextAlign.center,
                  overflow: TextOverflow.visible,
                  maxLines: 1,
                  style: TextStyle(
                    color: Colors.black,
                    fontSize: fontSize,
                  ),
                ),
              ),
            ),

this is image of output

推荐答案

使用FittedBox小部件自动zoom 文本以适应容器的大小,同时保持文本居中.

Container(
  height: height,
  width: width,
  alignment: Alignment.center,
  decoration: _isSelected
      ? BoxDecoration(
          color: Colors.transparent,
          border: Border.all(
            width: 2,
            color: Colors.black,
          ),
        )
      : null,
  child: FittedBox(
    fit: BoxFit.scaleDown, // Scale down the text to fit within the container
    alignment: Alignment.center,
    child: Text(
      _displayText.isEmpty ? 'Enter Text' : _displayText,
      textAlign: TextAlign.center,
      style: TextStyle(
        color: Colors.black,
        fontSize: fontSize,
      ),
    ),
  ),
),

Flutter相关问答推荐

当仅点击一行时,所有行都被选中

更改BottomNavigationBar onTap?的大小

为什么当我使用Riverpod更新状态时,列表会被刷新?

GitHub Actions工作流secret中的特殊字符未被保留

在Flutter 中创建自定义形状

如何在巡逻测试期间重新启动dart 侧应用程序状态?

如何在 Flutter 中设计像 zomato 应用程序一样的搜索字段?

了解多Provider 和流

构建上下文不能跨异步间隙使用

Flutter - Stripe:类型List 不是类型Map 的子类型? method_channel_stripe.dart 中的错误:263

在使用 Android 12+ 的真实 Android 设备上,SingleChildScrollView 中最初位于视口之外的无响应 Web 视图

在 flutter 中获取最喜欢的文档

如何删除或隐藏覆盖

Flutter/Dart - 从外部类更新状态

如何从 Firebase `get()` 获取 `DocumentReference`

为什么 orientation == Orientation.portrait 总是正确的,尽管我的设备已经处于横向

Flutter 将方法应用于静态变量

带图像封面的 ElevatedButton

我已经删除了我的 .android 密钥库,但我没有使用它,如何生成一个新的?

如何在Flutter 的 initState 中初始化 Firestore 查询