Gursewak Singh
Gursewak Singh

Reputation: 1968

TextSpan not displaying return text flutter?

I'm working on flutter app in which I used TextSpan widget for displaying the text. But when I returend the text from method TextSpan not display this text.

RichText(
 text: TextSpan( 
   children: [
         TextSpan(text:formatDate(comments[index].createdAt) +
          " at " formatTime(comments[index].createdAt),
           )
         ]
     )
 )


String formatDate(String time) {
var parsedDate = DateTime.parse(time);

final f = new DateFormat('MMM dd, yyyy').format(parsedDate);
String format = f.toString();
return format;
// f.format(new DateTime.fromMillisecondsSinceEpoch(values[index]["start_time"]*1000));
}

String formatTime(String time) {
var parsedDate = DateTime.parse(time);

final f = new DateFormat('hh:mm a').format(parsedDate);
String format = f.toString();
return format;
// f.format(new DateTime.fromMillisecondsSinceEpoch(values[index]["start_time"]*1000));
}

createdAt: "2019-09-30T02:55:46.428Z"

Upvotes: 1

Views: 1579

Answers (2)

Darshan
Darshan

Reputation: 11659

I tried to recreate your case.

Instead of:

TextSpan(text:formatDate(comments[index].createdAt) +
          " at " formatTime(comments[index].createdAt),
           )

It should be:

TextSpan(text:formatDate(comments[index].createdAt) +
          " at " + formatTime(comments[index].createdAt),
           )

Also, I don't know what comments[index].createdAt represents, so I tried to directly pass hardcoded test date and time in formatDate and formatTime method calls respectively and provide color property in TextSpan which helped to display the text on screen properly. Updated sample working code below:

body: Center(
            child: RichText(
              text: TextSpan(
                  children: [
              TextSpan(text:formatDate("2019-09-30") +
                  " at " + formatTime("2019-09-30"), style: TextStyle(color: Colors.black)),
          ]
                  ),
              ),
            ),

Result:

enter image description here

Hope this helps.

Upvotes: 1

Anil Chauhan
Anil Chauhan

Reputation: 707

Try this :

 RichText(
     text: TextSpan( 
       children: [
          TextSpan(text: '${formatDate(DateTime.now().toString())} at ${formatDate(DateTime.now().toString())}')   
        ]
     ),
 ),

//Replace DateTime.now() with your data

Upvotes: 0

Related Questions