user-interface - 如何在ListView中调用网络图像类型?

标签 user-interface flutter dart uiimageasset

我有一个应该显示网络图像的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,
                  ),
                ]),
          )),
    );
  }
}



只要检查一下示例,您就会明白

关于user-interface - 如何在ListView中调用网络图像类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59692815/

相关文章:

intellij-idea - 如何在 IntelliJ 中自动完成/导入 Flutter 类?

websocket - Dart扩展WebSocket

dart - 从开头或结尾修剪 Dart 字符串中的空格

matlab - 如何在Matlab uipanel中捕获按键按下

c# - Unity - 检查鼠标点击是否在矩形变换内

dart - 在 Flutter 中更改图像的背景颜色

flutter - 在 flutter 中运行应用程序时,任务 ':app:cleanMergeDebugAssets' 的执行失败

c++ - ID2D1HwndRenderTarget 总是有黑色背景而不是透明

android - 带有手势导航的错误 snackbar 位置

在 ListView 中 flutter GridView