【问题标题】:Flutter changing Icon onTap in my animated List在我的动画列表中颤动改变图标 onTap
【发布时间】:2021-04-28 09:41:17
【问题描述】:

点击列表项后,我正在尝试更改图标。我已经尝试了不同的方法:我尝试了 onTap 方法,但图标就是不想改变。我对颤动很陌生,我很想为我的问题找到一些帮助:)。这是我的代码。

我已经搜索过解决方案,但在我的项目中没有得到解决

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      title: 'To-Do List',
      theme: ThemeData(
        primaryColor: Colors.white,
        brightness: Brightness.dark,
      ),
      home: Scaffold(
        appBar: AppBar(title: Text('To-Do List'),
          backgroundColor: Colors.amber,
        ),
        body: BodyLayout(),
      ),
    );
  }
}

class BodyLayout extends StatefulWidget {
  @override
  BodyLayoutState createState() {
    return new BodyLayoutState();
  }
}

class BodyLayoutState extends State<BodyLayout> {

  // The GlobalKey keeps track of the visible state of the list items
  // while they are being animated.
  final GlobalKey<AnimatedListState> _listKey = GlobalKey();

  // backing data
  List<String> _data = [];
  final _isdone = Set<String>();
 // bool selected = false;
  List<bool> selected = new List<bool>();
  Icon notdone = Icon(Icons.check_box_outline_blank);
  Icon done = Icon(Icons.check_box);

  TextEditingController todoController = TextEditingController();



  @override
  Widget build(BuildContext context) {
    return Column(
      children: <Widget>[
        SizedBox(
          height: 445,
          child: AnimatedList(
            // Give the Animated list the global key
            key: _listKey,
            initialItemCount: _data.length,
            // Similar to ListView itemBuilder, but AnimatedList has
            // an additional animation parameter.
            itemBuilder: (context, index, animation) {
              // Breaking the row widget out as a method so that we can
              // share it with the _removeSingleItem() method.
              return _buildItem(_data[index], animation);
            },
          ),
        ),
        TextField(
          controller: todoController,
          decoration: InputDecoration(
            border: OutlineInputBorder(),
            labelText: 'To-Do'
          ),
        ),

        RaisedButton(
          child: Text('Insert item', style: TextStyle(fontSize: 20)),
          onPressed: () {
            _insertSingleItem();
          },
        ),
        RaisedButton(
          child: Text('Remove item', style: TextStyle(fontSize: 20)),
          onPressed: () {
            _removeSingleItem();
          },
        )
      ],
    );
  }

  // This is the animated row with the Card.
  Widget _buildItem(String item, Animation animation) {
    final isdone = _isdone.contains(item);
    selected.add(false);
    return SizeTransition(
      sizeFactor: animation,
      child: Card(
        child: ListTile(
          title: Text(
            item,
            style: TextStyle(fontSize: 20),
          ),
          trailing: Icon(
           isdone ? Icons.check_box: Icons.check_box_outline_blank
          ),
          onTap: (){
            setState(() {
             
            });
          },

        ),
      ),
    );
  }


  void _insertSingleItem() {
    int insertIndex = 0;
    setState(() {
      _data.insert(0, todoController.text);
    });
    // Add the item to the data list.
    // Add the item visually to the AnimatedList.
    _listKey.currentState.insertItem(insertIndex);
  }

  void _removeSingleItem() {
    int removeIndex = 0;
    // Remove item from data list but keep copy to give to the animation.
    String removedItem = _data.removeAt(removeIndex);
    // This builder is just for showing the row while it is still
    // animating away. The item is already gone from the data list.
    AnimatedListRemovedItemBuilder builder = (context, animation) {
      return _buildItem(removedItem, animation);
    };
    // Remove the item visually from the AnimatedList.
    _listKey.currentState.removeItem(removeIndex, builder);
  }
}```

【问题讨论】:

    标签: android list flutter


    【解决方案1】:

    您已经提到了上面的图标。您只需要使用它们而不是再次声明新的。

    // This is the animated row with the Card.
    Widget _buildItem(String item, Animation animation) {
      final isdone = _isdone.contains(item);
      selected.add(false);
      return SizeTransition(
        sizeFactor: animation,
        child: Card(
          child: ListTile(
            title: Text(
              item,
              style: TextStyle(fontSize: 20),
            ),
            trailing: isdone ? done: notdone, // use the icon variables you have already defined
            onTap: (){
              setState(() {
                // add the item to _isdone set if it is not added and remove it if it is added when tapped on the list item
                if(isdone) {
                  _isdone.remove(item);
                } else {
                  _isdone.add(item);
                }
              });
            },
          ),
        ),
      );
    }
    

    在这段代码中,我在onTap() 中添加并删除了setSate() 中的项目,因此每当您点击列表项时,_isdone Set 都会得到更新,build() 会重新加载.每次点击列表项时,您的布局和数据都会自行更新。

    【讨论】:

    • 非常感谢!它可以工作,但是在我单击项目以更改图标之后,我添加的所有其他项目都已经具有更改的图标。有没有办法只更改列表中一个项目的图标?
    • 啊,问题是,如果我添加一个具有不同字符串的新项目,则图标不会更改,但如果添加一个具有相同字符串的新项目,我已经更改了。图标仍然是更改后的图标。例如,在我的待办事项列表中,我添加了一个“学习”项目,然后单击它以将图标更改为完成。如果我现在添加一个具有相同字符串“正在学习”的新项目,它也完成了更改的图标。如果我添加不同的字符串,一切正常!
    • 是的,因为在代码中您正在检查列表项中的字符串是否完成。因此,如果有两个名称相同的列表项,它们都将显示为选中或未选中。
    猜你喜欢
    • 2020-07-20
    • 2021-09-11
    • 2022-07-13
    • 2019-05-12
    • 2021-01-25
    • 1970-01-01
    • 2021-03-31
    • 2022-11-10
    • 1970-01-01
    相关资源
    最近更新 更多