Gradient Text in Flutter - text

I was wondering if it is possible to create a gradient over text
Flutter. There is a gist of text gradient using Dart's ui, but it is kinda long and I was hoping to be simpler.

You can use ShaderMask for that task. In ShaderMask, you need to set the BlendMode to BlendMode.srcIn, "src" means the widget to apply the gradient to (in this case Text), "in" means only show the part of the Text where it overlaps with the background which is the text itself (so the gradient doesn't get applied on the background):
import 'package:flutter/material.dart';
class GradientText extends StatelessWidget {
const GradientText(
this.text, {
required this.gradient,
this.style,
});
final String text;
final TextStyle? style;
final Gradient gradient;
#override
Widget build(BuildContext context) {
return ShaderMask(
blendMode: BlendMode.srcIn,
shaderCallback: (bounds) => gradient.createShader(
Rect.fromLTWH(0, 0, bounds.width, bounds.height),
),
child: Text(text, style: style),
);
}
}
Usage
GradientText(
'Hello Flutter',
style: const TextStyle(fontSize: 40),
gradient: LinearGradient(colors: [
Colors.blue.shade400,
Colors.blue.shade900,
]),
),
Live Demo

Taken from here, you can use Text's style painter.
Create the shader,
final Shader linearGradient = LinearGradient(
colors: <Color>[Color(0xffDA44bb), Color(0xff8921aa)],
).createShader(Rect.fromLTWH(0.0, 0.0, 200.0, 70.0));
then use it in the TextStyle's foreground
Text(
'Hello Gradients!',
style: new TextStyle(
fontSize: 60.0,
fontWeight: FontWeight.bold,
foreground: Paint()..shader = linearGradient),
)
Source code

Use simple_gradient_text package and create GradienText
GradientText(
'Gradient Text Example',
style: TextStyle(
fontSize: 40.0,
),
colors: [
Colors.blue,
Colors.red,
Colors.teal,
],
),

First, we import Pkg
Link For PKG
Radial gradient
GradientText(
'Gradient Text Example',
style: TextStyle(
fontSize: 40.0,
),
gradientType: GradientType.radial,
gradientDirection: GradientDirection.ttb,
radius: 6,
colors: [
Color(0xff159DFF),
Color(0xff002981),
],
),
Linear gradient
GradientText(
'Gradient Text Example',
style: TextStyle(
fontSize: 40.0,
),
gradientType: GradientType.linear,
gradientDirection: GradientDirection.ttb,
radius: .4,
colors: [
Color(0xff159DFF),
Color(0xff002981),
],
),

Related

Color for String, in dart

i think is a simple question but i don t kwon who solve this,
Is it possible to put this
String asterisk = '*';
in red?
i try, something like that
String asterisk = '*',
style: TextStyle(
color: Colors.red,
);
but this just work in
const Text(
"Registar",
style: TextStyle(
color: Colors.black,
fontWeight: FontWeight.bold,
fontSize: 40),
),
basically I need to put the * in front of the "text('register')" and I wanted the * to be red
String asterisk = '*', color: Colors.red;
const Text(
"Registar *",
style: TextStyle(
color: Colors.black,
fontWeight: FontWeight.bold,
fontSize: 40),
),
You can use the RichText widget to provide multiple styles in TextSpan widgets:
RichText(
text: const TextSpan(
children: [
TextSpan(text: '*', style: TextStyle(color: Colors.red)),
TextSpan(
text: 'Registar',
style: TextStyle(
color: Colors.black,
fontWeight: FontWeight.bold,
fontSize: 40,
),
),
],
),
)

How to split a String in Dart, Flutter

I am getting a String balance from the backend, and it is something like this 430000.
I would like to have an output like this 430,000.
How do I go about it?
Row(
children: [
Padding(
padding: const EdgeInsets.only(
left: 12,
),
child: Text(
"₦ ${user.availableBalance.toStringAsFixed(0)} ",
style: TextStyle(
color: Colors.white,
fontSize: 21.sp,
),
)),
],
),
The best way is to use Regex so define this on the top
class _HomeTabState extends State<HomeTab> {
//Regex
RegExp reg = RegExp(r'(\d{1,3})(?=(\d{3})+(?!\d))'); //immport dar:core
RegExp regex = RegExp(r'([.]*0)(?!.*\d)');
String Function(Match) mathFunc = (Match match) => '${match[1]},'; //match
...
//In your build
Widget build(BuildContext context) {
...
Row(
children: [
Padding(
padding: const EdgeInsets.only(
left: 12,
),
child: Text(
"₦ ${double.parse(user.availableBalance.toString()).toStringAsFixed(0)} " .replaceAllMapped(
reg, mathFunc),
style: TextStyle(
color: Colors.white,
fontSize: 21.sp,
),
)),
],
),
...
}
}
Then to use it just turn your balance into a double then to String in order to access the toStringAsFixed(0) like this
"${double.parse(balance.toString()).toStringAsFixed(0)}".replaceAllMapped(reg, mathFunc),
so for 430 -> would be 430 and 4300 -> would be 4,300 and 43000 -> would be 43,000
I would suggest using the intl package https://pub.dev/packages/intl
Example usage:
var f = NumberFormat("#,###.##");
print(f.format(123456789.4567));
This will result in the following output:
123,456,789.46
Note that , in the pattern specifies the grouping separator and ### specifies the group size to be 3. .##specifies rounding on two decimal points.
Check for additional documentation: https://api.flutter.dev/flutter/intl/NumberFormat-class.html

Flutter: Text clipping not working when in a Row

I have a situation where there are two text items in a row. The first text can have a variable length, but I don't want to wrap the texts in two lines. So I need the first text to be clipped when the parent does not have enough width for it and the second text. Now, if I do not place the texts in a row, it works as expected. But if they are in a Row, the clipping does not occur. I can not wrap the first text in an Expanded widget (which fixes the clipping problem) because it adds a blank space between the two texts.
Here is a code snippet
Container(
child: Row (
children: <Widget>[
Text("Long text that needs to be clipped",
overflow: TextOverflow.clip,
textAlign: TextAlign.left),
//there should be no blank space between the elements
Text( "+1",
textAlign: TextAlign.left,
overflow: TextOverflow.fade),
],
)
)
Been scratching my head for a while...
We can use Expanded as per below:
Material(
child: Container(
color: appColor,
child: Row(
children: <Widget>[
Expanded(
child: Align(
alignment: AlignmentDirectional.centerStart,
child: Padding(
padding: const EdgeInsets.only(left: 8.0),
child: Text(
"Lorem Ipsum is simply dummy text of the printing and typesetting industry.",
softWrap: false,
overflow: TextOverflow.fade,
style: TextStyle(color: Colors.white),
),
),
),
),
IconButton(
icon: Image.asset('assets/images/filter.png'),
onPressed: () {},
),
IconButton(
icon: Image.asset('assets/images/notification.png'),
onPressed: () {},
),
],
)),
);

How to set background colour to the width containing text in flutter

I have a text which is basically a title in a box and I want to provide a background color to the space behind that text .
Padding(
padding: EdgeInsets.all(15.0),
child: Text(
'Your Profile',
style: TextStyle(
color: Colors.blue,
fontSize: 26.0,
background: Paint()..color = Colors.blue,
),
This is what I tried but it seems not working.
Expected:Here
"Your profile" text has a background color, I need to achieve this.
You can wrap it with a Container() but, most probably you have to define the width with something like double.maxFinite
Check this
Container(
width: double.maxFinite,
color: Colors.red, //define the background color
child: Text("My Text"),
)
Short answer:
Text(
"Hello World",
style: TextStyle(backgroundColor: Colors.blue), // give any color here
)
Full answer
You can use this in your showDialog call, Here is the full widget you were looking for.
Dialog(
elevation: 12,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Container(
color: Colors.blue[800],
width: double.maxFinite,
alignment: Alignment.center,
padding: EdgeInsets.symmetric(vertical: 16),
child: Text(
"Your Profile...",
style: TextStyle(fontSize: 20, color: Colors.white),
),
),
Padding(
padding: EdgeInsets.symmetric(horizontal: 32),
child: Column(
children: <Widget>[
SizedBox(height: 12),
TextField(decoration: InputDecoration(hintText: "Select a Photo")),
TextField(decoration: InputDecoration(hintText: "Take a Photo...")),
TextField(decoration: InputDecoration(hintText: "Choose form Library")),
],
),
),
Container(
padding: EdgeInsets.symmetric(vertical: 12),
child: RaisedButton(
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(50)),
child: Text("Back"),
onPressed: () {},
color: Colors.red,
textColor: Colors.white,
),
)
],
),
I agree with Saeb Nabil's answer, the easiest way is to use a container. That way you can also control the padding around the text. You can also use .withOpacity() to add transparency if you wish:
Container(
width: 100,
padding: EdgeInsets.symmetric(horizontal: 0, vertical: 10),
color: Colors.grey[900].withOpacity(0.6),
child: Text(
"Hello world!",
style: TextStyle(
fontSize: 14,
color: Colors.white),
), // Text
), // Container

How to remove the gap above and below the text on flutter

I am trying to put the text "Hello" right below the "123", but the bigger the text is, the bigger the gap. How do I remove the gap??? Flutter images are added below.
The only way I could find so far is to reduce height property, the problem though is that it reduces the gap above only. So in your case, you could try to set it for hello text to the minimum:
Text(
'123',
style: TextStyle(fontSize: 60.0),
),
Text(
'hello',
style: TextStyle(fontSize: 10.0, height: 0.1),
),
Use Stack widget to align your text widgets
Stack(
children: <Widget>[
Text(
'123',
style: TextStyle(fontSize: 60.0),
),
Positioned(
child: Text('Hello'),
bottom: 0.0,
left: 35.0,
)
],
),
Hope it helps!

Resources