Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter - Loops for ListTile

Tags:

flutter

dart

I am not sure how to generate multiple ListTiles by means of loops, such as for().

I do not know how Flutter works for rendering widgets, since in Angular 2 just insert the *ngFor directives in the layout (html).

I could not find such a subject in the Flutter documentation.

main.dart

import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      title: "Myapp",
      home: new HomePage(),
    );
  }
}

class HomePage extends StatelessWidget {

  @override
  Widget build(BuildContext context) => new Scaffold(
    appBar: new AppBar(
      backgroundColor: new Color(0xFF26C6DA),
    ),
    body: new ListView (
      children: <Widget>[
        new Card(
          child: new Column(
            mainAxisSize: MainAxisSize.min,
            children: <Widget>[
              const ListTile(
                leading: const Icon(Icons.album),
                title: const Text('The Enchanted Nightingale'),
                subtitle: const Text('Music by Julie Gable. Lyrics by Sidney Stein.'),
              ),
            ],
          ),
        ),
      ],
    )    
  );
}
like image 266
rafaelcb21 Avatar asked Feb 05 '23 06:02

rafaelcb21


1 Answers

List.generate() is useful for making small lists. For larger or infinite lists, use a ListView.builder() instead of ListView.

screenshot

import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      title: "Myapp",
      home: new HomePage(),
    );
  }
}

class HomePage extends StatelessWidget {

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
        backgroundColor: new Color(0xFF26C6DA),
      ),
      body: new ListView.builder(
        itemBuilder: (BuildContext context, int index) {
          return new Card(
            child: const ListTile(
              leading: const Icon(Icons.album),
              title: const Text('The Enchanted Nightingale'),
              subtitle: const Text(
                'Music by Julie Gable. Lyrics by Sidney Stein.',
              ),
            ),
          );
        },
      ),
    );
  }
}
like image 120
Collin Jackson Avatar answered Feb 08 '23 15:02

Collin Jackson