2

我有一个包含一些文本的容器,当文本处于正常水平位置时,它被分成两行,因为它不适合单行,我理解:

Container(
                    width: 30,
                    height: 250,
                    color: Color.fromRGBO(254, 242, 0, 1),
                    child: Center(
                      child: Text(
                        "dB per H",
                        style: TextStyle(
                          fontSize: 12,
                          color: Colors.black,
                        ),
                      ),
                    ),
                  ),

这将呈现为:

在此处输入图像描述

现在我正在旋转文本,使其在垂直方向呈现,容器有足够的空间。然而,它仍然被分成两行,当预期是现在它可以正常安装时。

如何解决这个问题?

Container(
                    width: 30,
                    height: 250,
                    color: Color.fromRGBO(254, 242, 0, 1),
                    child: Center(
                      child: Transform.rotate(
                        angle: -pi / 2,
                        child: Text(
                          "dB per H",
                          style: TextStyle(
                            fontSize: 12,
                            color: Colors.black,
                          ),
                        ),
                      ),
                    ),
                  ),

这将呈现为:

在此处输入图像描述

4

5 回答 5

3
import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(title: Text('Title')),
        body: Container(
          width: 30,
          height: 250,
          color: Color.fromRGBO(254, 242, 0, 1),
          child: Center(
            child: RotatedBox(
              quarterTurns: 3,
              child: Text(
                "dB per H",
                textAlign: TextAlign.center,
                style: TextStyle(
                  fontSize: 12,
                  color: Colors.black,
                ),
              ),
            ),
          ),
        ),
      ),
    );
  }
}

在此处输入图像描述

于 2019-10-15T10:36:50.283 回答
1

尝试这个

new RotationTransition(
  turns: new AlwaysStoppedAnimation(90 / 360),
  child: Container(
    width: 250,
    height: 60,
    color: Color.fromRGBO(254, 242, 0, 1),
    child: new Center(
      child: new Text("Lorem ipsum"),
    ),
  ),
),
于 2019-10-15T10:36:01.163 回答
0

所以,首先,孩子被渲染(高度和宽度确定),然后Transform小部件基于它应用转换。在这种情况下,将其旋转pi/2
然而,由于孩子的高度和宽度在此之前是固定的,因此旋转不会改变这一点。您可以尝试将文本放置在 flex 小部件或具有固定高度的容器中以查看所需的输出。这是容器方法的示例:

Container(
              width: 30,
              height: 250,
              color: Color.fromRGBO(254, 242, 0, 1),
              child: Center(
                child: Transform.rotate(
                  angle: -pi / 2,
                  child: Container(
                    height: 50,
                    child: Text(
                      "dB per H",
                      style: TextStyle(
                        fontSize: 12,
                        color: Colors.black,
                      ),
                    ),
                  ),
                ),
              ),
            ),
于 2019-10-15T10:34:13.660 回答
0

您可以使用height: double.infinity;

在此处输入图像描述

return MaterialApp(
      home: Scaffold(
        appBar: AppBar(title: Text('Title')),
        body: Container(
          width: 30,
          height: double.infinity,
          color: Color.fromRGBO(254, 242, 0, 1),
          child: Center(
            child: RotatedBox(
              quarterTurns: 3,
              child: Text(
                "dB per H",
                textAlign: TextAlign.center,
                style: TextStyle(
                  fontSize: 12,
                  color: Colors.black,
                ),
              ),
            ),
          ),
        ),
      ),
    );
于 2019-10-15T10:41:31.233 回答
0

尝试这个

Column(
    children: <Widget>[
      Container(
        height: 250,
        child: Row(
          children: <Widget>[
            Transform.rotate(
              angle: -pi / 2,
              child: Container(
                width: 250,
                color: Color.fromRGBO(254, 242, 0, 1),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: <Widget>[
                    Text(
                      "dB per H",
                      style: TextStyle(
                        fontSize: 12,
                        color: Colors.black,
                      ),
                    ),
                  ],
                ),
              ),
            ),
          ],
        ),
      ),
    ],
  ),
于 2019-10-15T10:45:22.850 回答