Line Breaks in Long Text Flutter

前端 未结 3 1883
伪装坚强ぢ
伪装坚强ぢ 2021-02-07 05:01

How can I create line breaks within a long Text widget?

For example, I am creating a biographical page about myself. And I have three paragraphs I want to be able to dis

3条回答
  •  傲寒
    傲寒 (楼主)
    2021-02-07 05:44

    I came here wanting to divide a long string in code over multiple lines. Starting with this:

    final longString = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.';
    

    Multi-line without new lines

    You can make it all visible in your code editor by doing this:

    final longString = 'Lorem ipsum dolor sit amet, consectetur adipiscing '
        'elit, sed do eiusmod tempor incididunt ut labore et dolore magna '
        'aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco '
        'laboris nisi ut aliquip ex ea commodo consequat. ';
    

    or this:

    final longString = 'Lorem ipsum dolor sit amet, consectetur adipiscing ' +
        'elit, sed do eiusmod tempor incididunt ut labore et dolore magna ' +
        'aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco ' +
        'laboris nisi ut aliquip ex ea commodo consequat.';
    

    without affecting the content of the string. That is, no newline characters will be added.

    Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.

    Multi-line with new lines

    If you actually do want the line breaks to stay, then you can use triple quotes.

    final longString = '''
    Lorem ipsum dolor sit amet, 
    consectetur adipiscing elit, 
    sed do eiusmod tempor incididunt 
    ut labore et dolore magna aliqua. 
    
    Ut enim ad minim veniam, 
    quis nostrud exercitation ullamco 
    laboris nisi ut aliquip ex ea 
    commodo consequat.
    ''';
    

    which produces:

    Lorem ipsum dolor sit amet,
    consectetur adipiscing elit,
    sed do eiusmod tempor incididunt
    ut labore et dolore magna aliqua.

    Ut enim ad minim veniam,
    quis nostrud exercitation ullamco
    laboris nisi ut aliquip ex ea
    commodo consequat.

    Dividing paragraphs

    As has already been mentioned, just add two \n newline characters to every paragraph to separate them. Or a \t tab at the beginning and a newline at the end.

    final paragraph1 = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.';
    final paragraph2 = 'Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.';
    final text = paragraph1 + '\n\n' + paragraph2;
    

    Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.

    Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.

    If you are doing a lot of string concatenation, then consider using StringBuffer.

    See also

    • Meaning of Paragraph in Flutter classes

提交回复
热议问题