我有一个应该显示网络图像的gridView。它的所有元素都在包含多个元素的列表中定义。它现在立即调用AssetImage,但我想将其更改为网络图像。这是列表元素的声明。
我想更改imagePath,但无法理解该声明。
class Category {
Category({
this.title = '',
this.imagePath = '',
this.lessonCount = '',
this.money = 0,
this.rating = 0.0,
});
String title;
String lessonCount;
int money;
double rating;
String imagePath;
static List<Category> offerwallList = <Category>[
Category(
imagePath: 'assets/app/games.png',
title: 'Games',
lessonCount: 'Play Games',
money: 18,
rating: 4.6,
),
];
在下面的其他一些地方也定义了它,这也是我必须更改的地方。
child: Image.asset(category.imagePath)
最佳答案
import 'package:flutter/material.dart';
main() => runApp(MyApp());
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
@override
Widget build(BuildContext context) {
// just pass the String path in Image.network
const List<Choice> choices = const <Choice>[
const Choice(
title: 'Car', networkImage: "https://via.placeholder.com/150"),
const Choice(
title: 'Bicycle', networkImage: "https://via.placeholder.com/150"),
const Choice(
title: 'Boat', networkImage: "https://via.placeholder.com/150"),
const Choice(
title: 'Bus', networkImage: "https://via.placeholder.com/150"),
];
return MaterialApp(
home: SafeArea(
child: Scaffold(
body: Padding(
padding: const EdgeInsets.all(8.0),
child: Container(
child: GridView.count(
crossAxisCount: 2,
children: List.generate(choices.length, (index) {
return Center(
child: Container(
child: ChoiceCard(choice: choices[index])),
);
}))),
),
),
),
);
}
}
class Choice {
const Choice({this.title, this.networkImage});
final String title;
final String networkImage;
}
class ChoiceCard extends StatelessWidget {
const ChoiceCard({Key key, this.choice}) : super(key: key);
final Choice choice;
@override
Widget build(BuildContext context) {
final TextStyle textStyle = Theme.of(context).textTheme.display1;
return Container(
child: Card(
color: Colors.white,
child: Center(
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Image.network(
choice.networkImage,
width: 150,
),
]),
)),
);
}
}
只要检查一下示例,您就会明白