[英]How to change images using the Provider Package in Flutter
Problem: I click the button 'do something' and the image can't seem to change from 'hello' to 'goodbye'.问题:我单击了“做某事”按钮,但图像似乎无法从“你好”变为“再见”。 The error coming back is
回来的错误是
'Error: The argument type 'Image' can't be assigned to the parameter type 'String'. “错误:无法将参数类型“图像”分配给参数类型“字符串”。
Is it possible to change the String into an image so it will read the image and display it on the screen?是否可以将字符串更改为图像,以便读取图像并将其显示在屏幕上?
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Provider<MyModel>(// <--- Provider
create: (context) => MyModel(),
child: Consumer<MyModel>( // <--- MyModel Consumer
builder: (context, myModel, child) {
return ValueListenableProvider<Image>.value( // <--- ValueListenableProvider
value: myModel.someValue,
child: MaterialApp(
home: Scaffold(
appBar: AppBar(title: Text('My App')),
body: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Flexible(
child: Container(
padding: const EdgeInsets.all(20),
color: Colors.green[200],
child: Consumer<MyModel>( // <--- Consumer
builder: (context, myModel, child) {
return RaisedButton(
child: Text('Do something'),
onPressed: (){
myModel.doSomething();
},
);
},
)
),
),
Flexible(
child: Container(
padding: const EdgeInsets.all(35),
color: Colors.blue[200],
child: Consumer<Image>(// <--- String Consumer
builder: (context, myValue, child) {
return Image.asset(myValue);
},
),
),
),
],
),
),
),
);
}),
);
}
}
class MyModel { // <--- MyModel
ValueNotifier<Image> someValue = ValueNotifier(Image.asset('images/hello.png')); // <--- ValueNotifier
void doSomething() {
someValue.value = Image.asset('images/goodbye.png');
print(someValue.value);
}
}
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Provider<MyModel>(
// <--- Provider
create: (context) => MyModel(),
child:
Consumer<MyModel>(// <--- MyModel Consumer
builder: (context, myModel, child) {
return ValueListenableProvider<Image>.value(
// <--- ValueListenableProvider
value: myModel.someValue,
child: MaterialApp(
home: Scaffold(
appBar: AppBar(title: Text('My App')),
body: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Flexible(
child: Container(
padding: const EdgeInsets.all(20),
color: Colors.green[200],
child: Consumer<MyModel>(
// <--- Consumer
builder: (context, myModel, child) {
return RaisedButton(
child: Text('Do something'),
onPressed: () {
myModel.doSomething();
},
);
},
)),
),
Flexible(
child: Container(
padding: const EdgeInsets.all(35),
color: Colors.blue[200],
child: Consumer<Image>(
// <--- String Consumer
builder: (context, myValue, child) {
return myValue; // <--- **Change**
},
),
),
),
],
),
),
),
);
}),
);
}
}
class MyModel {
// <--- MyModel
ValueNotifier<Image> someValue =
ValueNotifier(Image.asset('images/hello.png')); // <--- ValueNotifier
void doSomething() { // <--- **Change**
if (someValue.value.toString() ==
Image.asset('images/hello.png').toString()) {
someValue.value = Image.asset('images/goodbye.png');
} else if (someValue.value.toString() ==
Image.asset('images/goodbye.png').toString()) {
someValue.value = Image.asset('images/hello.png');
}
}
}
myValue is already an image so you don't need to open it again from Image.asset
also added feature to change image back and forth for your doSomething()
function myValue 已经是一个图像,因此您无需从
Image.asset
再次打开它,还添加了为doSomething()
函数来回更改图像的功能
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.