flutter 中的复选框是一个材料设计小部件。它总是在 Stateful Widget 中使用,因为它不维护自己的状态。我们可以使用它的 onChanged 属性来交互或修改 Flutter 应用中的其他小部件。与大多数其他 Flutter 小部件一样,它还带有许多属性,如 activeColor、checkColor、mouseCursor 等,让开发人员可以完全控制小部件的外观。
例子:
import 'package:flutter/material.dart';
//importing material design liverary
void main() {
runApp(MaterialApp(
//runApp method
home: HomePage(),
));//MaterialApp
}
class HomePage extends StatefulWidget {
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
bool value = false;
@override
//App widget tree
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('GeeksforGeeks'),
backgroundColor: Colors.greenAccent[400],
leading: IconButton(
icon: Icon(Icons.menu),
tooltip: 'Menu',
onPressed: () {},
), //IcoButton
), //AppBar
body: Center(
/** Card Widget **/
child: Card(
child: Padding(
padding: const EdgeInsets.all(15.0),
child: SizedBox(
width: 430,
height: 700,
child: Column(
children: [
Text(
'Algorithms',
style: TextStyle(
color: Colors.greenAccent[400],
fontSize: 30), //TextStyle
), //Text
SizedBox(height: 10),
Row(
children: <Widget>[
SizedBox(
width: 10,
), //SizedBox
Text(
'Liberary Implementation Of Searching Algorithm: ',
style: TextStyle(fontSize: 17.0),
), //Text
SizedBox(width: 10), //SizedBox
/** Checkbox Widget **/
Checkbox(
value: this.value,
onChanged: (bool value) {
setState(() {
this.value = value;
});
},
), //Checkbox
], //<Widget>[]
), //Row
],
), //Column
), //SizedBox
), //Padding
), //Card
), //Center//Center
), //Scaffold
); //MaterialApp
}
}