Flutter——Checkbox組件、CheckboxListTile(多選框組件)


  • 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;
          });
        },
      ),
    );
  }
}

 

 

 

 

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM