Flutter 我有两个图像,用于在颤振中锁定或取消锁定数据

Flutter 我有两个图像,用于在颤振中锁定或取消锁定数据,flutter,flutter-image,flutter-design,Flutter,Flutter Image,Flutter Design,我在颤振中有两个图像,用于在列表视图中锁定和取消锁定数据,因此我的程序是,当我单击锁定图像时,取消锁定图像应隐藏,当我单击取消锁定图像时,取消锁定图像应隐藏。那么如何在flatter中实现这一点 这是我的演示代码 class PinUnpinData extends StatefulWidget { @override _PinUnpinDataState createState() => _PinUnpinDataState(); } class _PinUnpinDataSt

我在颤振中有两个图像,用于在列表视图中锁定和取消锁定数据,因此我的程序是,当我单击锁定图像时,取消锁定图像应隐藏,当我单击取消锁定图像时,取消锁定图像应隐藏。那么如何在flatter中实现这一点

这是我的演示代码

class PinUnpinData extends StatefulWidget {
  @override
  _PinUnpinDataState createState() => _PinUnpinDataState();
}

class _PinUnpinDataState extends State<PinUnpinData> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Colors.white,
        title: Text(
          "Pin-Unpin",
          style: TextStyle(fontSize: 20, color: Colors.white),
        ),
      ),
      backgroundColor: Colors.white,
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: <Widget>[
            InkWell(
                onTap: () {
                  
                },
                child: Padding(
                  padding: const EdgeInsets.all(20),
                  child: Image.asset(
                    "assets/images/pin.png",
                    height: 20,
                    width: 20,
                  ),
                )),
            InkWell(
                onTap: () {},
                child: Padding(
                  padding: const EdgeInsets.all(20),
                  child: Image.asset(
                    "assets/images/unpin.png",
                    height: 20,
                    width: 20,
                  ),
                ))
          ],
        ),
      ),
    );
  }
}
类PinUnpinData扩展StatefulWidget{
@凌驾
_PinupIndataState createState()=>\u PinupIndataState();
}
类_PinUnpinDataState扩展状态{
@凌驾
小部件构建(构建上下文){
返回脚手架(
appBar:appBar(
背景颜色:Colors.white,
标题:正文(
“别针解开”,
样式:TextStyle(字体大小:20,颜色:Colors.white),
),
),
背景颜色:Colors.white,
正文:中(
子:列(
mainAxisAlignment:mainAxisAlignment.center,
crossAxisAlignment:crossAxisAlignment.center,
儿童:[
墨水池(
onTap:(){
},
孩子:填充(
填充:常数边集。全部(20),
子:Image.asset(
“assets/images/pin.png”,
身高:20,
宽度:20,
),
)),
墨水池(
onTap:(){},
孩子:填充(
填充:常数边集。全部(20),
子:Image.asset(
“assets/images/unpin.png”,
身高:20,
宽度:20,
),
))
],
),
),
);
}
}

创建一个局部变量以跟踪
固定的状态。然后使用
setState()
方法在点击按钮时更新该变量的状态。另外,要显示相关图像,只需检查
pinted
变量的值并显示相关图像,如果为真,则显示unpin图像,否则显示pin图像

class _PinUnpinDataState extends State<PinUnpinData> {

  bool pinned = false; // to keep track if it's pinned or not

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Colors.white,
        title: Text(
          "Pin-Unpin",
          style: TextStyle(fontSize: 20, color: Colors.white),
        ),
      ),
      backgroundColor: Colors.white,
      body: Center(
        child: InkWell(
            onTap: () {
              setState(() {
                pinned = pinned ? false : true; // check if pinned is true, if its true then set it false and voice versa
              });
            },
            child: Padding(
              padding: const EdgeInsets.all(20),
              child: Image.asset(
                pinned
                    ? "assets/images/unpin.png" //show this image when it's pinned
                    : "assets/images/pin.png", // show this image when it not pinned
                height: 20,
                width: 20,
              ),
            )),
      ),
    );
  }
}
class\u PinUnpinDataState扩展状态{
bool pinted=false;//跟踪是否已锁定
@凌驾
小部件构建(构建上下文){
返回脚手架(
appBar:appBar(
背景颜色:Colors.white,
标题:正文(
“别针解开”,
样式:TextStyle(字体大小:20,颜色:Colors.white),
),
),
背景颜色:Colors.white,
正文:中(
孩子:InkWell(
onTap:(){
设置状态(){
pinted=pinted?false:true;//检查pinted是否为true,如果为true,则将其设置为false,反之亦然
});
},
孩子:填充(
填充:常数边集。全部(20),
子:Image.asset(
钉住
?“assets/images/unpin.png”//固定此图像时显示此图像
:“assets/images/pin.png”,//未固定时显示此图像
身高:20,
宽度:20,
),
)),
),
);
}
}

如何使用ToastFlatter在此编码中显示pin和unpin在
onTap:(){…}
方法中使用您的消息显示所有toast方法。此外,如果我的回答有助于你实现你所追求的目标,请记下我的答案。:)