How to check if Flutter Text widget was overflowed

前端 未结 2 1313
失恋的感觉
失恋的感觉 2020-12-11 02:47

I have a Text widget which can be truncated if it exceeds a certain size:

ConstrainedBox(
  constraints: BoxConstr         


        
相关标签:
2条回答
  • 2020-12-11 03:03

    You can use a flutter plug-in auto_size_text at pub.dev. When the text is get overflowed, you can set some widget to be appeared.

    int maxLines = 3;
    String caption = 'Your caption is here';
    return AutoSizeText(
        caption,
        maxLines: maxLines,
        style: TextStyle(fontSize: 20),
        minFontSize: 15,
        overflowReplacement: Column( // This widget will be replaced. 
        crossAxisAlignment: CrossAxisAlignment.start,
        children: <Widget>[
          Text(
            caption,
            maxLines: maxLines,
            overflow: TextOverflow.ellipsis,
          ),
          Text(
            "Show more",
            style: TextStyle(color: PrimaryColor.kGrey),
          )
        ],
      ),
    );
    
    0 讨论(0)
  • 2020-12-11 03:11

    I found a way to do it. Full code below, but in short:

    1. Use a LayoutBuilder to determine how much space we have.
    2. Use a TextPainter to simulate the render of the text within the space.

    Here's the full demo app:

    import 'package:flutter/material.dart';
    
    void main() => runApp(MyApp());
    
    class MyApp extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          title: 'Text Overflow Demo',
          theme: ThemeData(
            primarySwatch: Colors.blue,
          ),
          home: Scaffold(
            appBar: AppBar(title: Text("DEMO")),
            body: TextOverflowDemo(),
          ),
        );
      }
    }
    
    class TextOverflowDemo extends StatefulWidget {
      @override
      _EditorState createState() => _EditorState();
    }
    
    class _EditorState extends State<TextOverflowDemo> {
      var controller = TextEditingController();
    
      @override
      void initState() {
        controller.addListener(() {
          setState(() {
            mytext = controller.text;
          });
        });
        controller.text = "This is a long overflowing text!!!";
        super.initState();
      }
    
      @override
      void dispose() {
        controller.dispose();
        super.dispose();
      }
    
      String mytext = "";
    
      @override
      Widget build(BuildContext context) {
        int maxLines = 1;
        double fontSize = 30.0;
    
        return Padding(
          padding: const EdgeInsets.all(12.0),
          child: Column(
            children: <Widget>[
              LayoutBuilder(builder: (context, size) {
                // Build the textspan
                var span = TextSpan(
                  text: mytext,
                  style: TextStyle(fontSize: fontSize),
                );
    
                // Use a textpainter to determine if it will exceed max lines
                var tp = TextPainter(
                  maxLines: maxLines,
                  textAlign: TextAlign.left,
                  textDirection: TextDirection.ltr,
                  text: span,
                );
    
                // trigger it to layout
                tp.layout(maxWidth: size.maxWidth);
    
                // whether the text overflowed or not
                var exceeded = tp.didExceedMaxLines;
    
                return Column(children: <Widget>[
                  Text.rich(
                    span,
                    overflow: TextOverflow.ellipsis,
                    maxLines: maxLines,
                  ),
                  Text(exceeded ? "Overflowed!" : "Not overflowed yet.")
                ]);
              }),
              TextField(
                controller: controller,
              ),
            ],
          ),
        );
      }
    }
    
    0 讨论(0)
提交回复
热议问题