Camila Matioli
Camila Matioli

Reputation: 131

How to use quotation marks in a text string in Flutter?

How do I write quotation marks in a text inside do:

ex: child: Text ("One fine day I woke up from" Bad Mood "was Monday"),

Container(      
    color: Colors.white,
    height: 50,
    width: 390,
    margin: EdgeInsets.only(left: 15.0, top: 15.0, right: 15.0, bottom: 5.0 ),
    alignment: Alignment.topRight,
    child: Text ("2 لا تتطلب النية قول أي شيء بصوت عال، ولكن التركيز على عبارة "بسم الله".", textDirection: TextDirection.rtl,
      style: TextStyle(
        decoration: TextDecoration.none,
        fontSize: 25.0,
        fontFamily: 'Poppins',
        fontWeight: FontWeight.w600,
        color: Colors.black,
      ),),

  ),

Upvotes: 13

Views: 24635

Answers (4)

Heshan Sandeepa
Heshan Sandeepa

Reputation: 3688

Use 'dart_quote'

dart_quote

There two types of quotes.

  1. Quote as a Widget

    WidgetQuote(
     text: 'This is the sample text including Quotes.',
    ).quote()
    

enter image description here

  1. Quote in String

    StringQuote(
    text: 'This is the sample text including Quotes.',
    ).quote()
    

enter image description here

Upvotes: -1

lrn
lrn

Reputation: 71623

Dart strings literals have several ways to include quotes in a string.

There are three ways Dart string literals can vary:

  • They can use single-quotes (') or double quotes (").
  • They can be single-line (one quote) or multi-line (three quotes).
  • They can be raw (prefixed by r) or interpolated (not prefixed by r).

All eight combinations can be used:

  • "A string ${withInterpolation}"
  • 'A string ${withInterpolation}'
  • """A string ${withInterpolation} (can contain newlines)"""
  • '''A string ${withInterpolation} (can contain newlines)'''
  • r"A raw string, $ means nothing"
  • r'A raw string, $ means nothing'
  • r"""A raw string, $ means nothing (can contain newlines)"""
  • r'''A raw string, $ means nothing (can contain newlines)'''

You cannot include the string delimiter inside the string without escaping it. Escapes use \ to ensure that the next quote character is used as string content, not a delimiter, but escapes do not work in raw strings—in a raw string, a \ is just a \ character, like a $ is just a character and not an interpolation starter.

So, to include a " in a string, you can either use an escape, or use a delimiter other than ". This rules out only a r" ... " string which uses " as delimiter and cannot use escapes.

  • "a \"quoted\" text."
  • 'a "quoted" text.'
  • """a "quoted" text.""".
  • and all the other strings work too, without escapes, except r" ... ".

This gives you a lot of flexibility in writing strings. Using a raw "multi-line" string for its tripple-quote, even if it doesn't actually span multiple lines, allows you to have both quote characters and dollar/backslash in a string, like:

var complexString = r"""I don't know if "amazing" is what I'd call this $-price""";

You are only stuck if you need a raw string that contains both """ and ''' sequences. In that case, Dart combines two adjacent string literals, even with different quotes:

var veryComplexString = 
   r"""Dart strings start with ", r", ',  r', """ 
   r'""", r""", ' 
   r"''', and r'''";

Finally, you can also use interpolations to include strings of other kinds:

var someStrings = "This string contains a ${'"'} quote character";

Upvotes: 22

Amit Prajapati
Amit Prajapati

Reputation: 14315

child:  Text ('One fine day I woke up from" Bad Mood "was Monday'),

Reference : Difference between single and double quotes in Flutter/Dart

Upvotes: 7

FloRaptor
FloRaptor

Reputation: 107

Use

child: Text ("One fine day I woke up from\" Bad Mood \"was Monday"),

Upvotes: 4

Related Questions