Switch组件常用的属性:

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

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

 

 

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 ? "选中" : "未选中"}")
        ],
      )
    );
  }
}

 

相关文章:

  • 2021-07-31
  • 2022-12-23
  • 2021-05-31
  • 2022-12-23
  • 2021-06-13
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-11-03
  • 2022-12-23
相关资源
相似解决方案