User interface 如何在ListView中调用网络映像类型?

User interface 如何在ListView中调用网络映像类型?,user-interface,flutter,dart,uiimageasset,User Interface,Flutter,Dart,Uiimageasset,我有一个gridView,它应该显示一个网络图像。它的所有元素都在包含多个元素的列表中定义。它现在调用AssetImage,但我想将其更改为网络映像。这是列表元素的声明。 我想更改imagePath,但无法理解声明 class Category { Category({ this.title = '', this.imagePath = '', this.lessonCount = '', this.money = 0, this.rating = 0

我有一个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,
    ),
  ];

使用带有url的图像路径

类别(
imagePath:'https://unsplash.com/photos/tpCPd4MbzNU' ,
标题:"游戏",,
新概念英语第二册玩游戏,
货币:18,
评级:4.6,
),
并将图像资产替换为网络资产

child:Image.network(category.imagePath)

使用带有url的图像路径

类别(
imagePath:'https://unsplash.com/photos/tpCPd4MbzNU' ,
标题:"游戏",,
新概念英语第二册玩游戏,
货币:18,
评级:4.6,
),
并将图像资产替换为网络资产

child:Image.network(category.imagePath)
导入“包装:颤振/材料.飞镖”;
main()=>runApp(MyApp());
类MyApp扩展了StatefulWidget{
@凌驾
_MyAppState createState()=>\u MyAppState();
}
类MyAppState扩展了状态{
@凌驾
小部件构建(构建上下文){
//只需在Image.network中传递字符串路径
常量列表选项=常量[
常数选择(
标题:“汽车”,网络图像:https://via.placeholder.com/150"),
常数选择(
标题:“自行车”,网络图像:https://via.placeholder.com/150"),
常数选择(
标题:“船”,网络图像:https://via.placeholder.com/150"),
常数选择(
标题:“总线”,网络映像:https://via.placeholder.com/150"),
];
返回材料PP(
家:安全区(
孩子:脚手架(
主体:填充物(
填充:常数边集全部(8.0),
子:容器(
子项:GridView.count(
交叉轴计数:2,
子项:List.generate(choices.length,(index){
返回中心(
子:容器(
child:ChoiceCard(选项:选项[索引]),
);
}))),
),
),
),
);
}
}
班级选择{
常量选择({this.title,this.networkImage});
最后的字符串标题;
最终字符串networkImage;
}
类ChoiceCard扩展了无状态小部件{
const ChoiceCard({Key-Key,this.choice}):super(Key:Key);
最终选择;
@凌驾
小部件构建(构建上下文){
final TextStyle TextStyle=Theme.of(context).textTheme.display1;
返回容器(
孩子:卡片(
颜色:颜色,白色,
儿童:中心(
子:列(
mainAxisSize:mainAxisSize.min,
crossAxisAlignment:crossAxisAlignment.center,
儿童:[
图像网络(
choice.networkImage,
宽度:150,
),
]),
)),
);
}
}
只需检查一下示例,您就可以得到这个想法

导入“package:flatter/material.dart”;
main()=>runApp(MyApp());
类MyApp扩展了StatefulWidget{
@凌驾
_MyAppState createState()=>\u MyAppState();
}
类MyAppState扩展了状态{
@凌驾
小部件构建(构建上下文){
//只需在Image.network中传递字符串路径
常量列表选项=常量[
常数选择(
标题:“汽车”,网络图像:https://via.placeholder.com/150"),
常数选择(
标题:“自行车”,网络图像:https://via.placeholder.com/150"),
常数选择(
标题:“船”,网络图像:https://via.placeholder.com/150"),
常数选择(
标题:“总线”,网络映像:https://via.placeholder.com/150"),
];
返回材料PP(
家:安全区(
孩子:脚手架(
主体:填充物(
填充:常数边集全部(8.0),
子:容器(
子项:GridView.count(
交叉轴计数:2,
子项:List.generate(choices.length,(index){
返回中心(
子:容器(
child:ChoiceCard(选项:选项[索引]),
);
}))),
),
),
),
);
}
}
班级选择{
常量选择({this.title,this.networkImage});
最后的字符串标题;
最终字符串networkImage;
}
类ChoiceCard扩展了无状态小部件{
const ChoiceCard({Key-Key,this.choice}):super(Key:Key);
最终选择;
@凌驾
小部件构建(构建上下文){
final TextStyle TextStyle=Theme.of(context).textTheme.display1;
返回容器(
孩子:卡片(
颜色:颜色,白色,
儿童:中心(
子:列(
mainAxisSize:mainAxisSize.min,
crossAxisAlignment:crossAxisAlignment.center,
儿童:[
图像网络(
choice.networkImage,
宽度:150,
),
]),
)),
);
}
}

只要检查一下这个例子,你就会明白这个想法

你能解释一下这个问题吗?这就像在
imagePath
中有一个url一样简单,并使用
child:Image.network(category.imagePath)
imagePath的声明仍然是字符串吗?是的,你可以这样做。你能解释更多问题吗?这就像在
imagePath
中有一个url一样简单,并使用
child:Image.network(category.imagePath)
imagePath的声明仍然是字符串吗?是的,你可以这样做,用networkImage替换imagePath,效果非常好,谢谢!将imagePath替换为networkImage,效果很好,谢谢!
 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,
                  ),
                ]),
          )),
    );
  }
}