Last active
March 5, 2024 14:09
-
-
Save t-artikov/2c4fd13afde20aed0fadad0dc5db0694 to your computer and use it in GitHub Desktop.
Flutter color interpolation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:ui'; | |
import 'package:flutter/material.dart'; | |
Color mixColors(Color a, Color b, double t) { | |
final w1 = (1 - t) * a.opacity; | |
final w2 = t * b.opacity; | |
final n = w1 + w2; | |
final w = n > 0.000001 ? w2 / n : 0.5; | |
return Color.fromARGB( | |
lerpDouble(a.alpha, b.alpha, t).toInt().clamp(0, 255), | |
lerpDouble(a.red, b.red, w).toInt().clamp(0, 255), | |
lerpDouble(a.green, b.green, w).toInt().clamp(0, 255), | |
lerpDouble(a.blue, b.blue, w).toInt().clamp(0, 255), | |
); | |
} | |
class MyColorTween extends Tween<Color> { | |
MyColorTween({Color begin, Color end}) : super(begin: begin, end: end); | |
@override | |
Color lerp(double t) => mixColors(begin, end, t); | |
} | |
class MyAnimatedContainer extends StatelessWidget { | |
final Widget child; | |
final Color color; | |
final double width; | |
final double height; | |
final Duration duration; | |
const MyAnimatedContainer({ | |
this.child, | |
this.color, | |
this.width, | |
this.height, | |
this.duration, | |
}); | |
@override | |
Widget build(BuildContext context) { | |
return TweenAnimationBuilder( | |
child: child, | |
duration: duration, | |
tween: MyColorTween(begin: color, end: color), | |
builder: (context, color, child) { | |
return Container( | |
child: child, | |
color: color, | |
width: width, | |
height: height, | |
); | |
}, | |
); | |
} | |
} | |
//------------------------------------------------------------- | |
void main() => runApp(MyApp()); | |
class MyApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
title: 'Transparent Issue', | |
debugShowCheckedModeBanner: false, | |
theme: ThemeData( | |
primarySwatch: Colors.blue, | |
), | |
home: MyHomePage(title: 'Colors.transparent issue'), | |
); | |
} | |
} | |
class MyHomePage extends StatefulWidget { | |
MyHomePage({Key key, this.title}) : super(key: key); | |
final String title; | |
@override | |
_MyHomePageState createState() => _MyHomePageState(); | |
} | |
class _MyHomePageState extends State<MyHomePage> { | |
bool isColorSwap = true; | |
void _incrementCounter() { | |
setState(() { | |
isColorSwap = !isColorSwap; | |
}); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
backgroundColor: Colors.white, | |
appBar: AppBar( | |
title: Text(widget.title), | |
), | |
body: Column( | |
crossAxisAlignment: CrossAxisAlignment.stretch, | |
children: <Widget>[ | |
Expanded( | |
child: MyAnimatedContainer( | |
child: Center(child: Text('fade between white color')), | |
width: double.infinity, | |
duration: Duration(milliseconds: 500), | |
color: isColorSwap ? Colors.lightBlue[100] : Colors.white)), | |
Expanded( | |
child: MyAnimatedContainer( | |
child: | |
Center(child: Text('fade between transparent color')), | |
width: double.infinity, | |
duration: Duration(milliseconds: 500), | |
color: isColorSwap | |
? Colors.lightBlue[100] | |
: Colors.transparent)) | |
]), | |
floatingActionButton: FloatingActionButton( | |
onPressed: _incrementCounter, | |
tooltip: 'ClickMe', | |
child: Icon(Icons.add), | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment