zl程序教程

您现在的位置是:首页 >  移动开发

当前栏目

Flutter Switch开关【转】

flutter switch 开关
2023-09-27 14:25:31 时间
class _MyHomePageState extends State<MyHomePage> {
  bool flag = true;

  void change(bool value) {
    setState(() {
      flag = value;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Switch(
              value: flag, // 设置值,true开,false关
              onChanged: change,// 值变化时调用
              activeColor: Colors.red, // 为true时的颜色 圆圈颜色
              activeTrackColor:
                  Colors.green, // 为true 时,bar的颜色 如果不设置此值,bar的颜色为activeColor颜色
              inactiveThumbColor: Colors.blue, // 为false时 圆圈的颜色
              inactiveTrackColor: Colors.yellow, //为false时,bar的颜色。如果不设置此值,默认灰色
//              activeThumbImage,//为true时圆圈图片
//            inactiveThumbImage: ,//为false时圆圈图片
//            dragStartBehavior: DragStartBehavior.start,// 没发现效果
            ),
          ],
        ),
      ),
    );
  }
}

来源:https://www.jianshu.com/p/cadad2417f22