在应用程序中,当您点击屏幕时,需要将颜色更改为随机,但必须是RGB格式。如何获得随机的 RGB 颜色?
class _HomePageState extends State<HomePage> {
Color _color = Colors.white;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text("Color picker"),
centerTitle: true,
),
body: GestureDetector(
onTap: () {
setState(() {
_color = ...
});
},
child: Container(
color: _color,
padding: const EdgeInsets.all(20),
),
),
);
}
}Run Code Online (Sandbox Code Playgroud)
你可以这样做
class _HomePageState extends State<HomePage> {
Random random = Random();
Color _color = Colors.white;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text("Color picker"),
centerTitle: true,
),
body: GestureDetector(
onTap: () {
setState(() {
_color = Color.fromRGBO(
random.nextInt(255),
random.nextInt(255),
random.nextInt(255),
1,
);
});
},
child: Container(
color: _color,
padding: const EdgeInsets.all(20),
),
),
);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
326 次 |
| 最近记录: |