写一个LinearGradient示例,带注释.从方向到渐变颜色
时间: 2024-05-04 16:15:35 浏览: 10
好的,以下是一个LinearGradient示例:
```
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(title: Text('LinearGradient示例')),
body: Center(
child: Container(
decoration: BoxDecoration(
gradient: LinearGradient(
begin: Alignment.topCenter, // 渐变开始方向为顶部中心
end: Alignment.bottomCenter, // 渐变结束方向为底部中心
colors: [Colors.blue, Colors.green], // 渐变颜色为蓝色和绿色
)
),
child: Text('Hello World!', style: TextStyle(fontSize: 30, color: Colors.white)),
),
),
),
);
}
}
```
这个示例展示了一个渐变背景和一个白色文本的组合。LinearGradient类指定了渐变的方向和颜色,从而创建了一个线性渐变。在这个示例中,渐变从屏幕顶部中心开始到底部中心结束,颜色从蓝色变为绿色。