TextSpan不显示返回文本抖动?

时间:2019-10-18 04:13:25

标签: flutter

我正在开发Flutter应用程序,其中使用了TextSpan小部件来显示文本。但是,当我从方法TextSpan中删除文本时,不会显示此文本。

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”

2 个答案:

答案 0 :(得分:1)

我试图重新创建您的案子。

代替:

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

应该是:

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

我也不知道comments[index].createdAt代表什么,所以我试图直接通过datetime方法的硬编码测试formatDateformatTime分别调用并在color中提供TextSpan属性,这有助于在屏幕上正确显示文本。更新了以下示例工作代码:

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

结果:

enter image description here

希望这会有所帮助。

答案 1 :(得分:0)

尝试一下:

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

//Replace DateTime.now() with your data