【问题标题】:OnPressed FloatingActionButton set IconOnPressed FloatingActionButton 设置图标
【发布时间】:2018-06-08 11:10:55
【问题描述】:
    class Tickets extends StatefulWidget {

      int groupid;
      int event_id;

      Tickets([this.groupid, this.event_id]);

      @override
      _TicketsState createState() => new _TicketsState();
    }
     List<Widget> ListMyWidgets(ticketGroups) {

    IconData icon = Icons.undo;
    List<Widget> list = new List();
    list.add(new CupertinoNavigationBar(
      backgroundColor: Colors.blue,
      leading:
          new Padding(
        padding: new EdgeInsets.all(8.0),
        child: new Text(ticketGroups[0]['ticket_name'],
            style: const TextStyle(
                fontSize: 20.0, fontFamily: 'Poppins', color: Colors.white)),
      ),

      trailing: new Padding(
          padding: new EdgeInsets.all(8.0),
          child: new Text('${ticketGroups.length}',
              style: const TextStyle(
                  fontSize: 20.0, fontFamily: 'Poppins', color: Colors.white))),


    ));


      for (int i = 0; i < ticketGroups.length; i++) {

        list.add(

          new GestureDetector(
              onHorizontalDragStart: (event) {
                print("checked in");
                print(ticketGroups[i]['id']);
                setState(() {
                  _getStatusIn(ticketGroups[i]);
                });
              },
              onDoubleTap: () {
                setState(() {
                  _getStatusOut(ticketGroups[i]);
                });
              },
              child: new Card(
                child: new Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: <Widget>[
                    new Padding(
                      padding: new EdgeInsets.all(8.0),
                      child: new Text(
                          "${ticketGroups[i]['ticket_name']}",
                          style:
                          const TextStyle(
                              fontSize: 15.0, fontFamily: 'Poppins')),
                    ),
                    new Padding(
                        padding: new EdgeInsets.all(8.0),
                        child: new Text(ticketGroups[i]["used"].toString(),
                            style: const TextStyle(
                                fontSize: 15.0, fontFamily: 'Poppins')),
                    ),


                    new SizedBox(
                        height: 30.0,
                        width: 30.0,
                        child: new IconButton(
                          padding: new EdgeInsets.all(0.0),

                          icon: new FloatingActionButton(
                              onPressed: (){
                                setState(() {
                                  icon = Icons.check_circle;
                                });

                              },
                              child: new Icon(icon, size: 20.0),heroTag: null)
                       child: new Icon(icon,size: 25.0),heroTag: null)
                          )

                        )
]
                    ),


              )),
        );
      }

  }

这是上一篇文章的更新部分,如您所见,我已经完成了建议的操作,但是当我点击浮动操作按钮时,其中的图标不会改变。 这是上一篇文章的更新部分,如您所见,我已经完成了建议的操作,但是当我点击浮动操作按钮时,其中的图标不会改变。

【问题讨论】:

    标签: dart flutter


    【解决方案1】:

    1) 声明要全局显示的图标

    IconData icon = Icons.check_circle;
    

    2) 使用此图标代替硬编码图标

    new FloatingActionButton(
        onPressed: (){
           setState(() {
               icon = icon == Icons.undo ? Icons.check_circle : Icons.undo; // Change icon and setState to rebuild
           });
        },
        child: new Icon(icon, size: 25.0),heroTag: null)
    );
    

    【讨论】:

    • @HeyabRedda 这肯定会奏效。您需要确保您在StatefulWidget 中操作。
    • 是的。正如@creativecreatorormaybenot 所说,为了更改小部件的状态,您必须使用有状态小部件。
    • 它将初始图标设置为 check_circle,无论我点击按钮时,它都不会改变,是的,我在 StatefulWidget 中工作
    • 发布您修改后的完整代码。只有我们才能确定问题所在。
    • 您代码中的 'icon' 不是全局变量。它在方法中声明。因此,每次调用此方法时,'icon 的值将设置为 Icons.undo'。为了使图标全局声明它在方法“ListMyWidgets”之外。
    猜你喜欢
    • 2023-03-19
    • 2016-08-05
    • 1970-01-01
    • 1970-01-01
    • 2020-02-21
    • 2021-11-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多