Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to read a Text widget's font size that has been set by a FittedBox?

Tags:

flutter

dart

I am looking to base one Text widget's font size off of another. If I put the Text widgets in a FittedBox and fill to container, the Text widgets become the same size but the fonts are different due to string length. Is there any way I can get the font size of a Text widget after it has been stretched or shrunk by a fitted box and then setState all the other font sizes I need to be the same? The farthest I've come is something like:

  double _getFontSize(double w) {
    Container c = Container(child: FittedBox(fit: BoxFit.fill,child: Text('What is my font size?',),), width: w,);
    FittedBox f = c.child as FittedBox;
    Text t = f.child as Text;
    return t.style!.fontSize!;
  } //My thinking was that the FittedBox did it's sizing before drawing but that is not the case.

  @override
  Widget build(BuildContext context) {
    var width = MediaQuery.of(context).size.width;
    var fontSize = _getFontSize(width);

    return yadayadayada;
  }

Let's say I have a widget layout that looks something like this:

class _Foo extends State<Bar> {
  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        Expanded(
          child: Row(
            children: [
              Expanded(
                child: Column(
                  children: [
                    Text('Short'), //--------------I WANT THIS GUY TO HAVE THE SAME FONT SIZE AS THE GUY BELOW
                  ],
                ),
              ),
              Expanded(child: Column()),
            ],
          ),
        ),
        Expanded(
          child: Row(
            children: [
              Expanded(child: Column()),
              Expanded(
                child: Column(
                  children: [
                    FittedBox(fit: BoxFit.fill,child: Text('Much Longer'),), //I WANT TO GET THIS GUY'S FONT SIZE
                  ],
                ),
              ),
            ],
          ),
        ),
      ],
    )
  }
}
like image 664
Wesley DaBes Avatar asked Dec 20 '25 10:12

Wesley DaBes


1 Answers

I can give you some resources will help you achieve your use case:
1-auto_size_text is amazing package and has group text feature may solve your use case
2- code_snippet to measure text size
3- measure size may help
4- LayoutBuilder may help you too

like image 134
Hosam Hasan Avatar answered Dec 23 '25 02:12

Hosam Hasan