Search code examples
flutterflutter-layoutflutter-pluginflutter-widgetflutter-listview

How to convert class to listview in list.builder


I am using a plugin called Icapp_license for flutter which creates a file which includes all the license of the plugins you use so the output was this file and I want to convert to listview

Generated File here link


Solution

  • You can copy paste run full code below
    After execute command flutter packages pub run icapps_license
    license.dart will auto generate in directory util
    You can import './util/license.dart';

    code snippet

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

    working demo

    enter image description here

    full code

    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),
          ),
        );
      }
    }