刷新图像和文本



我使用statefull来刷新,但我不知道我必须在setstate中写些什么才能刷新幻灯片日志。

我需要刷新一个我在对话框中选择的图像,它保存在一个名为ex3的变量中。这个变量是一个有名称、图像等的类…所以我应该刷新ex3.image和ex3.name

StatefulBuilder(builder: (context, _setState) {
return Container(
height: 120.0,
width: 120.0,
decoration: BoxDecoration(
image: DecorationImage(
image:
ex3?.image ?? AssetImage("assets/images/2.png"),
fit: BoxFit.fill,
),
shape: BoxShape.circle,
),
child: Text(ex3?.nom ?? "ex: Feliz"),
);
_setState({});
}),

我有一个名为usermodel的类,这个代码是添加新图像和新名称,所以这个值ex3.nom(name(和ex3.image更新

ElevatedButton(
child: Text(ex3?.nom ?? "Elige como te sientes"),
onPressed: () {
SelectDialog.showModal<UserModel>(
context,
label: "¿Como te sientes hoy?",
items: modelItems,
selectedValue: ex3,
itemBuilder: (BuildContext context, UserModel item,
bool isSelected) {
return Container(
decoration: !isSelected
? null
: BoxDecoration(
borderRadius: BorderRadius.circular(5),
color: Colors.white,
border: Border.all(
color: Theme.of(context).primaryColor),
),
child: ListTile(
leading: CircleAvatar(
backgroundImage: AssetImage(item.rutaimage!)),
selected: isSelected,
title: Text(item.nom),
subtitle: Text(item.color),
),
);
},
onChange: (selected) {
setState(() {
ex3 = selected;
});
},
);
},
),

使用bool检查某些内容是否为真您必须在onTap功能中使用CCD_ 2或不在容器中的可点击小部件,阅读这篇文章

class MyWidget extends StatefulWidget {
const MyWidget({Key key}) : super(key: key);
@override
State<MyWidget> createState() => _MyWidgetState();
}
class _MyWidgetState extends State<MyWidget> {
bool _flag = false;
@override
Widget build(BuildContext context) {
return InkWell(
onTap: () {
setState(() {
_flag != _flag;
});
},
child: Container(
height: 120.0,
width: 120.0,
decoration: BoxDecoration(
image: DecorationImage(
image: _flag
? AssetImage("assets/images/2.png")
: AssetImage("assets/images/3.png"),
fit: BoxFit.fill,
),
shape: BoxShape.circle,
),
child: Text(_flag ? "ex: Feliz" : "here is when false"),
),
);
}
}

为AssetImage分配一个变量,并在setState中更新该变量。在您的情况下:

代替:

StatefulBuilder(builder: (context, _setState) {
return Container(
height: 120.0,
width: 120.0,
decoration: BoxDecoration(
image: DecorationImage(
image:
ex3?.image ?? AssetImage("assets/images/2.png"),
fit: BoxFit.fill,
),
shape: BoxShape.circle,
),
child: Text(ex3?.nom ?? "ex: Feliz"),
);
_setState({});
}),

用途:

var path = "assets/images/2.png";
StatefulBuilder(builder: (context, _setState) {
return Container(
height: 120.0,
width: 120.0,
decoration: BoxDecoration(
image: DecorationImage(
image:
ex3?.image ?? AssetImage(path),
fit: BoxFit.fill,
),
shape: BoxShape.circle,
),
child: Text(ex3?.nom ?? "ex: Feliz"),
);
_setState({
path = "YourNewPath";
});
}),

最新更新