Flutter 中有一个名为 Colors
的预定义的颜色类,它包含了一些常见颜色的常量值,方便我们在应用中使用。主要用法如下:
import 'package:flutter/material.dart';
Container(
color: Colors.blue, // 使用颜色常量
child: Text(
'Hello World',
style: TextStyle(
color: Colors.white, // 使用颜色常量
fontWeight: FontWeight.bold,
),
),
)
除了使用预定义颜色常量外,有时我们需要自己定义颜色。可以使用 Color
类来定义颜色。Color
类需要传入四种值,分别是红、绿、蓝、透明度(ARGB)。
// 自定义颜色
Color customColor = Color.fromRGBO(100, 150, 200, 0.5);
Container(
color: customColor, // 使用自定义颜色
child: Text(
'Hello World',
style: TextStyle(
color: Colors.white, // 使用颜色常量
fontWeight: FontWeight.bold,
),
),
)