Flutter颜色选择器插件flutter_material_color_picker的使用
Flutter颜色选择器插件flutter_material_color_picker的使用
Flutter Material Color Picker
Material Color picker 是一个可定制化的Flutter小部件。默认情况下,它是Material Colors,但你可以定义自己的颜色。你还可以使用 CircleColor
小部件在应用中显示颜色。例如,你可以将颜色选择器设置在一个对话框中,并在 ListTile
中显示所选颜色,用于设置。
如何使用它
添加到你的Flutter项目
只需将 flutter_material_color_picker
作为依赖项添加到你的 pubspec.yaml
文件中。
dependencies:
flutter_material_color_picker: ^1.2.0
导入
在Dart文件中导入包:
import 'package:flutter_material_color_picker/flutter_material_color_picker.dart';
基本用法
下面是一个简单的 MaterialColorPicker
示例:
MaterialColorPicker(
onColorChange: (Color color) {
// 处理颜色变化
},
selectedColor: Colors.red
)
监听主色变化
你可以监听主色的变化:
MaterialColorPicker(
onColorChange: (Color color) {
// 处理颜色变化
},
onMainColorChange: (ColorSwatch color) {
// 处理主色变化
},
selectedColor: Colors.red
)
禁用色调
如果你只想显示和允许选择主色,可以禁用色调:
MaterialColorPicker(
allowShades: false, // 默认为true
onMainColorChange: (ColorSwatch color) {
// 处理主色变化
},
selectedColor: Colors.red
)
自定义颜色
你可以定义自定义颜色列表:
MaterialColorPicker(
onColorChange: (Color color) {
// 处理颜色变化
},
selectedColor: Colors.red,
colors: [
Colors.red,
Colors.deepOrange,
Colors.yellow,
Colors.lightGreen
],
)
示例代码
以下是一个完整的示例应用程序,展示了如何在不同的场景中使用 MaterialColorPicker
。
import 'package:flutter/material.dart';
import 'package:flutter_material_color_picker/flutter_material_color_picker.dart';
void main() => runApp(const App());
class App extends StatelessWidget {
const App({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: "Material Color Picker",
theme: ThemeData(primarySwatch: Colors.blue),
home: const HomeScreen(),
);
}
}
class HomeScreen extends StatefulWidget {
const HomeScreen({super.key});
@override
HomeScreenState createState() => HomeScreenState();
}
class HomeScreenState extends State<HomeScreen> {
// 使用临时变量以仅在按下对话框的“提交”按钮时更新颜色
ColorSwatch? _tempMainColor;
Color? _tempShadeColor;
ColorSwatch? _mainColor = Colors.blue;
Color? _shadeColor = Colors.blue[800];
void _openDialog(String title, Widget content) {
showDialog(
context: context,
builder: (_) {
return AlertDialog(
contentPadding: const EdgeInsets.all(18.0),
title: Text(title),
content: content,
actions: [
TextButton(
onPressed: Navigator.of(context).pop,
child: const Text('CANCEL'),
),
TextButton(
onPressed: () {
Navigator.of(context).pop();
setState(() => _mainColor = _tempMainColor);
setState(() => _shadeColor = _tempShadeColor);
},
child: const Text('SUBMIT'),
),
],
);
},
);
}
void _openColorPicker() async {
_openDialog(
"Color picker",
MaterialColorPicker(
selectedColor: _shadeColor,
onColorChange: (color) => setState(() => _tempShadeColor = color),
onMainColorChange: (color) => setState(() => _tempMainColor = color),
onBack: () => print("Back button pressed"),
),
);
}
void _openMainColorPicker() async {
_openDialog(
"Main Color picker",
MaterialColorPicker(
selectedColor: _mainColor,
allowShades: false,
onMainColorChange: (color) => setState(() => _tempMainColor = color),
),
);
}
void _openAccentColorPicker() async {
_openDialog(
"Accent Color picker",
MaterialColorPicker(
colors: accentColors,
selectedColor: _mainColor,
onMainColorChange: (color) => setState(() => _tempMainColor = color),
circleSize: 40.0,
spacing: 10,
),
);
}
void _openFullMaterialColorPicker() async {
_openDialog(
"Full Material Color picker",
MaterialColorPicker(
colors: fullMaterialColors,
selectedColor: _mainColor,
onMainColorChange: (color) => setState(() => _tempMainColor = color),
),
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
child: SingleChildScrollView(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
"Material color picker",
style: Theme.of(context).textTheme.headlineSmall,
),
const SizedBox(height: 62.0),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
CircleAvatar(
backgroundColor: _mainColor,
radius: 35.0,
child: const Text("MAIN"),
),
const SizedBox(width: 16.0),
CircleAvatar(
backgroundColor: _shadeColor,
radius: 35.0,
child: const Text("SHADE"),
),
],
),
const SizedBox(height: 32.0),
OutlinedButton(
onPressed: _openColorPicker,
child: const Text('Show color picker'),
),
const SizedBox(height: 16.0),
OutlinedButton(
onPressed: _openMainColorPicker,
child: const Text('Show main color picker'),
),
const Text('(only main color)'),
const SizedBox(height: 16.0),
OutlinedButton(
onPressed: _openAccentColorPicker,
child: const Text('Show accent color picker'),
),
const SizedBox(height: 16.0),
OutlinedButton(
onPressed: _openFullMaterialColorPicker,
child: const Text('Show full material color picker'),
),
],
),
),
),
);
}
}
通过以上内容,你应该能够理解并使用 flutter_material_color_picker
插件来实现颜色选择功能。希望这对你有所帮助!
更多关于Flutter颜色选择器插件flutter_material_color_picker的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
更多关于Flutter颜色选择器插件flutter_material_color_picker的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
当然,以下是如何在Flutter应用中使用flutter_material_color_picker
插件的一个代码示例。这个插件允许你在Flutter应用中轻松实现一个颜色选择器。
首先,你需要在pubspec.yaml
文件中添加这个插件的依赖:
dependencies:
flutter:
sdk: flutter
flutter_material_color_picker: ^x.x.x # 请替换为最新版本号
然后,运行flutter pub get
来安装依赖。
接下来,在你的Dart文件中,你可以按照以下方式使用flutter_material_color_picker
:
import 'package:flutter/material.dart';
import 'package:flutter_material_color_picker/flutter_material_color_picker.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Color Picker Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: ColorPickerDemo(),
);
}
}
class ColorPickerDemo extends StatefulWidget {
@override
_ColorPickerDemoState createState() => _ColorPickerDemoState();
}
class _ColorPickerDemoState extends State<ColorPickerDemo> {
Color selectedColor = Colors.blue;
void _pickColor(BuildContext context) async {
final Color? newColor = await showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: Text('Select a color'),
content: SingleChildScrollView(
child: ColorPicker(
pickerColor: selectedColor,
onColorChanged: (Color color) {
setState(() {
selectedColor = color;
});
},
enableAlpha: true,
pickerAreaHeightPercent: 0.8,
),
),
actions: <Widget>[
TextButton(
child: Text('Cancel'),
onPressed: () {
Navigator.of(context).pop();
},
),
TextButton(
child: Text('OK'),
onPressed: () {
Navigator.of(context).pop(selectedColor);
},
),
],
);
},
);
if (newColor != null) {
setState(() {
selectedColor = newColor!;
});
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Color Picker Demo'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Container(
width: 100,
height: 100,
color: selectedColor,
),
SizedBox(height: 20),
ElevatedButton(
onPressed: () => _pickColor(context),
child: Text('Pick a color'),
),
],
),
),
);
}
}
在这个示例中,我们创建了一个简单的Flutter应用,其中包含一个颜色方块和一个按钮。点击按钮会打开一个对话框,对话框中包含一个颜色选择器。用户可以选择颜色,点击“OK”按钮后,颜色方块的颜色会更新为用户选择的颜色。
关键点:
- 使用
showDialog
函数显示一个包含ColorPicker
的对话框。 ColorPicker
组件的onColorChanged
回调用于更新当前选择的颜色。- 使用
setState
函数重新构建UI以反映颜色变化。
这个示例展示了如何集成和使用flutter_material_color_picker
插件,提供了一个直观的用户界面来选择颜色。