codeKiller
codeKiller

Reputation: 5749

Flutter rotated text not filling available space

I have a container holding some text and when the text is normal horizontal position it is split into 2 lines as it does not fit in a single line, which I understand:

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

This will be rendered as:

enter image description here

Now I am rotating the text so it is rendered in vertical direction, where the container has plenty of space. However it is still split in 2 lines, when the expected is that now it would fit with no problem.

How to fix this?

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

This will be rendered as:

enter image description here

Upvotes: 4

Views: 2391

Answers (5)

Try this

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

Upvotes: 0

Ahmad Aghazadeh
Ahmad Aghazadeh

Reputation: 17131

You can use 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,
                ),
              ),
            ),
          ),
        ),
      ),
    );

Upvotes: 0

user10539074
user10539074

Reputation:

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

Upvotes: 6

aswin darma
aswin darma

Reputation: 438

try this

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

Upvotes: 1

RoyalGriffin
RoyalGriffin

Reputation: 2007

So, first, the child gets rendered(height & width determined), then the Transform widget applies the transformation based on that. In this case, rotates it by pi/2.
Since however the child's height & width was fixed before that, rotation doesn't change that. You can try placing the text either in a flex widget or in a container with a fixed height to see the desired output. Here's an example with the container approach:

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

Upvotes: 0

Related Questions