How to dynamically resize text in Flutter?

后端 未结 7 1665
梦如初夏
梦如初夏 2021-01-31 06:53

I retrieve a piece of text from an API. I want to allot a set amount of space to it (say a max Container with width: 300.0 and height: 100.0). Sometimes, the piece of text fits

相关标签:
7条回答
  • 2021-01-31 07:52

    Easiest thing I found was auto_size_text: ^2.1.0. As simple as importing the dependency and using AutoSizeText() instead of Text()

    import 'package:auto_size_text/auto_size_text.dart';
    import 'package:flutter/material.dart';
    
    void main() {
      runApp(App());
    }
    
    class App extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          home: Scaffold(
            body: Center(
              child: SizedBox(
                width: 200.0,
                height: 140.0,
                child: AutoSizeText(
                  'This string will be automatically resized to fit in two lines.',
                  style: TextStyle(fontSize: 30.0),
                  maxLines: 2,
                ),
              ),
            ),
          ),
        );
      }
    }
    
    0 讨论(0)
提交回复
热议问题