Flutter Tutorials

Flutter app development tutorials


flutter – Checkbox onChanged

main.dart


import 'package:flutter/material.dart';

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

class FlutterExample extends StatelessWidget {
  const FlutterExample({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        title: 'Flutter Example',
        theme: ThemeData(primarySwatch: Colors.indigo),
        home: const StateExample()
    );
  }
}

class StateExample extends StatefulWidget {
  const StateExample({Key? key}) : super(key: key);

  @override
  _StateExampleState createState() => _StateExampleState();
}

class _StateExampleState extends State<StateExample> {
  bool checkedStatus = false;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        backgroundColor: const Color(0xFFF5F5F5),
        appBar: AppBar(
            title: const Text("Flutter - Checkbox onChanged")
        ),
        body: bodyContent()
    );
  }


  bodyContent() {
    return Center(
        child: Checkbox(
          value: checkedStatus,
          onChanged: (bool? value){
              setState(() {
                checkedStatus = value??false;
              });
              SnackBar snackBar = SnackBar(
                  content: Text(
                      (value??false)? "Checkbox Checked" : "Checkbox Unchecked"
                  )
              );
              ScaffoldMessenger.of(context).showSnackBar(snackBar);
          },
        )
    );
  }
}

More flutter tutorials



About Me

Flutter Developer

Design a site like this with WordPress.com
Get started