Dart 我的CachedNetworkImage没有';当值改变时,它不会改变 @覆盖 小部件构建(构建上下文){ var开关按钮=新开关( 价值:细节, 一旦更改:(布尔值){ 设置状态(){ 细节=价值; }); }, ); var imagejadwal=新缓存网络映像( imageUrl:开关按钮。值?“https://drive.google.com/uc?export=download&id=1v-dA5bG7Fwk_hJvL2wu4Z9P10JdsaWIe:“https://drive.google.com/uc?export=download&id=1qfdI_yM7rzdLMqRizlr76445qc0IQKhD", 占位符:新的CircularProgressIndicator(), errorWidget:新图标(Icons.error), ); 归还新脚手架( appBar:新的appBar( 标题:新文本(widget.title), ), 正文:新栏目( 儿童:[ 新对齐( 对齐:alignment.topRight, 孩子:开关按钮, ), 贾德瓦尔, ], ) ); }
这是因为CachedNetworkImage或我的代码错误?谁能 帮助我?我还是新来的,谢谢你Dart 我的CachedNetworkImage没有';当值改变时,它不会改变 @覆盖 小部件构建(构建上下文){ var开关按钮=新开关( 价值:细节, 一旦更改:(布尔值){ 设置状态(){ 细节=价值; }); }, ); var imagejadwal=新缓存网络映像( imageUrl:开关按钮。值?“https://drive.google.com/uc?export=download&id=1v-dA5bG7Fwk_hJvL2wu4Z9P10JdsaWIe:“https://drive.google.com/uc?export=download&id=1qfdI_yM7rzdLMqRizlr76445qc0IQKhD", 占位符:新的CircularProgressIndicator(), errorWidget:新图标(Icons.error), ); 归还新脚手架( appBar:新的appBar( 标题:新文本(widget.title), ), 正文:新栏目( 儿童:[ 新对齐( 对齐:alignment.topRight, 孩子:开关按钮, ), 贾德瓦尔, ], ) ); },dart,flutter,Dart,Flutter,这是因为CachedNetworkImage或我的代码错误?谁能 帮助我?我还是新来的,谢谢你 Lib:您的代码运行良好,但是您需要处理两个问题: 图像溢出,可能会阻止UI更新 图像太大,需要等待一点时间才能加载 我已经修改了您的一些代码: class\u CachedImageExampleState扩展状态{ bool switchState=true; @凌驾 小部件构建(构建上下文){ var开关按钮=新开关( 值:switchState, 一旦更改:(布尔值){ 设置状态(){ 开关
Lib:您的代码运行良好,但是您需要处理两个问题:
class\u CachedImageExampleState扩展状态{
bool switchState=true;
@凌驾
小部件构建(构建上下文){
var开关按钮=新开关(
值:switchState,
一旦更改:(布尔值){
设置状态(){
开关状态=值;
});
},
);
var imagejadwal=新缓存网络映像(
imageUrl:switchState
? "https://drive.google.com/uc?export=download&id=1v-dA5bG7Fwk_hJvL2wu4Z9P10JdsaWIe”
: "https://drive.google.com/uc?export=download&id=1qfdI_yM7rzdLMqRizlr76445qc0IQKhD",
占位符:新的CircularProgressIndicator(),
errorWidget:新图标(Icons.error),
);
归还新脚手架(
appBar:新的appBar(
标题:新文本(“测试图像”),
),
正文:新栏目(
儿童:[
新对齐(
对齐:alignment.topRight,
孩子:开关按钮,
),
新容器(
//宽度:500.0,
///容器来处理溢出,您可能不希望将其用于硬编码
///高度,因为它不允许视图响应,但它只是
///注意处理溢出的问题
高度:400.0,
儿童:imagejadwal),
],
)
);
}
}
我设法解决了你的问题,这个插件有两个实现,下面的一个应该可以帮你解决。我不确定状态不更新背后的原因(可能无法覆盖imageUrl)
无论如何,这是您的解决方案:
class _CachedImageExampleState extends State<CachedImageExample> {
bool switchState = true;
@override
Widget build(BuildContext context) {
var switchButton = new Switch(
value: switchState,
onChanged: (bool value) {
setState(() {
switchState = value;
});
},
);
var imagejadwal = new CachedNetworkImage(
imageUrl: switchState
? "https://drive.google.com/uc?export=download&id=1v-dA5bG7Fwk_hJvL2wu4Z9P10JdsaWIe"
: "https://drive.google.com/uc?export=download&id=1qfdI_yM7rzdLMqRizlr76445qc0IQKhD",
placeholder: new CircularProgressIndicator(),
errorWidget: new Icon(Icons.error),
);
return new Scaffold(
appBar: new AppBar(
title: new Text("TestImage"),
),
body: new Column(
children: <Widget>[
new Align(
alignment: Alignment.topRight,
child: switchButton,
),
new Container (
//width: 500.0,
///container to deal with the overflow, you may not want to use it with hardcoded
///height because it will not allow the view to be responsive, but it is just to
///make a point about dealing with the overflow
height: 400.0,
child: imagejadwal),
],
)
);
}
}
class CachedImageExample扩展了StatefulWidget{
@凌驾
_CachedImageExampleState createState()=>new_CachedImageExampleState();
}
类\u CachedImageExampleState扩展状态{
布尔切换=真;
@凌驾
小部件构建(构建上下文){
var开关按钮=新开关(
值:切换,
一旦更改:(布尔值){
设置状态(){
切换=值;
});
},
);
var img=新映像(映像:new CachedNetworkImageProvider(
切换
? "https://drive.google.com/uc?export=download&id=1v-dA5bG7Fwk_hJvL2wu4Z9P10JdsaWIe”
: "https://drive.google.com/uc?export=download&id=1qfdI_yM7rzdLMqRizlr76445qc0IQKhD"));
归还新脚手架(
appBar:新的appBar(
标题:新文本(“测试图像”),
),
正文:新栏目(
儿童:[
新对齐(
对齐:alignment.topRight,
孩子:开关按钮,
),
新容器(
宽度:200.0,
高度:200.0,
儿童:img),
],
)
);
}
}
更新:使图像适合整个屏幕
class CachedImageExample extends StatefulWidget {
@override
_CachedImageExampleState createState() => new _CachedImageExampleState();
}
class _CachedImageExampleState extends State<CachedImageExample> {
bool toggle = true;
@override
Widget build(BuildContext context) {
var switchButton = new Switch(
value: toggle,
onChanged: (bool value) {
setState(() {
toggle = value;
});
},
);
var img= new Image(image: new CachedNetworkImageProvider(
toggle
? "https://drive.google.com/uc?export=download&id=1v-dA5bG7Fwk_hJvL2wu4Z9P10JdsaWIe"
: "https://drive.google.com/uc?export=download&id=1qfdI_yM7rzdLMqRizlr76445qc0IQKhD"));
return new Scaffold(
appBar: new AppBar(
title: new Text("TestImage"),
),
body: new Column(
children: <Widget>[
new Align(
alignment: Alignment.topRight,
child: switchButton,
),
new Container (
width: 200.0,
height: 200.0,
child: img),
],
)
);
}
}
class CachedImageExample扩展了StatefulWidget{
@凌驾
_CachedImageExampleState createState()=>new_CachedImageExampleState();
}
类\u CachedImageExampleState扩展状态{
布尔切换=真;
@凌驾
小部件构建(构建上下文){
var开关按钮=新开关(
activeColor:Colors.amber,
activeTrackColor:Colors.amberAccent,
颜色:Colors.amber,
值:切换,
一旦更改:(布尔值){
设置状态(){
切换=值;
});
},
);
归还新脚手架(
appBar:新的appBar(
行动:[
开关按钮
],
标题:新文本(“测试图像”),
),
正文:
新容器(
装饰:新盒子装饰(
图片:新装饰图片(
适合:BoxFit.cover,
图片:
新CachedNetworkImageProvider(
切换
? "https://drive.google.com/uc?export=download&id=1v-dA5bG7Fwk_hJvL2wu4Z9P10JdsaWIe”
: "https://drive.google.com/uc?export=download&id=1qfdI_yM7rzdLMqRizlr76445qc0IQKhD"
)
),
),
),
);
}
}
我尝试了aziza的代码,但它对我也不起作用
我在CachedNetworkImage中更改了一些代码,这似乎有效,我更改了“didUpdateWidget”:
class CachedImageExample extends StatefulWidget {
@override
_CachedImageExampleState createState() => new _CachedImageExampleState();
}
class _CachedImageExampleState extends State<CachedImageExample> {
bool toggle = true;
@override
Widget build(BuildContext context) {
var switchButton = new Switch(
activeColor: Colors.amber,
activeTrackColor: Colors.amberAccent,
inactiveThumbColor: Colors.amber,
value: toggle,
onChanged: (bool value) {
setState(() {
toggle = value;
});
},
);
return new Scaffold(
appBar: new AppBar(
actions: <Widget>[
switchButton
],
title: new Text("TestImage"),
),
body:
new Container (
decoration: new BoxDecoration(
image: new DecorationImage(
fit: BoxFit.cover,
image:
new CachedNetworkImageProvider(
toggle
? "https://drive.google.com/uc?export=download&id=1v-dA5bG7Fwk_hJvL2wu4Z9P10JdsaWIe"
: "https://drive.google.com/uc?export=download&id=1qfdI_yM7rzdLMqRizlr76445qc0IQKhD"
)
),
),
),
);
}
}
它需要更改其ImageProvider。我在github上做了一个
也可以使用堆栈。这样,您可以更好地控制从一个图像到另一个图像的动画。比如说
@override
void didUpdateWidget(CachedNetworkImage oldWidget) {
super.didUpdateWidget(oldWidget);
if (widget.imageUrl != oldWidget.imageUrl ||
widget.placeholder != widget.placeholder){
_imageProvider = new CachedNetworkImageProvider(widget.imageUrl,
errorListener: _imageLoadingFailed);
_resolveImage();
}
}
class\u CachedImageExampleState扩展状态{
bool switchState=true;
@超越
@override
void didUpdateWidget(CachedNetworkImage oldWidget) {
super.didUpdateWidget(oldWidget);
if (widget.imageUrl != oldWidget.imageUrl ||
widget.placeholder != widget.placeholder){
_imageProvider = new CachedNetworkImageProvider(widget.imageUrl,
errorListener: _imageLoadingFailed);
_resolveImage();
}
}
class _CachedImageExampleState extends State<CachedImageExample> {
bool switchState = true;
@override
Widget build(BuildContext context) {
var switchButton = new Switch(
value: switchState,
onChanged: (bool value) {
setState(() {
switchState = value;
});
},
);
var imagejadwal1 = new CachedNetworkImage(
imageUrl: "https://drive.google.com/uc?export=download&id=1v-dA5bG7Fwk_hJvL2wu4Z9P10JdsaWIe",
placeholder: new CircularProgressIndicator(),
errorWidget: new Icon(Icons.error),
);
var imagejadwal2 = new CachedNetworkImage(
imageUrl: "https://drive.google.com/uc?export=download&id=1qfdI_yM7rzdLMqRizlr76445qc0IQKhD",
placeholder: new CircularProgressIndicator(),
errorWidget: new Icon(Icons.error),
);
return new Scaffold(
appBar: new AppBar(
title: new Text("TestImage"),
),
body: new Column(
children: <Widget>[
new Align(
alignment: Alignment.topRight,
child: switchButton,
),
new Container (
//width: 500.0,
///container to deal with the overflow, you may not want to use it with hardcoded
///height because it will not allow the view to be responsive, but it is just to
///make a point about dealing with the overflow
height: 400.0,
child: new Stack(children: <Widget>[
new Opacity(opacity: switchState ? 1.0 : 0.0, child: imagejadwal1),
new Opacity(opacity: switchState ? 0.0 : 1.0, child: imagejadwal2,)
],)),
],
)
);
}
}