Flutter学习第二课-基础组件 Switch

/*
  const Switch({
    Key key,
    @required this.value, //是否选中
    @required this.onChanged, //点击后回调
    this.activeColor,//选中时滑块颜色
    this.activeTrackColor,//选中时滑道颜色
    this.inactiveThumbColor,//未选中时滑块颜色
    this.inactiveTrackColor,//未选中时滑道颜色
    this.activeThumbImage,//选中时滑块图标
    this.inactiveThumbImage,//未选中滑块图标
    this.materialTapTargetSize,
    this.dragStartBehavior = DragStartBehavior.start,
    this.focusColor,
    this.hoverColor,
    this.focusNode,
    this.autofocus = false,
  })
    const SwitchListTile({
    Key key,
    @required this.value, //是否选中
    @required this.onChanged, //点击后回调
    this.activeColor,//选中时滑块颜色
    this.activeTrackColor,//选中时滑道颜色
    this.inactiveThumbColor,//未选中时滑块颜色
    this.inactiveTrackColor,//未选中时滑道颜色
    this.activeThumbImage,//选中时滑块图标
    this.inactiveThumbImage,//未选中滑块图标
    this.title, //正标题
    this.subtitle,//副标题
    this.isThreeLine = false,//开启时subtitle必须有值,当title未空时可以使subtitle 居中
    this.dense,
    this.contentPadding,//内边距缩进
    this.secondary,//左侧小图标
    this.selected = false,
  })
*/
import 'package:flutter/material.dart';

void main() => runApp(MyApp());

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

class _CounterState extends State<MyApp> {
  bool _switchSelected = true; //维护单选开关状态
  bool _switchTitleSelected = true; //维护单选开关状态

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: "Flutter Deam",
      color: Colors.white,
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Switch Sample'),
        ),
        body: Container(
          alignment: Alignment.center,
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              Switch(
                value: _switchSelected,
                activeColor: Colors.deepPurple,
                activeTrackColor: Colors.purple,
                activeThumbImage: NetworkImage(
                    "https://pcdn.flutterchina.club/imgs/3-17.png"),
                inactiveThumbColor: Colors.black,
                inactiveTrackColor: Colors.grey,
                inactiveThumbImage: NetworkImage(
                    "https://pcdn.flutterchina.club/imgs/3-17.png"),
                onChanged: (value) {
                  setState(() {
                    _switchSelected = value;
                  });
                },//选中是回调
              ),
              SizedBox(
                width: 300.0,
                child: SwitchListTile(
//                  isThreeLine:true,
                  contentPadding:EdgeInsets.all(30),
                  value: _switchTitleSelected,
//                  title: Text("title"),
                  subtitle: Text("subtitle"),
                  secondary: Image.network(
                      "https://pcdn.flutterchina.club/imgs/3-17.png"),
                  onChanged: (value) {
                    setState(() {
                      _switchTitleSelected = value;
                    });
                  },
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}

常用方法已经添加了注释,可复制代码直接运行看效果

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值