kibudude
kibudude

Reputation: 79

Auto scale text inside a grid in flutter

I am trying to create a table in flutter where the texts auto scale to fit within the cells, but all the cells will have same text size while maximizing the the font size. I tried FittedBox but it wont work that way. Any ideas how to proceed?

class MyTable extends StatelessWidget {

  @override
  Widget build(BuildContext context) {
    return Container(
      child: Column(
        children: [
          Expanded(
            child: Row(
              children: [
                Expanded(child: Center(child: Text("R1C1: Some text1"))),
                Expanded(child: Center(child: Text("R1C2: Some text2"))),
                Expanded(child: Center(child: Text("R1C3: Some text3"))),
              ],
            ),
          ),
          Expanded(
            child: Row(
              children: [
                Expanded(child: Center(child: Text("R2C1: Some text4"))),
                Expanded(child: Center(child: Text("R2C2: Some text5"))),
                Expanded(child: Center(child: Text("R2C3: Some text6"))),
              ],
            ),
          ),
          Expanded(
            child: Row(
              children: [
                Expanded(child: Center(child: Text("R3C1: Some text7"))),
                Expanded(child: Center(child: Text("R3C2: Some text8"))),
                Expanded(child: Center(child: Text("R3C3: Some text9"))),
              ],
            ),
          ),
        ],
      ),
    );
  }
}

Upvotes: 0

Views: 509

Answers (1)

kibudude
kibudude

Reputation: 79

I was able solve this using auto_size_text package. I used group to synchronize sizes of all the cells.

class MyTable extends StatelessWidget {
  final AutoSizeGroup myGroup = AutoSizeGroup();

  @override
  Widget build(BuildContext context) {
    return Container(
      child: Column(
        children: [
          Expanded(
            child: Row(
              children: [
                Expanded(
                  child: Center(
                    child: AutoSizeText(
                      "R1C1: Some text1",
                      style: TextStyle(fontSize: 50),
                      group: myGroup,
                    ),
                  ),
                ),
                Expanded(
                  child: Center(
                    child: AutoSizeText(
                      "R1C2: Some text2",
                      style: TextStyle(fontSize: 50),
                      group: myGroup,
                    ),
                  ),
                ),
...

Upvotes: 1

Related Questions