Skip to content

Instantly share code, notes, and snippets.

@jagomf
Last active December 16, 2021 11:03
Show Gist options
  • Save jagomf/9ddbad3f41f19701f946b54495ad27bd to your computer and use it in GitHub Desktop.
Save jagomf/9ddbad3f41f19701f946b54495ad27bd to your computer and use it in GitHub Desktop.
Getting a color in between a scale in Dart/Flutter
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
const totalColorsToShow = 50;
const startColor = Colors.red;
const finalColor = Colors.green;
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(title: const Text('Lerping colors')),
body: ListView(
children: List<double>.generate(totalColorsToShow, (i) => i / totalColorsToShow).map((e) {
final color = HSVColor.lerp(HSVColor.fromColor(startColor), HSVColor.fromColor(finalColor), e)?.toColor();
return ListTile(
title: Text('scale $e/1, ${color.toString()}'),
tileColor: color,
);
}).toList()
),
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment