首页 > 解决方案 > 当我从列表中删除项目并执行 setState((){}) 时,颜色状态正在发生变化

问题描述

我在颤动中为 ListTile 前导属性分配随机颜色,但是当我使用 setState 删除一个项目时,所有列表项的颜色都在变化,我尝试使用 ObjectKey() ,我想保持颜色状态不变

         ListTile(
              key: ObjectKey(expense),
              onTap: () {
                setState(() {
                 _allExpenses.remove(expense);
                });
              },
              leading: CircleAvatar(
                radius: 20,
                backgroundColor: Color(Random().nextInt(0xffffffff)),
                child: FindIcon(expense.type),
              ),
              title: Text("Test"),
            )

我正在使用提供者和 notifyListeners(); 这使得在每个项目删除时重建 UI

标签: flutterdartsetstate

解决方案


在您的情况下,setState每次都会重建。如果要使用s,则必须创建key一个新的包装器。ListTile

但是,还有另一种选择。在 中initState,可以创建随机颜色列表:

class MyList extends StatefulWidget {
  const MyList({ Key key }) : super(key: key);

  @override
  _MyListState createState() => _MyListState();
}

class _MyListState extends State<MyList> {
  final List<int> _allExpenses = [2, 4, 5, 6, 9, 10, 22];
  
  List<Color> _colors;
  
  @override
  void initState(){
    super.initState();
    _colors = [
      for(final _ in _allExpenses)
        Color(Random().nextInt(0xffffffff)),
    ];
  }
  
  void removeOne(int index){
    setState(() {
       _allExpenses.removeAt(index);
      _colors.removeAt(index);
    });
  }
  
  
  @override
  Widget build(BuildContext context) {
    return ListView(
      children: [
        for(int i = 0; i < _allExpenses.length; i++)
          ListTile(
            //key: ObjectKey(expense),
            onTap: () => removeOne(i),
            leading: CircleAvatar(
              radius: 20,
              backgroundColor: _colors[i],
              child: Icon(Icons.account_circle),
            ),
            title: Text("Test ${_allExpenses[i]}"),
          ),
      ]
    );
  }
}

在此处输入图像描述

这是一个很好的选择,因为您可以控制这些随机颜色所需的内存。该逻辑可以通过在s中使用扩展至ChangeNotifiers :notifyListeners()addListener()initState

@override initState(){
  ...
  myChangeNotifierList.addListener(() => setState((){}));
}

还有另一种选择,您可以委托处理随机颜色并保持每个列表图块的状态。

class MyListWithKeys extends StatefulWidget {
  const MyListWithKeys({ Key key }):  super(key: key);


  @override
  _MyListWithKeysState createState() => _MyListWithKeysState();
}

class _MyListWithKeysState extends State<MyListWithKeys> {
  final List<int> _allExpenses = [2, 4, 5, 6, 9, 10, 22];
  
  void removeOne(int expense){
    setState(() {
       _allExpenses.remove(expense);
    });
  }
  
  @override
  Widget build(BuildContext context) {
    return ListView(
      children: [
        for(final expense in _allExpenses)
          MyTile(
            key: ObjectKey(expense),
            expense: expense,
            onTap: () => removeOne(expense),
          ),
      ]
    );
  }
}

class MyTile extends StatefulWidget {
  const MyTile({
    Key key,
    @required this.expense,
    @required this.onTap,
  }) : assert(expense != null),
       assert(onTap != null),
       super(key: key);
  
  final int expense;
  final VoidCallback onTap;

  @override
  _MyTileState createState() => _MyTileState();
}

class _MyTileState extends State<MyTile> {
  Color _color;
  
  @override
  void initState() {
    super.initState();
    _color = Color(Random().nextInt(0xffffffff));
  }
  
  @override
  Widget build(BuildContext context) {
    return ListTile(
      onTap: widget.onTap,
      leading: CircleAvatar(
        radius: 20,
        backgroundColor: _color,
        child: Icon(Icons.account_circle),
      ),
      title: Text('Test ${widget.expense}'),
    );
  }
}

推荐阅读