首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >TextSpan不显示返回文本颤振?

TextSpan不显示返回文本颤振?
EN

Stack Overflow用户
提问于 2019-10-18 04:13:25
回答 2查看 1.4K关注 0票数 1

我正在开发颤振应用程序,在这个应用程序中我使用TextSpan小部件来显示文本。但是,当我从方法TextSpan返回文本时,不会显示这个文本。

代码语言:javascript
复制
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“

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-10-18 07:55:23

我试着重建你的案子。

而不是:

代码语言:javascript
复制
TextSpan(text:formatDate(comments[index].createdAt) +
          " at " formatTime(comments[index].createdAt),
           )

它应该是:

代码语言:javascript
复制
TextSpan(text:formatDate(comments[index].createdAt) +
          " at " + formatTime(comments[index].createdAt),
           )

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

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

结果:

希望这能有所帮助。

票数 1
EN

Stack Overflow用户

发布于 2019-10-18 04:28:15

试试这个:

代码语言:javascript
复制
 RichText(
     text: TextSpan( 
       children: [
          TextSpan(text: '${formatDate(DateTime.now().toString())} at ${formatDate(DateTime.now().toString())}')   
        ]
     ),
 ),

//Replace DateTime.now() with your data
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58443859

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档