Flutter框架跨平台鸿蒙开发——Container渐变效果
Container组件通过BoxDecoration提供了强大的渐变效果支持,可以实现线性渐变、径向渐变、扫描渐变等多种视觉效果。本文将深入探讨Container的渐变效果,包括渐变类型、颜色配置、角度控制、动画渐变以及实际应用场景。
·

Container组件通过BoxDecoration提供了强大的渐变效果支持,可以实现线性渐变、径向渐变、扫描渐变等多种视觉效果。本文将深入探讨Container的渐变效果,包括渐变类型、颜色配置、角度控制、动画渐变以及实际应用场景。
一、渐变类型
Flutter提供了三种主要的渐变类型,每种类型都有其独特的应用场景。
1.1 渐变类型对比
1.2 渐变特性对比
| 渐变类型 | 定义方式 | 应用场景 | 复杂度 | 性能 |
|---|---|---|---|---|
| LinearGradient | 直线方向 | 背景、按钮 | 简单 | 好 |
| RadialGradient | 圆形辐射 | 强调中心 | 中等 | 中 |
| SweepGradient | 圆环旋转 | 进度环、图表 | 较难 | 中 |
二、线性渐变
2.1 基础线性渐变
class BasicLinearGradient extends StatelessWidget {
const BasicLinearGradient({super.key});
Widget build(BuildContext context) {
return Column(
children: [
// 水平渐变
Container(
height: 80,
decoration: BoxDecoration(
gradient: const LinearGradient(
colors: [Colors.blue, Colors.purple],
),
),
child: const Center(
child: Text(
'水平渐变',
style: TextStyle(color: Colors.white, fontSize: 18),
),
),
),
const SizedBox(height: 16),
// 垂直渐变
Container(
height: 80,
decoration: BoxDecoration(
gradient: const LinearGradient(
begin: Alignment.topCenter,
end: Alignment.bottomCenter,
colors: [Colors.green, Colors.teal],
),
),
child: const Center(
child: Text(
'垂直渐变',
style: TextStyle(color: Colors.white, fontSize: 18),
),
),
),
const SizedBox(height: 16),
// 对角渐变
Container(
height: 80,
decoration: BoxDecoration(
gradient: const LinearGradient(
begin: Alignment.topLeft,
end: Alignment.bottomRight,
colors: [Colors.orange, Colors.red],
),
),
child: const Center(
child: Text(
'对角渐变',
style: TextStyle(color: Colors.white, fontSize: 18),
),
),
),
],
);
}
}
2.2 多色渐变
class MultiColorGradient extends StatelessWidget {
const MultiColorGradient({super.key});
Widget build(BuildContext context) {
return Container(
height: 120,
decoration: BoxDecoration(
gradient: LinearGradient(
colors: [
Colors.red,
Colors.orange,
Colors.yellow,
Colors.green,
Colors.blue,
Colors.purple,
],
stops: const [0.0, 0.2, 0.4, 0.6, 0.8, 1.0],
),
),
child: const Center(
child: Text(
'彩虹渐变',
style: TextStyle(
color: Colors.white,
fontSize: 24,
fontWeight: FontWeight.bold,
),
),
),
);
}
}
2.3 渐变角度控制
class AngleGradient extends StatelessWidget {
const AngleGradient({super.key});
Widget build(BuildContext context) {
return Wrap(
spacing: 16,
runSpacing: 16,
children: [
_buildAngleGradient(0, '0°'),
_buildAngleGradient(45, '45°'),
_buildAngleGradient(90, '90°'),
_buildAngleGradient(135, '135°'),
_buildAngleGradient(180, '180°'),
_buildAngleGradient(270, '270°'),
],
);
}
Widget _buildAngleGradient(double angle, String label) {
return Container(
width: 80,
height: 80,
decoration: BoxDecoration(
gradient: LinearGradient(
begin: Alignment.centerLeft,
end: Alignment.centerRight,
colors: [Colors.blue, Colors.purple],
transform: GradientRotation(angle * 3.14159 / 180),
),
borderRadius: BorderRadius.circular(8),
),
child: Center(
child: Text(
label,
style: const TextStyle(
color: Colors.white,
fontWeight: FontWeight.bold,
),
),
),
);
}
}
三、径向渐变
3.1 基础径向渐变
class BasicRadialGradient extends StatelessWidget {
const BasicRadialGradient({super.key});
Widget build(BuildContext context) {
return Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
// 中心扩散
Container(
width: 100,
height: 100,
decoration: BoxDecoration(
gradient: const RadialGradient(
colors: [Colors.blue, Colors.transparent],
),
shape: BoxShape.circle,
),
child: const Center(
child: Text(
'中心扩散',
style: TextStyle(fontSize: 12),
textAlign: TextAlign.center,
),
),
),
// 自定义中心
Container(
width: 100,
height: 100,
decoration: BoxDecoration(
gradient: RadialGradient(
center: const Alignment(-0.5, -0.5),
colors: [Colors.red, Colors.white],
),
),
),
// 多色径向
Container(
width: 100,
height: 100,
decoration: BoxDecoration(
gradient: const RadialGradient(
colors: [Colors.yellow, Colors.orange, Colors.red],
stops: [0.0, 0.5, 1.0],
),
shape: BoxShape.circle,
),
),
],
);
}
}
3.2 径向渐变半径
class RadialGradientRadius extends StatelessWidget {
const RadialGradientRadius({super.key});
Widget build(BuildContext context) {
return Wrap(
spacing: 16,
children: [
_buildRadiusGradient(0.5, '半径50%'),
_buildRadiusGradient(0.8, '半径80%'),
_buildRadiusGradient(1.0, '半径100%'),
],
);
}
Widget _buildRadiusGradient(double radius, String label) {
return Container(
width: 100,
height: 100,
decoration: BoxDecoration(
gradient: RadialGradient(
radius: radius,
colors: [Colors.purple.shade200, Colors.purple.shade600],
),
shape: BoxShape.circle,
),
child: Center(
child: Text(
label,
style: const TextStyle(
color: Colors.white,
fontSize: 12,
fontWeight: FontWeight.bold,
),
),
),
);
}
}
3.3 焦点控制
class RadialGradientFocal extends StatelessWidget {
const RadialGradientFocal({super.key});
Widget build(BuildContext context) {
return Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
// 焦点在中心
Container(
width: 100,
height: 100,
decoration: BoxDecoration(
gradient: RadialGradient(
focal: Alignment.center,
colors: [Colors.yellow, Colors.orange],
),
shape: BoxShape.circle,
),
),
// 焦点在左上
Container(
width: 100,
height: 100,
decoration: BoxDecoration(
gradient: RadialGradient(
focal: const Alignment(-0.5, -0.5),
focalRadius: 0.3,
colors: [Colors.green, Colors.teal],
),
shape: BoxShape.circle,
),
),
// 焦点在右下
Container(
width: 100,
height: 100,
decoration: BoxDecoration(
gradient: RadialGradient(
focal: const Alignment(0.5, 0.5),
focalRadius: 0.3,
colors: [Colors.blue, Colors.indigo],
),
shape: BoxShape.circle,
),
),
],
);
}
}
四、扫描渐变
4.1 基础扫描渐变
class BasicSweepGradient extends StatelessWidget {
const BasicSweepGradient({super.key});
Widget build(BuildContext context) {
return Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
// 彩虹扫描
Container(
width: 100,
height: 100,
decoration: BoxDecoration(
gradient: const SweepGradient(
colors: [
Colors.red,
Colors.yellow,
Colors.green,
Colors.blue,
Colors.red,
],
),
shape: BoxShape.circle,
),
),
// 蓝绿扫描
Container(
width: 100,
height: 100,
decoration: BoxDecoration(
gradient: const SweepGradient(
colors: [Colors.blue, Colors.green, Colors.blue],
stops: [0.0, 0.5, 1.0],
),
shape: BoxShape.circle,
),
),
// 自定义起始角度
Container(
width: 100,
height: 100,
decoration: BoxDecoration(
gradient: SweepGradient(
startAngle: 0.5,
endAngle: 2.5,
colors: [
Colors.purple,
Colors.pink,
Colors.purple,
],
),
shape: BoxShape.circle,
),
),
],
);
}
}
4.2 扫描渐变应用
class SweepGradientApps extends StatelessWidget {
const SweepGradientApps({super.key});
Widget build(BuildContext context) {
return Wrap(
spacing: 16,
children: [
// 进度环
Container(
width: 100,
height: 100,
decoration: BoxDecoration(
gradient: SweepGradient(
colors: [
Colors.blue.shade200,
Colors.blue.shade600,
Colors.blue.shade200,
],
stops: const [0.0, 0.7, 0.7],
),
shape: BoxShape.circle,
),
child: const Center(
child: Text(
'70%',
style: TextStyle(
fontSize: 24,
fontWeight: FontWeight.bold,
color: Colors.blue,
),
),
),
),
// 仪表盘
Container(
width: 100,
height: 100,
decoration: BoxDecoration(
gradient: SweepGradient(
startAngle: 3.14,
endAngle: 5.0,
colors: [
Colors.green.shade200,
Colors.green.shade600,
Colors.green.shade200,
],
stops: const [0.0, 0.6, 0.6],
),
shape: BoxShape.circle,
),
child: const Center(
child: Text(
'60',
style: TextStyle(
fontSize: 24,
fontWeight: FontWeight.bold,
color: Colors.green,
),
),
),
),
// 加载动画
Container(
width: 100,
height: 100,
decoration: BoxDecoration(
gradient: SweepGradient(
center: Alignment.center,
startAngle: 0.0,
endAngle: 5.0,
colors: const [
Colors.transparent,
Colors.orange,
Colors.transparent,
],
tileMode: TileMode.repeated,
),
shape: BoxShape.circle,
),
),
],
);
}
}
五、渐变动画
5.1 渐变颜色动画
class AnimatedGradientContainer extends StatefulWidget {
const AnimatedGradientContainer({super.key});
State<AnimatedGradientContainer> createState() => _AnimatedGradientContainerState();
}
class _AnimatedGradientContainerState extends State<AnimatedGradientContainer>
with SingleTickerProviderStateMixin {
late AnimationController _controller;
late Animation<double> _animation;
void initState() {
super.initState();
_controller = AnimationController(
duration: const Duration(seconds: 5),
vsync: this,
)..repeat();
_animation = Tween<double>(begin: 0, end: 1).animate(_controller);
}
void dispose() {
_controller.dispose();
super.dispose();
}
Widget build(BuildContext context) {
return AnimatedBuilder(
animation: _animation,
builder: (context, child) {
return Container(
width: 200,
height: 100,
decoration: BoxDecoration(
gradient: LinearGradient(
colors: [
Color.lerp(Colors.blue, Colors.purple, _animation.value)!,
Color.lerp(Colors.purple, Colors.pink, _animation.value)!,
],
),
borderRadius: BorderRadius.circular(12),
),
child: const Center(
child: Text(
'颜色动画',
style: TextStyle(
color: Colors.white,
fontSize: 20,
fontWeight: FontWeight.bold,
),
),
),
);
},
);
}
}
5.2 渐变角度动画
class RotatingGradientContainer extends StatefulWidget {
const RotatingGradientContainer({super.key});
State<RotatingGradientContainer> createState() => _RotatingGradientContainerState();
}
class _RotatingGradientContainerState extends State<RotatingGradientContainer>
with SingleTickerProviderStateMixin {
late AnimationController _controller;
late Animation<double> _animation;
void initState() {
super.initState();
_controller = AnimationController(
duration: const Duration(seconds: 3),
vsync: this,
)..repeat();
_animation = Tween<double>(begin: 0, end: 2 * 3.14159).animate(_controller);
}
void dispose() {
_controller.dispose();
super.dispose();
}
Widget build(BuildContext context) {
return AnimatedBuilder(
animation: _animation,
builder: (context, child) {
return Container(
width: 150,
height: 150,
decoration: BoxDecoration(
gradient: LinearGradient(
begin: Alignment.centerLeft,
end: Alignment.centerRight,
colors: [Colors.orange, Colors.red],
transform: GradientRotation(_animation.value),
),
shape: BoxShape.circle,
),
child: const Center(
child: Text(
'旋转渐变',
style: TextStyle(
color: Colors.white,
fontWeight: FontWeight.bold,
),
),
),
);
},
);
}
}
5.3 呼吸渐变
class BreathingGradientContainer extends StatefulWidget {
const BreathingGradientContainer({super.key});
State<BreathingGradientContainer> createState() => _BreathingGradientContainerState();
}
class _BreathingGradientContainerState extends State<BreathingGradientContainer>
with SingleTickerProviderStateMixin {
late AnimationController _controller;
late Animation<double> _animation;
void initState() {
super.initState();
_controller = AnimationController(
duration: const Duration(seconds: 3),
vsync: this,
)..repeat(reverse: true);
_animation = Tween<double>(begin: 0.0, end: 1.0).animate(
CurvedAnimation(
parent: _controller,
curve: Curves.easeInOut,
),
);
}
void dispose() {
_controller.dispose();
super.dispose();
}
Widget build(BuildContext context) {
return AnimatedBuilder(
animation: _animation,
builder: (context, child) {
return Container(
width: 200,
height: 100,
decoration: BoxDecoration(
gradient: RadialGradient(
colors: [
Color.lerp(Colors.green.shade200, Colors.green.shade600, _animation.value)!,
Colors.white,
],
),
borderRadius: BorderRadius.circular(12),
),
child: const Center(
child: Text(
'呼吸渐变',
style: TextStyle(
fontSize: 20,
fontWeight: FontWeight.bold,
),
),
),
);
},
);
}
}
六、渐变与圆角结合
6.1 圆角渐变
class RoundedGradientContainer extends StatelessWidget {
const RoundedGradientContainer({super.key});
Widget build(BuildContext context) {
return Wrap(
spacing: 16,
runSpacing: 16,
children: [
// 小圆角
Container(
width: 120,
height: 80,
decoration: BoxDecoration(
gradient: const LinearGradient(
colors: [Colors.blue, Colors.purple],
),
borderRadius: BorderRadius.circular(8),
),
child: const Center(
child: Text(
'小圆角',
style: TextStyle(color: Colors.white, fontWeight: FontWeight.bold),
),
),
),
// 中圆角
Container(
width: 120,
height: 80,
decoration: BoxDecoration(
gradient: const LinearGradient(
colors: [Colors.green, Colors.teal],
),
borderRadius: BorderRadius.circular(16),
),
child: const Center(
child: Text(
'中圆角',
style: TextStyle(color: Colors.white, fontWeight: FontWeight.bold),
),
),
),
// 大圆角
Container(
width: 120,
height: 80,
decoration: BoxDecoration(
gradient: const LinearGradient(
colors: [Colors.orange, Colors.red],
),
borderRadius: BorderRadius.circular(24),
),
child: const Center(
child: Text(
'大圆角',
style: TextStyle(color: Colors.white, fontWeight: FontWeight.bold),
),
),
),
// 完全圆角
Container(
width: 80,
height: 80,
decoration: BoxDecoration(
gradient: const LinearGradient(
colors: [Colors.pink, Colors.purple],
),
shape: BoxShape.circle,
),
child: const Center(
child: Text(
'圆形',
style: TextStyle(
color: Colors.white,
fontSize: 12,
fontWeight: FontWeight.bold,
),
),
),
),
],
);
}
}
6.2 不规则圆角
class IrregularRoundedGradient extends StatelessWidget {
const IrregularRoundedGradient({super.key});
Widget build(BuildContext context) {
return Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
// 只有左上圆角
Container(
width: 100,
height: 80,
decoration: BoxDecoration(
gradient: const LinearGradient(
colors: [Colors.blue, Colors.purple],
),
borderRadius: const BorderRadius.only(
topLeft: Radius.circular(20),
),
),
),
// 对角圆角
Container(
width: 100,
height: 80,
decoration: BoxDecoration(
gradient: const LinearGradient(
colors: [Colors.green, Colors.teal],
),
borderRadius: const BorderRadius.only(
topLeft: Radius.circular(20),
bottomRight: Radius.circular(20),
),
),
),
// 顶部圆角
Container(
width: 100,
height: 80,
decoration: BoxDecoration(
gradient: const LinearGradient(
colors: [Colors.orange, Colors.red],
),
borderRadius: const BorderRadius.only(
topRight: Radius.circular(20),
topLeft: Radius.circular(20),
),
),
),
],
);
}
}
七、渐变应用场景
7.1 按钮渐变
class GradientButton extends StatelessWidget {
final String text;
final Gradient gradient;
const GradientButton({
super.key,
required this.text,
required this.gradient,
});
Widget build(BuildContext context) {
return InkWell(
onTap: () {
print('$text 被点击');
},
child: Container(
padding: const EdgeInsets.symmetric(horizontal: 24, vertical: 12),
decoration: BoxDecoration(
gradient: gradient,
borderRadius: BorderRadius.circular(24),
boxShadow: [
BoxShadow(
color: gradient.colors.first.withOpacity(0.3),
blurRadius: 8,
offset: const Offset(0, 4),
),
],
),
child: Text(
text,
style: const TextStyle(
color: Colors.white,
fontSize: 16,
fontWeight: FontWeight.bold,
),
),
),
);
}
}
class GradientButtonsExample extends StatelessWidget {
const GradientButtonsExample({super.key});
Widget build(BuildContext context) {
return Wrap(
spacing: 16,
children: [
GradientButton(
text: '蓝色按钮',
gradient: const LinearGradient(
colors: [Colors.blue, Colors.blue.shade700],
),
),
GradientButton(
text: '绿色按钮',
gradient: const LinearGradient(
colors: [Colors.green, Colors.green.shade700],
),
),
GradientButton(
text: '紫色按钮',
gradient: const LinearGradient(
colors: [Colors.purple, Colors.purple.shade700],
),
),
],
);
}
}
7.2 卡片渐变
class GradientCard extends StatelessWidget {
final String title;
final String subtitle;
final Gradient gradient;
const GradientCard({
super.key,
required this.title,
required this.subtitle,
required this.gradient,
});
Widget build(BuildContext context) {
return Container(
width: 200,
padding: const EdgeInsets.all(20),
decoration: BoxDecoration(
gradient: gradient,
borderRadius: BorderRadius.circular(16),
boxShadow: [
BoxShadow(
color: gradient.colors.first.withOpacity(0.3),
blurRadius: 12,
offset: const Offset(0, 6),
),
],
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
title,
style: const TextStyle(
fontSize: 20,
fontWeight: FontWeight.bold,
color: Colors.white,
),
),
const SizedBox(height: 8),
Text(
subtitle,
style: TextStyle(
fontSize: 14,
color: Colors.white.withOpacity(0.9),
),
),
],
),
);
}
}
class GradientCardsExample extends StatelessWidget {
const GradientCardsExample({super.key});
Widget build(BuildContext context) {
return Wrap(
spacing: 16,
runSpacing: 16,
children: [
GradientCard(
title: '日出',
subtitle: '清晨的第一缕阳光',
gradient: LinearGradient(
colors: [Colors.orange.shade300, Colors.red.shade600],
begin: Alignment.topLeft,
end: Alignment.bottomRight,
),
),
GradientCard(
title: '海洋',
subtitle: '深邃的蓝色海洋',
gradient: LinearGradient(
colors: [Colors.blue.shade300, Colors.blue.shade800],
begin: Alignment.topLeft,
end: Alignment.bottomRight,
),
),
],
);
}
}
八、完整渐变示例
class ContainerGradientExample extends StatelessWidget {
const ContainerGradientExample({super.key});
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Container渐变效果'),
backgroundColor: Colors.blue,
foregroundColor: Colors.white,
),
body: ListView(
padding: const EdgeInsets.all(16),
children: [
const Text(
'线性渐变',
style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold),
),
const SizedBox(height: 12),
const BasicLinearGradient(),
const SizedBox(height: 16),
const MultiColorGradient(),
const SizedBox(height: 16),
const AngleGradient(),
const SizedBox(height: 24),
const Text(
'径向渐变',
style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold),
),
const SizedBox(height: 12),
const BasicRadialGradient(),
const SizedBox(height: 16),
const RadialGradientRadius(),
const SizedBox(height: 16),
const RadialGradientFocal(),
const SizedBox(height: 24),
const Text(
'扫描渐变',
style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold),
),
const SizedBox(height: 12),
const BasicSweepGradient(),
const SizedBox(height: 16),
const SweepGradientApps(),
const SizedBox(height: 24),
const Text(
'渐变动画',
style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold),
),
const SizedBox(height: 12),
const AnimatedGradientContainer(),
const SizedBox(height: 16),
const RotatingGradientContainer(),
const SizedBox(height: 16),
const BreathingGradientContainer(),
const SizedBox(height: 24),
const Text(
'圆角渐变',
style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold),
),
const SizedBox(height: 12),
const RoundedGradientContainer(),
const SizedBox(height: 16),
const IrregularRoundedGradient(),
const SizedBox(height: 24),
const Text(
'应用场景',
style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold),
),
const SizedBox(height: 12),
const GradientButtonsExample(),
const SizedBox(height: 16),
const GradientCardsExample(),
],
),
);
}
}
欢迎加入开源鸿蒙跨平台社区:https://openharmonycrossplatform.csdn.net
更多推荐



所有评论(0)