Search code examples
flutterflutter-dependenciesflutter-animation

Scroll only when text length is greater than particular number - Flutter Marquee


I am using Marquee widget to scroll texts. I have made a lists of some texts, but some text contains only two or three words, and some text contains more words. I just want to scroll the texts which have more words, not every texts.enter image description here

Container(
                color: Colors.green,
                width: MediaQuery.of(context).size.width * 0.5,
                height: MediaQuery.of(context).size.width * 0.1,
                child: Marquee(
                    showFadingOnlyWhenScrolling: true,
                    startAfter: Duration(seconds: 5),
                    pauseAfterRound: Duration(seconds: 2),
                    fadingEdgeEndFraction: 0.3,
                    blankSpace: 100,
                    velocity: 50,
                    text: title,
                    style: TextStyle(
                        color: Colors.white,
                        fontSize: 25,
                        decoration: isDone
                            ? TextDecoration.lineThrough
                            : TextDecoration.none)),
              ),

Solution

  • first you need to know how long your String is. For that you can save your String in a variable.

    then you can get the length from the string like this:

    String myString = "abc";
    
    // this will print 3
    print(myString.length);
    
    

    so after this you can use a simple if:

    String myString = "abc";
    
    if(myString.length > 10) {
      scroll();
    }
    

    you also can do:

    String myString = "abc";
    
    ...
    
    //                  if             else
    myString.length > 10 ? Widget1(...) : Widget2(...)