flutter-tutorial

Flutter Checkbox

In flutter, a checkbox is a material design widget. It’s always utilised in the Stateful Widget because it doesn’t have its own state. Its onChanged property can be used to interact with and alter other widgets in the flutter app. It, like most other flutter widgets, has a number of properties, such as activeColor, checkColor, mouseCursor, and so on, that give developers complete flexibility over the widget’s appearance.

Syntax

Checkbox(
  value: false,
  onChanged: () {},
),

Example

Flutter Application with a Checkbox widget.

isChecked boolean variable is used to hold the state of this Checkbox widget, whether checked (true) or not (false).

import 'package:flutter/material.dart';

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

/// main application widget
class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  static const String _title = 'Learn Code Zone';

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: _title,
      home: Scaffold(
        appBar: AppBar(title: const Text(_title)),
        body: const MyStatefulWidget(),
      ),
    );
  }
}

/// stateful widget that the main application instantiates
class MyStatefulWidget extends StatefulWidget {
  const MyStatefulWidget({Key? key}) : super(key: key);

  @override
  State<MyStatefulWidget> createState() => _MyStatefulWidgetState();
}

/// private State class that goes with MyStatefulWidget
class _MyStatefulWidgetState extends State<MyStatefulWidget> {
  bool isChecked = false;
  @override
  Widget build(BuildContext context) {
    return Center(
      child: Column(
        children: <Widget>[
          const SizedBox(
            height: 30,
          ),
          Checkbox(
            checkColor: Colors.white,
            value: isChecked,
            onChanged: (bool? value) {
              setState(() {
                isChecked = value!;
              });
            },
          ),
        ],
      ),
    );
  }
}


Output

RECOMMENDED ARTICLES





Leave a Reply

Your email address will not be published.