0
Follow
0
View

I want to add different images each time, I reuses this Card.How to do that without copy pasting the whole Code

salar_dai 注册会员
2023-01-25 20:42

pass image url to OwnCard class and display it:

List urls = ['', '', ''];

GridView.builder(
      gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
        crossAxisCount: 3,
        childAspectRatio: 1.0,
      ),
      itemCount: 12,
      itemBuilder: (context, index) {
        return OwnCard(urls: urls);
      },
    );
    
class OwnCard extends StatelessWidget {
  final List urls;

  const OwnCard({
    Key? key,
    required this.urls,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Card(
      child: Column(
        children: [
          AspectRatio(
            aspectRatio: 18 / 11,
            child: Image.network(urls[0]),
          ),
          Padding(
            padding: EdgeInsets.fromLTRB(16, 12, 16, 8),
            child: Column(
              children: [
                Text('Title'),
                SizedBox(
                  height: 12,
                ),
                Text('Secondary Text'),
                Image.network(urls[1]),
                Image.network(urls[2]),
              ],
            ),
          )
        ],
      ),
    );
wei_kesong 注册会员
2023-01-25 20:42

I prefer this way


class OwnCard extends StatelessWidget {
  final String imagePath;
  final String title;
  final String secondaryText;

  const OwnCard(
      {Key? key,
      required this.imagePath,
      required this.title,
      required this.secondaryText})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Card(
      child: Column(
        children: [
          AspectRatio(
            aspectRatio: 18 / 11,
            child: Image.asset(imagePath),
          ),
          Padding(
            padding: const EdgeInsets.fromLTRB(16, 12, 16, 8),
            child: Column(
              children: [
                Text(title),
                const SizedBox(
                  height: 12,
                ),
                Text(secondaryText)
              ],
            ),
          )
        ],
      ),
    );
  }
}


About the Author

Question Info

Publish Time
2023-01-25 20:42
Update Time
2023-01-25 20:42

Related Question

Prometheus idelta函数确实显示了预期的结果

我如何访问一个数组在openweathermap json使用java?

springboot框架集成mybatis genertor时出现问题

ckeditor加载后更改默认颜色文本

在js中使用getElementById或querySelectorAll获取输入id

无法连接到Presto网关后端

Js ".toJSON()"给出一个"Buffer"(?)而不是json数据

ArcMap无法打开,显示ArcGIS Besktop 停止工作。

在Webots中使用PROTO文件导入.obj和.mtl文件

AttributeError:模块'Kurtosis'没有属性'Kurtosis_c'