Flutter自定义开关组件插件custom_switch的使用
custom_switch
Beautiful Custom Switch package created with Flutter.
该源代码为 100% Dart,所有代码位于 /lib
文件夹中。
支持项目
💻 安装
在 pubspec.yaml
的 dependencies:
部分添加以下行:
custom_switch: <latest_version>
然后在项目中导入:
import 'package:custom_switch/custom_switch.dart';
❔ 基本用法
以下是一个简单的示例,展示如何使用 custom_switch
插件:
class HomeScreen extends StatefulWidget {
@override
_HomeScreenState createState() => _HomeScreenState();
}
class _HomeScreenState extends State<HomeScreen> {
// 控制开关状态的布尔值
bool status = false;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Custom Switch Example'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
// 自定义开关组件
CustomSwitch(
// 开关开启时的颜色
activeColor: Colors.pinkAccent,
// 当前开关的状态
value: status,
// 状态改变时的回调函数
onChanged: (value) {
print("VALUE : $value");
// 更新状态
setState(() {
status = value;
});
},
),
// 添加间距
SizedBox(height: 12.0,),
// 显示当前开关状态的文本
Text('Value : $status', style: TextStyle(
color: Colors.black,
fontSize: 20.0
),)
],
),
),
);
}
}
效果图
👨 开发者
Mohak Gupta




👍 如何贡献
- Fork 该项目。
- 创建一个新的分支(例如:
git checkout -b my-new-feature
)。 - 提交更改(例如:
git commit -am 'Add some feature'
)。 - 推送到新分支(例如:
git push origin my-new-feature
)。 - 创建新的 Pull Request。
📃 许可证
Copyright (c) 2019 Mohak Gupta
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
更多关于Flutter自定义开关组件插件custom_switch的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html
1 回复
更多关于Flutter自定义开关组件插件custom_switch的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
在Flutter中,你可以使用自定义开关组件插件 custom_switch
来创建自定义的开关控件。以下是如何使用 custom_switch
插件的步骤:
1. 添加依赖
首先,你需要在 pubspec.yaml
文件中添加 custom_switch
插件的依赖:
dependencies:
flutter:
sdk: flutter
custom_switch: ^1.0.0 # 请确保使用最新版本
然后运行 flutter pub get
来获取依赖。
2. 导入包
在你的 Dart 文件中导入 custom_switch
包:
import 'package:custom_switch/custom_switch.dart';
3. 使用 CustomSwitch
组件
你可以在你的 widget 树中使用 CustomSwitch
组件。以下是一个简单的示例:
import 'package:flutter/material.dart';
import 'package:custom_switch/custom_switch.dart';
class MyHomePage extends StatefulWidget {
[@override](/user/override)
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
bool _switchValue = false;
[@override](/user/override)
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Custom Switch Example'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
CustomSwitch(
value: _switchValue,
onChanged: (value) {
setState(() {
_switchValue = value;
});
},
activeColor: Colors.green, // 自定义开启时的颜色
inactiveColor: Colors.red, // 自定义关闭时的颜色
activeText: 'ON', // 自定义开启时的文本
inactiveText: 'OFF', // 自定义关闭时的文本
),
SizedBox(height: 20),
Text(
'Switch is ${_switchValue ? 'ON' : 'OFF'}',
style: TextStyle(fontSize: 20),
),
],
),
),
);
}
}
void main() => runApp(MaterialApp(
home: MyHomePage(),
));