Open In App

Flutter – Implement ListWheelChildBuilderDelegate Widget to a List

Last Updated : 06 Nov, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

The ListWheelChildBuilderDelegate widget in Flutter is used with the ListWheelScrollView to create a scrollable list with custom child widgets generated by a builder function. The ListWheelChildBuilderDelegate widget in Flutter is typically used as an argument to the childDelegate property of the ListWheelScrollView. In this article, we are going to implement the ListWheelChildBuilderDelegate widget. A sample video is given below to get an idea about what we are going to do in this article.

Basic Syntax of ListWheelChildBuilderDelegate Widget

ListWheelScrollView(
itemExtent: itemHeight, // Height of each item
childrenDelegate: ListWheelChildBuilderDelegate(
builder: (BuildContext context, int index) {
// Build and return a widget based on the index
return YourChildWidget(index);
},
childCount: itemCount, // Total number of items in the list
),
)

Required Tools

To build this app, you need the following items installed on your machine:

  • Visual Studio Code / Android Studio
  • Android Emulator / iOS Simulator / Physical Device device.
  • Flutter Installed
  • Flutter plugin for VS Code / Android Studio.

Step By Step Implementation

Step 1: Create a New Project in Android Studio

To set up Flutter Development on Android Studio please refer to Android Studio Setup for Flutter Development, and then create a new project in Android Studio please refer to Creating a Simple Application in Flutter.

Step 2: Import the Package

First of all import material.dart file.

import 'package:flutter/material.dart';

Step 3: Execute the main Method

Here the execution of our app starts.

Dart




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


Step 4: Create MyApp Class

In this class we are going to implement the MaterialApp, here we are also set the Theme of our App.

Dart




class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
       theme: ThemeData(
        primarySwatch: Colors.green, // Set the app's primary theme color
      ),
      debugShowCheckedModeBanner: false,
      home: ListWheelChildBuilderDelegateExample(),
    );
  }
}


Step 5: Create ListWheelChildBuilderDelegateExample Class

In this step we are going to implement the ListWheelChildBuilderDelegate Widget in a flutter app. In the childrenDelegate an instance of ListWheelChildBuilderDelegate, which is responsible for building the child widgets within the scrollable list. Comments are added for better understanding.

childDelegate: ListWheelChildBuilderDelegate(
builder: (BuildContext context, int index) {
// Build a ListTile for each item in the list
return ListTile(
title: Text(items[index]), // Display item's name
onTap: () {
// Handle tap event and print item's name
print('Tapped on ${items[index]}');
},
);
},
childCount: items.length, // Total number of items in the list
),

Dart




class ListWheelChildBuilderDelegateExample extends StatelessWidget {
  const ListWheelChildBuilderDelegateExample({super.key});
  
  @override
  Widget build(BuildContext context) {
    // Generate a list of 50 items with unique names
    final List<String> items = List.generate(50, (index) => 'Item $index');
    return Scaffold(
      appBar: AppBar(
        title: Text('ListWheelChildBuilderDelegate Example'),
      ),
      body: Center(
        child: ListWheelScrollView.useDelegate(
          itemExtent: 60.0, // Height of each item in the list
          childDelegate: ListWheelChildBuilderDelegate(
            builder: (BuildContext context, int index) {
              // Build a ListTile for each item in the list
              return ListTile(
                title: Text(items[index]), // Display item's name
                onTap: () {
                  // Handle tap event and print item's name
                  print('Tapped on ${items[index]}');
                },
              );
            },
            childCount: items.length, // Total number of items in the list
          ),
        ),
      ),
    );
  }
}


Here is the full Code of main.dart file

Dart




import 'package:flutter/material.dart';
  
void main() {
  runApp(MyApp());
}
  
class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData(
        primarySwatch: Colors.green, // Set the app's primary theme color
      ),
      debugShowCheckedModeBanner: false,
      home: ListWheelChildBuilderDelegateExample(),
    );
  }
}
  
class ListWheelChildBuilderDelegateExample extends StatelessWidget {
  const ListWheelChildBuilderDelegateExample({super.key});
  
  @override
  Widget build(BuildContext context) {
    // Generate a list of 50 items with unique names
    final List<String> items = List.generate(50, (index) => 'Item $index');
    return Scaffold(
      appBar: AppBar(
        title: Text('ListWheelChildBuilderDelegate Example'),
      ),
      body: Center(
        child: ListWheelScrollView.useDelegate(
          itemExtent: 60.0, // Height of each item in the list
          childDelegate: ListWheelChildBuilderDelegate(
            builder: (BuildContext context, int index) {
              // Build a ListTile for each item in the list
              return ListTile(
                title: Text(items[index]), // Display item's name
                onTap: () {
                  // Handle tap event and print item's name
                  print('Tapped on ${items[index]}');
                },
              );
            },
            childCount: items.length, // Total number of items in the list
          ),
        ),
      ),
    );
  }
}


Output:



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads