微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何像 instagram Flutter 一样添加评论?

如何解决如何像 instagram Flutter 一样添加评论?

我不知道如何创建状态 addComment 并返回 Widget 去我按下按钮

enter image description here

我不知道如何用 val in 返回 ListTile

List<CommentDetail> comments = [
    CommentDetail(
        otherUsername: "b-akused02",otherUserProfileImageUrl: Utils.getRandomImageUrl(),comment: "Nice Nice Nice Nice Nice Nice",date: "3 days"),];

这是我的失败代码

    class CommentsPage extends StatefulWidget {
  @override
  _CommentsPageState createState() => _CommentsPageState();
}

class CommentDetail {
  String comment;
  String otherUsername;
  String otherUserProfileImageUrl;
  String date;
  CommentDetail(
      {@required this.comment,@required this.otherUsername,@required this.otherUserProfileImageUrl,@required this.date});
}

class _CommentsPageState extends State<CommentsPage> {
  List<CommentDetail> comments = [
    CommentDetail(
        otherUsername: "b-akused02",];
  var txt = TextEditingController();

  void _addComment(String val) {
    setState(() {
      comments.add(val);
    });
  }

  Widget _buildCommentList() {
    return ListView.builder(itemBuilder: (context,index) {
      if (index < comments.length) {
        return _buildCommentItem(comments[index]);
      }
    });
  }

  Widget _buildCommentItem({
    @required String comment,@required String otherUsername,@required String otherUserProfileImageUrl,@required String date,}) {
    return ListTile(//comment Box inside),

解决方法

你快到了
您在 _buildCommentItem 函数中传递的是一个 CommentDetail 对象。 此外,在收到 CommentDetail 对象后,您只需像往常一样填充 ListTile。

 Widget _buildCommentList() {
    return ListView.builder(itemBuilder: (context,index) {
      if (index < comments.length) {
        return _buildCommentItem(commentDetail: comments[index]);
      }
    });
  }

  Widget _buildCommentItem({
    @required CommentDetail commentDetail
  }) {
    return ListTile(
        title: Text(commentDetail.comment),);
  }

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。