颤动的旋转文本未填充可用空间

时间:2019-10-15 09:31:46

标签: flutter dart flutter-container

我有一个容纳一些文本的容器,当文本处于正常水平位置时,由于不适合一行而被分成两行,据我了解:

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,
                        ),
                      ),
                    ),
                  ),

这将呈现为:

enter image description here

现在,我正在旋转文本,以便在容器具有足够空间的垂直方向上进行渲染。但是,它仍然分为两行,按预期现在它可以毫无问题地容纳了。

该如何解决?

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,
                          ),
                        ),
                      ),
                    ),
                  ),

这将呈现为:

enter image description here

5 个答案:

答案 0 :(得分: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"),
    ),
  ),
),

答案 1 :(得分:1)

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,
                ),
              ),
            ),
          ),
        ),
      ),
    );
  }
}

enter image description here

答案 2 :(得分: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,
                      ),
                    ),
                  ),
                ),
              ),
            ),

答案 3 :(得分:0)

您可以使用height: double.infinity;

enter image description here

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,
                ),
              ),
            ),
          ),
        ),
      ),
    );

答案 4 :(得分: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,
                      ),
                    ),
                  ],
                ),
              ),
            ),
          ],
        ),
      ),
    ],
  ),