如何将 class 转换为 list.builder 中的列表视图

How to convert class to listview in list.builder

我正在使用一个名为 Icapp_license 对于 flutter,它会创建一个文件,其中包含您使用的插件的所有许可证,因此输出就是这个文件,我想转换为列表视图

已生成 在此处归档 link

您可以复制粘贴 运行 下面的完整代码
执行命令后 flutter packages pub run icapps_license
license.dart 将在目录 util
中自动生成 你可以import './util/license.dart';

代码片段

ListView.builder(
      itemCount: LicenseUtil.getLicenses().length,
      itemBuilder: (BuildContext context, int index) {
        final item = LicenseUtil.getLicenses()[index];

工作演示

完整代码

import 'package:flutter/material.dart';
import './util/license.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;

  void _incrementCounter() {
    setState(() {
      _counter++;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: ListView.builder(
          itemCount: LicenseUtil.getLicenses().length,
          itemBuilder: (BuildContext context, int index) {
            final item = LicenseUtil.getLicenses()[index];
            return Padding(
              padding: const EdgeInsets.all(8.0),
              child: Container(
                color: Colors.grey.withOpacity(0.5),
                child: Column(
                  children: [
                    Text(item.name),
                    Text(item.version),
                    Text(item.url),
                    Container(height: 8),
                    Text(item.license),
                  ],
                ),
              ),
            );
          },
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: Icon(Icons.add),
      ),
    );
  }
}