Flutter добавляет время к сообщению чата

#firebase #flutter #datetime #chat #message

#firebase #flutter #дата и время #Чат #Сообщение

Вопрос:

Мне нужно создать чат в моем приложении. Проблема в том, что время, когда кто-то отправил свое сообщение, неправильно отображается на экране чата, а номер указан неправильно или длинный. Время должно выглядеть примерно так (13:36). Кто-нибудь знает, как я могу указать время под сообщением, как это делает WhatsApp? Это мой экран чата здесь

 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>[
        Text( sendByMe ? time.toString() : time.toString(),style: sendByMe ?
        TextStyle(color: Colors.blue, fontWeight: FontWeight.bold, fontFamily: 'Orbitron', fontSize: 7.0,):
        TextStyle(color: Colors.white, fontWeight: FontWeight.bold, fontFamily: 'Orbitron', fontSize: 7.0,) ,
       ),
        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: Text(message,
                    textAlign: TextAlign.start,
                    style: TextStyle(
                    color: Colors.black,
                    fontWeight: FontWeight.bold,
                    fontFamily: 'Orbitron',
                    fontSize: 9.0,),),
            ),
        ),
      ],
    );
  }
  

Комментарии:

1. Есть ли какая-то конкретная причина, по которой вы используете тип данных int для хранения ваших данных о времени вместо DateTime?

2. Нет, на самом деле просто так, чтобы сообщения сортировались по времени сверху вниз

3. Что ж, тогда было бы более уместно хранить время как DateTime или String . Вы можете сортировать их, используя их метод compareTo().

4. Нравится как? Не могли бы вы показать пример

Ответ №1:

Заменить time.toString() на DateTime.fromMillisecondsSinceEpoch(time).toString()

 child: Text( sendByMe ? DateTime.fromMillisecondsSinceEpoch(time).toString() : DateTime.fromMillisecondsSinceEpoch(time).toString(),style: sendByMe ?
      TextStyle(color: Colors.blue, fontFamily: 'Orbitron', fontSize: 9.0,):
      TextStyle(color: Colors.white, fontFamily: 'Orbitron', fontSize: 9.0,),
      ),