【问题标题】:how to change comment_box userImage on flutter with local img?如何使用本地图像更改评论框用户图像?
【发布时间】:2023-02-23 08:41:20
【问题描述】:

我用 comment_box 包制作了带有 flutter 的评论框,但我尝试将“userImage”更改为本地 img,但它似乎不起作用。任何人有任何解决方案来改变它吗? 这是我的代码:

Expanded(
          child: CommentBox(
            userImage: 'assets/profile_icon.png',
            child: commentChild(filedata),
            labelText: 'Write a comment...',
            withBorder: false,
            errorText: 'Comment cannot be blank',
            sendButtonMethod: () {
              if (formKey.currentState!.validate()) {
                print(commentController.text);
                setState(() {
                  addComment();
                });

                FocusScope.of(context).unfocus();
              } else {
                print("Not validated");
              }
            },
            formKey: formKey,
            commentController: commentController,
            backgroundColor: Color.fromARGB(255, 255, 255, 255),
            textColor: Color.fromARGB(255, 0, 0, 0),
            sendWidget: Icon(Icons.send_sharp, size: 30, color: Colors.grey),
          ),
        ),

【问题讨论】:

    标签: flutter dart


    【解决方案1】:

    不幸的是,无法将本地图像用于 userImage 参数。在comment_box 0.0.17包配置中,它使用Network Image来显示userImage用户界面,它只接受URL text (String userImage)。在这种情况下,您必须为您的 UI 配置一个自定义评论框,或者您可以继续为您的 UI 实现搜索另一个包。

    dependencies:
      comment_box: ^0.0.17
    

    【讨论】:

      【解决方案2】:

      我已经更新了 comment_box 包以支持本地图像和网络图像。

      包裹:comment_box

      例子

      import 'package:comment_box/comment/comment.dart';
      import 'package:flutter/material.dart';
      
      class TestMe extends StatefulWidget {
        @override
        _TestMeState createState() => _TestMeState();
      }
      
      class _TestMeState extends State<TestMe> {
        final formKey = GlobalKey<FormState>();
        final TextEditingController commentController = TextEditingController();
        List filedata = [
          {
            'name': 'Chuks Okwuenu',
            'pic': 'https://picsum.photos/300/30',
            'message': 'I love to code',
            'date': '2021-01-01 12:00:00'
          },
          {
            'name': 'Biggi Man',
            'pic': 'https://www.adeleyeayodeji.com/img/IMG_20200522_121756_834_2.jpg',
            'message': 'Very cool',
            'date': '2021-01-01 12:00:00'
          },
          {
            'name': 'Tunde Martins',
            'pic': 'assets/img/userpic.jpg',
            'message': 'Very cool',
            'date': '2021-01-01 12:00:00'
          },
          {
            'name': 'Biggi Man',
            'pic': 'https://picsum.photos/300/30',
            'message': 'Very cool',
            'date': '2021-01-01 12:00:00'
          },
        ];
      
        Widget commentChild(data) {
          return ListView(
            children: [
              for (var i = 0; i < data.length; i++)
                Padding(
                  padding: const EdgeInsets.fromLTRB(2.0, 8.0, 2.0, 0.0),
                  child: ListTile(
                    leading: GestureDetector(
                      onTap: () async {
                        // Display the image in large form.
                        print("Comment Clicked");
                      },
                      child: Container(
                        height: 50.0,
                        width: 50.0,
                        decoration: new BoxDecoration(
                            color: Colors.blue,
                            borderRadius: new BorderRadius.all(Radius.circular(50))),
                        child: CircleAvatar(
                            radius: 50,
                            backgroundImage: CommentBox.commentImageParser(
                                imageURLorPath: data[i]['pic'])),
                      ),
                    ),
                    title: Text(
                      data[i]['name'],
                      style: TextStyle(fontWeight: FontWeight.bold),
                    ),
                    subtitle: Text(data[i]['message']),
                    trailing: Text(data[i]['date'], style: TextStyle(fontSize: 10)),
                  ),
                )
            ],
          );
        }
      
        @override
        Widget build(BuildContext context) {
          return Scaffold(
            appBar: AppBar(
              title: Text("Comment Page"),
              backgroundColor: Colors.pink,
            ),
            body: Container(
              child: CommentBox(
                userImage: CommentBox.commentImageParser(
                    imageURLorPath: "assets/img/userpic.jpg"),
                child: commentChild(filedata),
                labelText: 'Write a comment...',
                errorText: 'Comment cannot be blank',
                withBorder: false,
                sendButtonMethod: () {
                  if (formKey.currentState!.validate()) {
                    print(commentController.text);
                    setState(() {
                      var value = {
                        'name': 'New User',
                        'pic':
                            'https://lh3.googleusercontent.com/a-/AOh14GjRHcaendrf6gU5fPIVd8GIl1OgblrMMvGUoCBj4g=s400',
                        'message': commentController.text,
                        'date': '2021-01-01 12:00:00'
                      };
                      filedata.insert(0, value);
                    });
                    commentController.clear();
                    FocusScope.of(context).unfocus();
                  } else {
                    print("Not validated");
                  }
                },
                formKey: formKey,
                commentController: commentController,
                backgroundColor: Colors.pink,
                textColor: Colors.white,
                sendWidget: Icon(Icons.send_sharp, size: 30, color: Colors.white),
              ),
            ),
          );
        }
      }
      
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-05-23
        • 1970-01-01
        • 2011-03-09
        • 2021-03-18
        • 1970-01-01
        • 2014-10-03
        • 2017-08-29
        • 1970-01-01
        相关资源
        最近更新 更多