Flutter——Switch组件(开关组件)

Switch组件常用的属性:

属性 描述
value 单选的值
onChanged 改变时触发
activeColor 选中的颜色、背景颜色

import 'package:flutter/material.dart';

void main() {
  runApp(MaterialApp(
    title: "Switch",
    home: MyApp(),
  ));
}


class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}


class _MyAppState extends State<MyApp> {
  bool flag = false;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text("Switch")),
      body: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
          Switch(
            value: this.flag,
            activeColor: Colors.red,
            onChanged: (value) {
              setState(() {
                this.flag = value;
              });
            },
          ),
          Text("此时的状态是${this.flag == true ? "选中" : "未选中"}")
        ],
      )
    );
  }
}
原文地址:https://www.cnblogs.com/chichung/p/12021182.html