Flutter 我怎样才能使这个单一的选择颤振?

Flutter 我怎样才能使这个单一的选择颤振?,flutter,dart,flutter-layout,flutter-dependencies,flutter-test,Flutter,Dart,Flutter Layout,Flutter Dependencies,Flutter Test,我有一个应用程序,它的列表视图中有一个颤振的反应按钮。我想当一个用户点击这个爱情图标时,它就会变成红色 就像这张图片,但问题是当我点击其中一个爱情图标时,所有图标都变成了红色,但我只想更改所选图标的爱情颜色 import 'package:flutter/material.dart'; void main() { runApp(MyApp()); } class MyApp extends StatelessWidget { // This widget is the root

我有一个应用程序,它的列表视图中有一个颤振的反应按钮。我想当一个用户点击这个爱情图标时,它就会变成红色

就像这张图片,但问题是当我点击其中一个爱情图标时,所有图标都变成了红色,但我只想更改所选图标的爱情颜色

import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  bool like;
  @override
  List<String> user = ['Dipto', 'Dipankar', "Sajib", 'Shanto', 'Pranto'];
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('ListView Demu'),
      ),
      body: Center(
          child: Container(
        child: ListView.builder(
          itemCount: user.length,
          itemBuilder: (context, index) {
            return Container(
              padding: EdgeInsets.all(10),
              height: 50,
              width: MediaQuery.of(context).size.width * 0.8,
              color: Colors.yellowAccent,
              child: Row(
                crossAxisAlignment: CrossAxisAlignment.center,
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  Text(
                    user[index],
                  ),
                  Positioned(
                    child: IconButton(
                      icon: _iconControl(like),
                      onPressed: () {
                        if (like == false) {
                          setState(() {
                            like = true;
                            _iconControl(like);
                          });
                        } else {
                          setState(() {
                            like = false;
                            _iconControl(like);
                          });
                        }
                      },
                    ),
                  ),
                ],
              ),
            );
          },
        ),
      )),
    );
  }

  _iconControl(bool like) {
    if (like == false) {
      return Icon(Icons.favorite_border);
    } else {
      return Icon(
        Icons.favorite,
        color: Colors.red,
      );
    }
  }
}

你能帮帮我吗。提前感谢

您可以创建一个模态类来管理列表的选择

只需创建一个模态类,并添加一个布尔变量来维护选择。那个布尔变量

示例代码

import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  bool like;
  List<Modal> userList = List<Modal>();

  @override
  void initState() {
    userList.add(Modal(name: 'Dipto', isSelected: false));
    userList.add(Modal(name: 'Dipankar', isSelected: false));
    userList.add(Modal(name: 'Sajib', isSelected: false));
    userList.add(Modal(name: 'Shanto', isSelected: false));
    userList.add(Modal(name: 'Pranto', isSelected: false));
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('ListView Demu'),
      ),
      body: Center(
          child: Container(
            child: ListView.builder(
              itemCount: userList.length,
              itemBuilder: (context, index) {
                return Container(
                  padding: EdgeInsets.all(10),
                  height: 50,
                  width: MediaQuery
                      .of(context)
                      .size
                      .width * 0.8,
                  color: Colors.yellowAccent,
                  child: Row(
                    crossAxisAlignment: CrossAxisAlignment.center,
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      Text(
                        userList[index].name,
                      ),
                      Positioned(
                        child: IconButton(
                          icon: _iconControl( userList[index].isSelected),
                          onPressed: () {
                            setState(() {
                              userList.forEach((element) {
                                element.isSelected = false;
                              });

                              userList[index].isSelected = true;
                            });
                          },
                        ),
                      ),
                    ],
                  ),
                );
              },
            ),
          )),
    );
  }

  _iconControl(bool like) {
    if (like == false) {
      return Icon(Icons.favorite_border);
    } else {
      return Icon(
        Icons.favorite,
        color: Colors.red,
      );
    }
  }
}

class Modal {
  String name;
  bool isSelected;

  Modal({this.name, this.isSelected = false});
}
导入“包装:颤振/材料.省道”;
void main(){
runApp(MyApp());
}
类MyApp扩展了无状态小部件{
//此小部件是应用程序的根。
@凌驾
小部件构建(构建上下文){
返回材料PP(
主页:MyHomePage(),
);
}
}
类MyHomePage扩展StatefulWidget{
@凌驾
_MyHomePageState createState()=>\u MyHomePageState();
}
类_MyHomePageState扩展状态{
布尔式;
List userList=List();
@凌驾
void initState(){
add(模态(名称:'Dipto',isSelected:false));
add(Modal(名称:'Dipankar',isSelected:false));
add(Modal(名称:'Sajib',isSelected:false));
add(Modal(名称:'Shanto',isSelected:false));
add(Modal(名称:'Pranto',isSelected:false));
super.initState();
}
@凌驾
小部件构建(构建上下文){
返回脚手架(
appBar:appBar(
标题:文本('ListView Demu'),
),
正文:中(
子:容器(
子项:ListView.builder(
itemCount:userList.length,
itemBuilder:(上下文,索引){
返回容器(
填充:边缘设置。全部(10),
身高:50,
宽度:MediaQuery
.of(上下文)
.尺寸
.宽度*0.8,
颜色:Colors.yellowAccent,
孩子:排(
crossAxisAlignment:crossAxisAlignment.center,
mainAxisAlignment:mainAxisAlignment.spaceBetween,
儿童:[
正文(
用户列表[索引]。名称,
),
定位(
孩子:我的钮扣(
图标:_iconControl(用户列表[index].isSelected),
已按下:(){
设置状态(){
userList.forEach((元素){
element.isSelected=false;
});
userList[index].isSelected=true;
});
},
),
),
],
),
);
},
),
)),
);
}
_iconControl(类布尔){
if(like==false){
返回图标(图标、收藏夹边框);
}否则{
返回图标(
我的最爱,
颜色:颜色,红色,
);
}
}
}
类模态{
字符串名;
布尔当选;
模态({this.name,this.isSelected=false});
}

谢谢Nilesh Rathod的回答,你的回答对我帮助很大,我只需要这样的设置状态((){if(userList[index].isSelected==true){userList[index].isSelected=false;}else{userList[index].isSelected=true;}}});@DipankarDedbnath很高兴能为您提供帮助!如果您觉得它对您有用,请随时联系。:-)
import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  bool like;
  List<Modal> userList = List<Modal>();

  @override
  void initState() {
    userList.add(Modal(name: 'Dipto', isSelected: false));
    userList.add(Modal(name: 'Dipankar', isSelected: false));
    userList.add(Modal(name: 'Sajib', isSelected: false));
    userList.add(Modal(name: 'Shanto', isSelected: false));
    userList.add(Modal(name: 'Pranto', isSelected: false));
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('ListView Demu'),
      ),
      body: Center(
          child: Container(
            child: ListView.builder(
              itemCount: userList.length,
              itemBuilder: (context, index) {
                return Container(
                  padding: EdgeInsets.all(10),
                  height: 50,
                  width: MediaQuery
                      .of(context)
                      .size
                      .width * 0.8,
                  color: Colors.yellowAccent,
                  child: Row(
                    crossAxisAlignment: CrossAxisAlignment.center,
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      Text(
                        userList[index].name,
                      ),
                      Positioned(
                        child: IconButton(
                          icon: _iconControl( userList[index].isSelected),
                          onPressed: () {
                            setState(() {
                              userList.forEach((element) {
                                element.isSelected = false;
                              });

                              userList[index].isSelected = true;
                            });
                          },
                        ),
                      ),
                    ],
                  ),
                );
              },
            ),
          )),
    );
  }

  _iconControl(bool like) {
    if (like == false) {
      return Icon(Icons.favorite_border);
    } else {
      return Icon(
        Icons.favorite,
        color: Colors.red,
      );
    }
  }
}

class Modal {
  String name;
  bool isSelected;

  Modal({this.name, this.isSelected = false});
}