11import 'package:flutter/material.dart' ;
2+ import 'dart:math' ;
23
34class Index extends StatefulWidget {
45 @override
56 _IndexState createState () => _IndexState ();
67}
78
89class _IndexState extends State <Index > {
9- bool _checkBoxValue = false ;
10-
10+ List <bool > checkBoxValue = [false , true , true , false , true ];
1111 @override
1212 Widget build (BuildContext context) {
13- return ListView (
14- children: < Widget > [
15- Center (
16- child: Checkbox (
17- value: _checkBoxValue,
18- onChanged: (value) {
19- setState (() {
20- _checkBoxValue = value;
21- });
22- },
23- ),
24- )
25- ],
13+ return Scaffold (
14+ appBar: AppBar (title: Text ('Checkbox' ),),
15+ body: Center (
16+ child: Wrap (
17+ children: List .generate (5 , (index) {
18+ Color color = _randomColor ();
19+ return Checkbox (
20+ value: checkBoxValue[index],
21+ activeColor: color,
22+ tristate: false ,
23+ onChanged: (value) {
24+ setState (() {
25+ checkBoxValue[index] = value;
26+ });
27+ },
28+ );
29+ }),
30+ ),
31+ ),
2632 );
2733 }
2834}
2935
30-
31- // class CheckBoxDemo extends StatefulWidget {
32- // @override
33- // _CheckBoxDemoState createState() => _CheckBoxDemoState();
34- // }
35-
36- // class _CheckBoxDemoState extends State<CheckBoxDemo> {
37- // bool _checkBoxValue;
38- // // _CheckBoxDemoState({Key key, this._checkBoxValue}):super(key: key);
39- // @override
40- // Widget build(BuildContext context) {
41- // return Checkbox(
42- // value: _checkBoxValue,
43- // onChanged: (value) {
44- // setState(() {
45- // _checkBoxValue = value;
46- // });
47- // },
48- // );
49- // }
50- // }
36+ Color _randomColor () {
37+ var red = Random .secure ().nextInt (255 );
38+ var green = Random .secure ().nextInt (255 );
39+ var blue = Random .secure ().nextInt (255 );
40+ return Color .fromARGB (255 , red, green, blue);
41+ }
0 commit comments