We often need to create lists that display different types of content. Forexample, we might be working on a List that shows a heading followed by a fewitems related to the heading, followed by another heading, and so on.

How would we create such a structure with Flutter?

Directions

  • Create a data source with different types of items
  • Convert the data source into a List of Widgets

1. Create a data source with different types of item

Types of Items

In order to represent different types of items in a List, we’ll need to definea class for each type of item.

In this example, we’ll work on an app that shows a header followed by fivemessages. Therefore, we’ll create three classes: ListItem, HeadingItem,and MessageItem.

  1. // The base class for the different types of items the List can contain
  2. abstract class ListItem {}
  3. // A ListItem that contains data to display a heading
  4. class HeadingItem implements ListItem {
  5. final String heading;
  6. HeadingItem(this.heading);
  7. }
  8. // A ListItem that contains data to display a message
  9. class MessageItem implements ListItem {
  10. final String sender;
  11. final String body;
  12. MessageItem(this.sender, this.body);
  13. }

Create a List of Items

Most of the time, we’d fetch data from the internet or a local database andconvert that data into a list of items.

For this example, we’ll generate a list of items to work with. The list willcontain a header followed by five messages. Rinse, repeat.

  1. final items = List<ListItem>.generate(
  2. 1200,
  3. (i) => i % 6 == 0
  4. ? HeadingItem("Heading $i")
  5. : MessageItem("Sender $i", "Message body $i"),
  6. );

2. Convert the data source into a List of Widgets

In order to handle converting each item into a Widget, we’ll employ theListView.builderconstructor.

In general, we’ll want to provide a builder function that checks for what typeof item we’re dealing with, and returns the appropriate Widget for that type ofitem.

In this example, using the is keyword to check the type of item we’re dealingwith can be handy. It’s fast, and will automatically cast each item to theappropriate type. However, there are different ways to approach this problem ifyou prefer another pattern!

  1. ListView.builder(
  2. // Let the ListView know how many items it needs to build
  3. itemCount: items.length,
  4. // Provide a builder function. This is where the magic happens! We'll
  5. // convert each item into a Widget based on the type of item it is.
  6. itemBuilder: (context, index) {
  7. final item = items[index];
  8. if (item is HeadingItem) {
  9. return ListTile(
  10. title: Text(
  11. item.heading,
  12. style: Theme.of(context).textTheme.headline,
  13. ),
  14. );
  15. } else if (item is MessageItem) {
  16. return ListTile(
  17. title: Text(item.sender),
  18. subtitle: Text(item.body),
  19. );
  20. }
  21. },
  22. );

Complete example

  1. import 'package:flutter/foundation.dart';
  2. import 'package:flutter/material.dart';
  3. void main() {
  4. runApp(MyApp(
  5. items: List<ListItem>.generate(
  6. 1000,
  7. (i) => i % 6 == 0
  8. ? HeadingItem("Heading $i")
  9. : MessageItem("Sender $i", "Message body $i"),
  10. ),
  11. ));
  12. }
  13. class MyApp extends StatelessWidget {
  14. final List<ListItem> items;
  15. MyApp({Key key, @required this.items}) : super(key: key);
  16. @override
  17. Widget build(BuildContext context) {
  18. final title = 'Mixed List';
  19. return MaterialApp(
  20. title: title,
  21. home: Scaffold(
  22. appBar: AppBar(
  23. title: Text(title),
  24. ),
  25. body: ListView.builder(
  26. // Let the ListView know how many items it needs to build
  27. itemCount: items.length,
  28. // Provide a builder function. This is where the magic happens! We'll
  29. // convert each item into a Widget based on the type of item it is.
  30. itemBuilder: (context, index) {
  31. final item = items[index];
  32. if (item is HeadingItem) {
  33. return ListTile(
  34. title: Text(
  35. item.heading,
  36. style: Theme.of(context).textTheme.headline,
  37. ),
  38. );
  39. } else if (item is MessageItem) {
  40. return ListTile(
  41. title: Text(item.sender),
  42. subtitle: Text(item.body),
  43. );
  44. }
  45. },
  46. ),
  47. ),
  48. );
  49. }
  50. }
  51. // The base class for the different types of items the List can contain
  52. abstract class ListItem {}
  53. // A ListItem that contains data to display a heading
  54. class HeadingItem implements ListItem {
  55. final String heading;
  56. HeadingItem(this.heading);
  57. }
  58. // A ListItem that contains data to display a message
  59. class MessageItem implements ListItem {
  60. final String sender;
  61. final String body;
  62. MessageItem(this.sender, this.body);
  63. }

Mixed List Demo