CheckboxListTitle using List in Flutter

CheckboxListTitle using List in Flutter : this flutter tutorial is on checkbox list. learn this flutter tutorial it is used in form data. using this code you can add your hobbies in the form

The output and the code of this tutorial is given below.

Screenshot :

CheckboxListTitle using List in Flutter

CheckboxListTitle using List in Flutter :

main.dart

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

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

class Sport {
  int userId;
  String title;
  bool isCheck;

  Sport({this.userId, this.title, this.isCheck});

  static List<Sport> getUsers() {
    return <Sport>[
      Sport(userId: 1, title: "Cricket", isCheck: true),
      Sport(userId: 2, title: "Football", isCheck: false),
      Sport(userId: 3, title: "tumbling", isCheck: false),
      Sport(userId: 4, title: "walking", isCheck: false),
      Sport(userId: 5, title: "baseball", isCheck: false),
    ];
  }
}

class Demo extends StatefulWidget {
  @override
  DemoState createState() => new DemoState();
}

class DemoState extends State<Demo> {
  List<Sport> sportList = Sport.getUsers();

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(title: new Text('CheckboxListTile demo')),
      body: new ListView.builder(
          itemCount: sportList.length,
          itemBuilder: (BuildContext context, int index) {
            return new Card(
              child: new Container(
                padding: new EdgeInsets.all(10.0),
                child: new Column(
                  children: <Widget>[
                    new CheckboxListTile(
                        activeColor: Colors.green, //Check is true color is green
                        dense: true, //font change
                        value: sportList[index].isCheck,
                        title: new Text(sportList[index].title),
                        controlAffinity: ListTileControlAffinity.leading, //Display check box first, remove this line checkbox display last
                        onChanged: (bool val) {
                          itemChange(val, index);
                        })
                  ],
                ),
              ),
            );
          }),
    );
  }

  void itemChange(bool val, int index) {
    setState(() {
      sportList[index].isCheck = val;
    });
  }
}

The flutter tutorial  is a website that bring you the latest and amazing resources of code. All the languages codes are included in this website. The languages like flutter, android, java,kotlin etc.with the help of this languages any user can develop the beautiful application.

For more information about Flutter. visit www.fluttertutorial.in