当前位置:网站首页>浅识Flutter 基本组件之CheckboxListTile组件
浅识Flutter 基本组件之CheckboxListTile组件
2022-07-31 03:12:00 【阿大豆】
浅识Flutter 基本组件之CheckboxListTile组件
CheckboxListTile的使用方法、功能和Checkbox差不多,但是它的使用范围更广、设计出的UI更美观
CheckboxListTile常用属性如下表所示。
| 属性名 | 类型 | 功能说明 |
|---|---|---|
| title | widget | 设置主标题组件 |
| subtitle | Widget | 设置副标题组件 |
| isThreeLine | bool | 设置显示的复选框是否占三行,默认值为false |
| dense | bool | 设置是否垂直密集显示标题 |
| secondary | widget | 设置显示的小组件,与□所在位置相反 |
| selected | bool | 设置选中后标题文字高亮,默认值为 false |
| controlAffinity | ListTileControlAffinity | 设置□相对于标题文字的位置,取值包含leading(前面)、platform (后面)和trailing(后面) |

CheckboxListTile的属性值
const CheckboxListTile({
Key? key,
required this.value,
required this.onChanged,
this.activeColor,
this.checkColor,
this.tileColor,
this.title,
this.subtitle,
this.isThreeLine = false,
this.dense,
this.secondary,
this.selected = false,
this.controlAffinity = ListTileControlAffinity.platform,
this.autofocus = false,
this.contentPadding,
this.tristate = false,
this.shape,
this.selectedTileColor,
this.visualDensity,
this.focusNode,
this.enableFeedback,
}) : assert(tristate != null),
assert(tristate || value != null),
assert(isThreeLine != null),
assert(!isThreeLine || subtitle != null),
assert(selected != null),
assert(controlAffinity != null),
assert(autofocus != null),
super(key: key);
必须要有value,onChanged
CheckboxListTile(onChanged: (bool? value) {
}, value:false,),

controlAffinity 设置□相对于标题文字的位置,取值包含leading(前面)、platform (后面)和trailing(后面)
/*调整复选框和图标的位置*/
controlAffinity:ListTileControlAffinity.leading ,
设置前

设置后:
创建多个CheckboxListTile
Column column = Column(
children: <Widget>[
CheckboxListTile(
value: this.isChecked,
/*设置主标题组件*/
title: Text(
'全选',
style: TextStyle(color: Colors.red),
),
/*设置副标题组件*/
subtitle: Text('全选表示自成一派'),
/*设置显示的小组件,与□所在位置相反*/
secondary: Icon(Icons.flag),
/*调整复选框和图标的位置*/
controlAffinity: ListTileControlAffinity.leading,
onChanged: (bool? value) {
setState(() {
isChecked = value!;
});
},
),
CheckboxListTile(
title: Text(select[0]),
subtitle: Text('堕胎小分队'),
secondary: Icon(Icons.info),
/*调整复选框和图标的位置*/
controlAffinity: ListTileControlAffinity.leading,
value: flag[0],
onChanged: (value) {
setState(() {
flag[0] = value;
});
}
),
CheckboxListTile(
title: Text(select[1]),
subtitle: Text('世兰好苦队'),
secondary: Icon(Icons.info),
/*调整复选框和图标的位置*/
controlAffinity: ListTileControlAffinity.leading,
value: flag[1],
onChanged: (value) {
setState(() {
flag[1] = value;
});
}
),
CheckboxListTile(
title: Text(select[2]),
subtitle: Text('莞莞类卿队'),
secondary: Icon(Icons.info),
/*调整复选框和图标的位置*/
controlAffinity: ListTileControlAffinity.leading,
value: flag[2],
onChanged: (value) {
setState(() {
flag[2] = value;
});
}
),
],
);

实现一键全选或一键取消全选 功能

全部代码
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
class zhucepage extends StatefulWidget {
@override
State<StatefulWidget> createState() {
return MyState();
}
}
class MyState extends State {
List flag = [false, false, false];
List select = ['皇后', '华妃', '甄嬛'];
bool isChecked = false;
@override
Widget build(BuildContext context) {
Row row = Row(
children: <Widget>[
Text('选择你的阵营: '),
Text(select[0]),
Checkbox(
value: flag[0],
onChanged: (value) {
//setState更新值
setState(() {
flag[0] = value!;
});
},
),
Text(select[1]),
Checkbox(
value: flag[1],
onChanged: (value) {
//setState更新值
setState(() {
flag[1] = value!;
});
},
),
Text(select[2]),
Checkbox(
value: flag[2],
onChanged: (value) {
//setState更新值
setState(() {
flag[2] = value!;
});
},
),
],
);
/*创建多个CheckboxListTile*/
Column column = Column(
children: <Widget>[
CheckboxListTile(
value: this.isChecked,
/*设置主标题组件*/
title: Text(
'全选',
style: TextStyle(color: Colors.red),
),
/*设置副标题组件*/
subtitle: Text('全选表示自成一派'),
/*设置显示的小组件,与□所在位置相反*/
secondary: Icon(Icons.flag),
/*调整复选框和图标的位置*/
controlAffinity: ListTileControlAffinity.leading,
onChanged: (bool? value) {
setState(() {
isChecked = value!;
for(int i=0;i<flag.length;i++){
flag[i]=isChecked;
}
});
},
),
CheckboxListTile(
title: Text(select[0]),
subtitle: Text('堕胎小分队'),
secondary: Icon(Icons.info),
/*调整复选框和图标的位置*/
controlAffinity: ListTileControlAffinity.leading,
value: flag[0],
onChanged: (value) {
setState(() {
flag[0] = value;
});
}
),
CheckboxListTile(
title: Text(select[1]),
subtitle: Text('世兰好苦队'),
secondary: Icon(Icons.info),
/*调整复选框和图标的位置*/
controlAffinity: ListTileControlAffinity.leading,
value: flag[1],
onChanged: (value) {
setState(() {
flag[1] = value;
});
}
),
CheckboxListTile(
title: Text(select[2]),
subtitle: Text('莞莞类卿队'),
secondary: Icon(Icons.info),
/*调整复选框和图标的位置*/
controlAffinity: ListTileControlAffinity.leading,
value: flag[2],
onChanged: (value) {
setState(() {
flag[2] = value;
});
}
),
],
);
return Scaffold(
appBar: AppBar(
title: Text('用户注册'),
centerTitle: true,
),
body: column,
floatingActionButton: FloatingActionButton(
onPressed: () {
String info = "你选择的阵营是:";
for (int i = 0; i < flag.length; i++) {
if (flag[i]) {
/*如果选项被选中*/
info = info + select[i] + ' ';
}
}
print(info);
},
tooltip: 'Increment',
child: Icon(Icons.save),
),
floatingActionButtonLocation:
FloatingActionButtonLocation.centerFloat, //浮标的位置
);
}
}
边栏推荐
- Difference between unallocated blocks and unused blocks in database files
- [Android] Room - Alternative to SQLite
- Office automation case: how to automatically generate period data?
- Distributed locks and three implementation methods
- 10 Permission introduction
- VS QT - ui does not display newly added members (controls) || code is silent
- CorelDRAW2022 streamlined Asia Pacific new features in detail
- Moxa NPort 设备缺陷可能使关键基础设施遭受破坏性攻击
- SQL injection Less47 (error injection) and Less49 (time blind injection)
- 15. Website Statistics
猜你喜欢

Chapter 9 SVM实践

10. Redis implements likes (Set) and obtains the total number of likes

【异常】The field file exceeds its maximum permitted size of 1048576 bytes.

Mysql 45讲学习笔记(二十五)MYSQL保证高可用
![[C language] Preprocessing operation](/img/69/0aef065ae4061edaf0d96b89846bf2.png)
[C language] Preprocessing operation

Getting Started with CefSharp - winform

分布式与集群是什么 ? 区别是什么?
![[Android] Room - Alternative to SQLite](/img/52/0bc1c0a3173da6d39224ad8440a462.png)
[Android] Room - Alternative to SQLite

QML的使用

Graphical lower_bound & upper_bound
随机推荐
遗留系统的自动化策略
Redis实现分布式锁
SQL injection Less47 (error injection) and Less49 (time blind injection)
Point Cloud DBSCAN Clustering (MATLAB, not built-in function)
分布式锁以及实现方式三种
Mycat's master-slave relationship, vertical sub-database, horizontal sub-table, and detailed configuration of mycat fragmented table query (mysql5.7 series)
els 方块向左移动条件判断
测试中的误报和漏报同样的值得反复修正
12 Disk related commands
Unity3D Button mouse hover enter and mouse hover exit button events
PMP微信群日常习题
【C语言】预处理操作
Automation strategies for legacy systems
The els block moves the boundary to the right, and accelerates downward.
CorelDRAW2022 streamlined Asia Pacific new features in detail
TCP详解(三)
SQALE 是什么
TCP详解(一)
3.5 】 【 Cocos Creator slow operating system to stop all animations
分布式与集群是什么 ? 区别是什么?