dart 按下flutter中listTile中的图标按钮后,所有图标按钮均发生变化

zfciruhq  于 2022-12-06  发布在  Flutter
关注(0)|答案(1)|浏览(176)

当我按下图标按钮时,它会改变列表平铺中所有图标按钮的颜色...我只想改变特定的图标按钮import 'package:comment_box/comment/comment. dart';导入“ Package :flutter/材料. dart”;导入“数据包:peta/api_storage/存储. dart”;

class CommentPage extends StatefulWidget {
  final String? img;
  const CommentPage(this.img, {Key? key}) : super(key: key);
  //const CommentPage({Key? key}) : super(key: key);

  @override
  State<CommentPage> createState() => CommentPageState();
}

class CommentPageState extends State<CommentPage> {
  Color _likeIcon = Colors.grey;
  //final myposts mypost;

  // bool toggle=false;
  final formKey = GlobalKey<FormState>();
  final TextEditingController commentController = TextEditingController();

  Widget commentChild(data) {
    return ListView(
      children: [
        Image(
          image: NetworkImage(widget.img.toString()),
        ),
        for (var i = 0; i < data.length; i++)
          Padding(
            padding: const EdgeInsets.fromLTRB(2.0, 8.0, 2.0, 0.0),
            child: ListTile(
              isThreeLine: true,
              //dense: true,
              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(color: Colors.white, fontWeight: FontWeight.bold),
              ),

              subtitle: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Text(
                    data[i]['message'],
                    style: TextStyle(color: Colors.white, fontSize: 13),
                  ),
                  SizedBox(
                    height: 9,
                  ),
                  Row(
                    children: [
                      Text(data[i]['date'],
                          style: TextStyle(
                            fontSize: 12,
                            color: Colors.grey,
                            fontWeight: FontWeight.bold,
                          )),
                      SizedBox(
                        width: 13,
                      ),
                      Text(
                        data[i]['likes'],
                        style: TextStyle(
                          fontSize: 12,
                          color: Colors.grey,
                          fontWeight: FontWeight.bold,
                        ),
                      ),
                      SizedBox(
                        width: 13,
                      ),
                      Text(
                        'Reply',
                        style: TextStyle(
                          fontSize: 12,
                          color: Colors.grey,
                          fontWeight: FontWeight.bold,
                        ),
                      ),
                      SizedBox(
                        width: 13,
                      ),
                      Text(
                        'Send',
                        style: TextStyle(
                          fontSize: 12,
                          color: Colors.grey,
                          fontWeight: FontWeight.bold,
                        ),
                      ),
                    ],
                  )
                ],
              ),
              `trailing: IconButton(
                color: _likeIcon,
                highlightColor: Colors.red,
                iconSize: 18,
               icon: Icon(
                  data[i]['icon']
                  //Icons.favorite,
                ),
                onPressed: () {
                  setState(() {
                    //toggle=!toggle;
                    //_likeIcon = data[i]['color'];
                    _likeIcon == Colors.grey ? _likeIcon=Colors.red : _likeIcon=Colors.grey;
                  });
                },
              ),
              // trailing: Text(data[i]['date'], style: TextStyle(fontSize: 10,color: Colors.white)),
            ),
          ),
      ],
    );
  }`

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Comment Page"),
        backgroundColor: Colors.black,
      ),
      body: Container(
        height: MediaQuery.of(context).size.height,
        width: MediaQuery.of(context).size.width,
        decoration: const BoxDecoration(
            //color: Colors.transparent,
            image: DecorationImage(
                image: AssetImage('assets/BG/PetaBG.png'),
                fit: BoxFit.cover,
                colorFilter: ColorFilter.mode(
                  Color.fromARGB(120, 0, 0, 0),
                  BlendMode.darken,
                ))),
        child: CommentBox(

          userImage: CommentBox.commentImageParser(
              imageURLorPath: "assets/images/dog1.jpg"),
          child: commentChild(postcomments),
          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': 'assets/images/dog1.jpg',
                  //'https://lh3.googleusercontent.com/a-/AOh14GjRHcaendrf6gU5fPIVd8GIl1OgblrMMvGUoCBj4g=s400',
                  'message': commentController.text,
                  'date': '1s',
                  'likes': '',
                };
                postcomments.insert(0, value);
              });
              commentController.clear();
              FocusScope.of(context).unfocus();
            } else {
              print("Not validated");
            }
          },
          formKey: formKey,
          commentController: commentController,
          backgroundColor: Colors.blue,
          textColor: Colors.white,
          sendWidget: Text(
            'Post',
            style: TextStyle(color: Colors.blue),
          ),

          // Icon(Icons.send_sharp, size: 30, color: Colors.white),
        ),
      ),
    );
  }
}
cgh8pdjw

cgh8pdjw1#

根据您在这里提到的代码,只有一个颜色变量分配给列表中的所有项;如果该变量改变,则所有项也改变。
但如果你提供完整的页面代码,那么我可以在这方面帮助更多。

相关问题