首页 > 解决方案 > 如何将 localTime 转换为正常时间?

问题描述

有截图在我的应用程序中,我创建了一个聊天。当有人发送消息时,也有一段时间用户发送了他的消息。但我不希望整个时间都在显示,而只是像(14:36)这样的正常时间。有谁知道怎么做?ndnvknfkvjndkjnvfkjdnfvkjndkjvfnkjdfnv mdlkvbldfkbmkldfklbvmdflkbmvlkdf

class MessageTile extends StatelessWidget {
  final String message;
  final bool sendByMe;
  final int time;

  MessageTile({@required this.message, @required this.sendByMe, @required this.time});


  @override
  Widget build(BuildContext context) {
    return Column(
      crossAxisAlignment: sendByMe ? CrossAxisAlignment.end : CrossAxisAlignment.start,
      children: <Widget>[
        Container(
          padding: EdgeInsets.only(
              top: 3,
              bottom: 3,
              left: sendByMe ? 0 : 24,
              right: sendByMe ? 24 : 0),
          alignment: sendByMe ? Alignment.centerRight : Alignment.centerLeft,
          child: Container(
            margin: sendByMe
                ? EdgeInsets.only(left: 30)
                : EdgeInsets.only(right: 30),
            padding: EdgeInsets.only(
                top: 17, bottom: 17, left: 20, right: 20),
            decoration: BoxDecoration(
                borderRadius: sendByMe ? BorderRadius.only(
                    topLeft: Radius.circular(9),
                    topRight: Radius.circular(9),
                    bottomLeft: Radius.circular(9),
                ) :
                BorderRadius.only(
                    topLeft: Radius.circular(9),
                    topRight: Radius.circular(9),
                    bottomRight: Radius.circular(9)),
               color: sendByMe ? Colors.blue : Colors.white
            ),
            child: Column(
              children: [
                Text(message,
                        textAlign: TextAlign.start,
                        style: TextStyle(
                        color: Colors.black,
                        fontWeight: FontWeight.bold,
                        fontFamily: 'Orbitron',
                        fontSize: 12.5,),),
                Text( sendByMe ? DateTime.now().toLocal().toString() :  DateTime.now().toLocal().toString(),style:
                TextStyle(color: Colors.black, fontWeight: FontWeight.bold, fontFamily: 'Orbitron', fontSize: 7.0,) ,
                ),
              ],
            ),
            ),
        ),
      ],
    );
  }
}
addMessage() {
    if (messageEditingController.text.isNotEmpty) {
      Map<String, dynamic> chatMessageMap = {
        "sendBy": Constants.myName,
        "message": messageEditingController.text,
        'time': DateTime.now().millisecondsSinceEpoch,
      };

      DatabaseService().addMessage(widget.chatRoomId, chatMessageMap);

      setState(() {
        messageEditingController.text = "";
      });
    }
  }


标签: firebaseflutterdatetimetime

解决方案


Use the intl package (https://pub.dev/packages/intl) to format the DateTime to the format you want.


推荐阅读