-
Checkbox組件
Checkbox組件常用的屬性:
屬性 | 描述 |
value |
true 或者 false
|
onChanged
|
改變的時候觸發的事件
|
activeColor
|
選中的顏色、背景顏色
|
checkColor
|
選中的顏色、Checkbox 里面對號的顏色
|
import 'package:flutter/material.dart'; void main() { runApp(MaterialApp( title: "CheckBox", home: MyApp(), )); } class MyApp extends StatefulWidget { @override _MyAppState createState() => _MyAppState(); } class _MyAppState extends State<MyApp> { var flag = false; @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar(title: Text("CheckBox")), body: Column( mainAxisAlignment: MainAxisAlignment.center, children: <Widget>[ Row( mainAxisAlignment: MainAxisAlignment.center, children: <Widget>[ Checkbox( value: this.flag, onChanged: (value) { setState(() { this.flag = value; }); }, activeColor: Colors.red, checkColor: Colors.blue, ) ], ), Row( mainAxisAlignment: MainAxisAlignment.center, children: <Widget>[ Text(this.flag ? "選中" : "未選中") ], ) ], ), ); } }
-
CheckboxListTile組件
屬性 | 描述 |
value
|
true 或者 false
|
onChanged
|
改變的時候觸發的事件
|
activeColor
|
選中的顏色、背景顏色
|
title
|
標題
|
subtitle
|
二級標題
|
secondary
|
配置圖標或者圖片
|
selected
|
選中的時候文字顏色是否跟着改變
|
import 'package:flutter/material.dart'; void main() { runApp(MaterialApp( title: "CheckboxListTile", home: MyApp(), )); } class MyApp extends StatefulWidget { @override _MyAppState createState() => _MyAppState(); } class _MyAppState extends State<MyApp> { var flag = false; @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar(title: Text("CheckboxListTile")), body: CheckboxListTile( value: this.flag, title: Text("標題"), subtitle: Text("這是二級標題"), secondary: Icon(Icons.headset_mic), selected: this.flag, onChanged: (value) { setState(() { this.flag = value; }); }, ), ); } }