Skip to content

Instantly share code, notes, and snippets.

@NearHuscarl
Last active March 7, 2021 08:21
Show Gist options
  • Save NearHuscarl/195907b36fef5aefbf8682133b95c01d to your computer and use it in GitHub Desktop.
Save NearHuscarl/195907b36fef5aefbf8682133b95c01d to your computer and use it in GitHub Desktop.
51686868/gradient-text-in-flutter
// https://gist.github.com/NearHuscarl/195907b36fef5aefbf8682133b95c01d
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
debugShowCheckedModeBanner: false,
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
final String title;
MyHomePage({Key? key, required this.title}) : super(key: key);
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
final veryDarkRed = darken(Colors.red, .4);
final darkRed = darken(Colors.red, .2);
final red = Colors.red;
final lightRed = lighten(Colors.red);
final veryLightRed = lighten(Colors.red, .3);
return Scaffold(
body: SizedBox.expand(
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
GradientText(
'Hello Flutter',
gradient: LinearGradient(colors: [
Colors.blue.shade400,
Colors.blue.shade900,
]),
),
GradientText(
'Rainbow',
gradient: LinearGradient(colors: [
Colors.red,
Colors.pink,
Colors.purple,
Colors.deepPurple,
Colors.deepPurple,
Colors.indigo,
Colors.blue,
Colors.lightBlue,
Colors.cyan,
Colors.teal,
Colors.green,
Colors.lightGreen,
Colors.lime,
Colors.yellow,
Colors.amber,
Colors.orange,
Colors.deepOrange,
]),
),
GradientText(
'Fade out',
gradient: LinearGradient(
colors: [
Colors.black,
Colors.white,
],
begin: Alignment.topLeft,
end: Alignment.bottomRight,
),
),
],
),
),
);
}
}
class GradientText extends StatelessWidget {
GradientText(
this.text, {
@required this.gradient,
});
final String text;
final Gradient gradient;
@override
Widget build(BuildContext context) {
return ShaderMask(
shaderCallback: (bounds) => gradient.createShader(
Rect.fromLTWH(0, 0, bounds.width, bounds.height),
),
child: Text(
text,
style: TextStyle(
// The color must be set to white for this to work
color: Colors.white,
fontSize: 40,
),
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment