/*
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;
});
},
),
),
],
),
),
),
);
}
}
常用方法已经添加了注释,可复制代码直接运行看效果