Flutter 旋转文本无法填充 space
Flutter rotated text not filling available space
我有一个包含一些文本的容器,当文本处于正常水平位置时,它被分成两行,因为它不能放在一行中,我的理解是:
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,
),
),
),
),
这将呈现为:
现在我正在旋转文本,使其在垂直方向呈现,其中容器有很多 space。然而它仍然分成两行,当预期的是现在它可以毫无问题地适应时。
如何解决这个问题?
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,
),
),
),
),
),
这将呈现为:
因此,首先,child 被渲染(确定高度和宽度),然后 Transform
小部件应用基于此的转换。在这种情况下,将其旋转 pi/2
.
然而,由于 child 的高度和宽度在此之前是固定的,因此旋转不会改变它。您可以尝试将文本放在 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,
),
),
),
),
),
),
试试这个
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"),
),
),
),
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,
),
),
),
),
),
),
);
}
}
您可以使用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,
),
),
),
),
),
),
);
试试这个
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,
),
),
],
),
),
),
],
),
),
],
),
我有一个包含一些文本的容器,当文本处于正常水平位置时,它被分成两行,因为它不能放在一行中,我的理解是:
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,
),
),
),
),
这将呈现为:
现在我正在旋转文本,使其在垂直方向呈现,其中容器有很多 space。然而它仍然分成两行,当预期的是现在它可以毫无问题地适应时。
如何解决这个问题?
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,
),
),
),
),
),
这将呈现为:
因此,首先,child 被渲染(确定高度和宽度),然后 Transform
小部件应用基于此的转换。在这种情况下,将其旋转 pi/2
.
然而,由于 child 的高度和宽度在此之前是固定的,因此旋转不会改变它。您可以尝试将文本放在 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,
),
),
),
),
),
),
试试这个
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"),
),
),
),
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,
),
),
),
),
),
),
);
}
}
您可以使用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,
),
),
),
),
),
),
);
试试这个
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,
),
),
],
),
),
),
],
),
),
],
),