【问题标题】:FittedBox and Text whitespace issueFittedBox 和文本空白问题
【发布时间】:2019-04-03 14:28:57
【问题描述】:

我正在尝试使所有文本(长度不同)在一个固定宽度的框中适合,并且我希望它们在应用 FittedBox 时看起来与最长单词的大小相同。所以我正在做的就是用空白填充单词的其余部分,以使长度与最长的单词相匹配。但这并不像您在下图中看到的那样:

代码如下:

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      routes: {
        '/': (context) => HomePage(),
      },
    );
  }
}

class HomePage extends StatelessWidget {
  final String padding = " " * 5;
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Home'),
      ),
      body: Center(
        child: Container(
          color: Colors.red,
          height: 100.0,
          width: 100.0,
          padding: const EdgeInsets.all(8.0),
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              Center(
                child: FittedBox(
                  fit: BoxFit.scaleDown,
                  child: Text(
                    "${padding}Demo$padding",
                    textAlign: TextAlign.center,
                    style: TextStyle(color: Colors.black),
                  ),
                ),
              ),
              Icon(Icons.mic, size: 24.0),
            ],
          ),
        ),
      ),
    );
  }
}

你知道这里有什么问题吗?

【问题讨论】:

  • 你好安德烈!对不起,我忘了更新这个。是的,问题是使用字符代码 String.fromCharCode(160) 而不是 ' ' 解决的。
  • 那我觉得还是把答案写出来标记为正确比较好

标签: text flutter fitted-box


【解决方案1】:

这真的很奇怪,但是如果您删除对齐或将其设置为启动,它似乎可以正常工作。 (是什么导致了这个错误 - 我仍然不知道)

child: Text('${padding}Demo$padding',
  style: TextStyle(color: Colors.black, decoration: TextDecoration.underline),)

我已经添加了装饰来查看这些空间

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-10-02
    • 1970-01-01
    • 1970-01-01
    • 2010-11-21
    • 1970-01-01
    • 1970-01-01
    • 2019-04-25
    • 2010-11-03
    相关资源
    最近更新 更多