Make CupertinoIcon render and not show placeholder - flutter

Based on this SO question, I tried to draw a CupertinoIcon on a Canvas:
final icon = CupertinoIcons.add;
TextPainter textPainter = TextPainter(
textDirection: TextDirection.ltr
);
textPainter.text = TextSpan(
text: String.fromCharCode(
icon.codePoint
),
style: TextStyle(
fontSize: 40.0,
fontFamily: icon.fontFamily
)
);
textPainter.layout();
textPainter.paint( canvas, Offset( params.x, params.y ) );
Note that the code is fully generic, since it uses properties like codePoint of the CupertinoIcon.
While this code renders the other painter commands, the code unfortunately renders a placeholder of the icon in Google Chrome:
How do I make the icon appear?

The icon appears, as soon as the package parameter gets added to TextStyle:
TextStyle(
fontSize: 60.0,
fontFamily: icon.fontFamily,
color: Colors.red,
package: icon.fontPackage // Need to make the icon appear
)
The documentation says this:
To use a font family defined in a package, the package argument must be provided.

Related

How to properly "override" some property from TextStyle

I have my own style:
final myStyle = GoogleFonts.roboto(
height: 1.2,
color: Colors.black54,
fontWeight: FontWeight.w300,
fontSize: 16
);
I want to use it later in two TextWidget, but with different FontWeight property, so I thought about using copyWith method like this:
Text(
"myText",
style: myStyle.copyWith(fontWeight:FontWeight.w400 ),
),
But it's not working. Text is not bolded (by FontWeight.w400)
Default value of fonts is w400 already so it will look as it is not working but it is, so I suggest you to try a different FontWeight so you can see the difference. By the way you are overriding it properly already.

Dart - How to a change the color of my raisedButton after clicking in dart?

so i've been struggling to change the color of my button from green to red for my flutter app. Most of the online resources are confusing me. This is my following code.
new RaisedButton(key:null, onPressed:buttonPressed,
color: Colors.green,
child:
new Text(
"10:00 A.M. - 11:00 A.M.",
style: new TextStyle(fontSize:15.0,
color: const Color(0xFF000000),
fontWeight: FontWeight.w200,
fontFamily: "Roboto"),
)
),void buttonPressed(){
}
I want to click it and it turn green, or even better. Click it, so it turns gray. Then click another button that states "confirm", and it would make all the grey buttons that have been clicked red. Regardless, I'm just trying to understand how to make the button change color after being clicked.
Basically, what you need to do is,
Make RaisedButton color a class-level variable instead of constant as you have now.
Initialize that variable to some color value in initState method.
In the onPressed handler of RaisedButton, do the following:
Change that variable to some color value of your liking.
Trigger Repaint (so that the new color gets painted) i.e. call setState method.
Last but not least, you'll need to have all the code above part of a stateful widget.
Hopefully, it's clear enough, let us know if you'd need some code sample as well.
bool turnToRed = false;
bool colorIsGreen = true;
RaisedButton(key:null, onPressed: (){
setState((){
colorIsGreen = !colorIsGreen;
});
},
color: colorIsGreen ? Colors.green :turnToRed ? Colors.red : Colors.grey,
child:
new Text(
"10:00 A.M. - 11:00 A.M.",
style: new TextStyle(fontSize:15.0,
color: const Color(0xFF000000),
fontWeight: FontWeight.w200,
fontFamily: "Roboto"),
)
),
RaisedButton(key:null, onPressed: (){
setState((){
turnToRed = !turnToRed;
});
},
color: Colors.red
child:
new Text(
"Confirm",
style: new TextStyle(fontSize:15.0,
color: const Color(0xFF000000),
fontWeight: FontWeight.w200,
fontFamily: "Roboto"),
)
),

Flutter: how to paint a Font Awesome Icon?

I am using CustomPainter to draw in flutter as per this question: Flutter: How to paint an Icon on Canvas?
final icon = Icons.cake;
TextPainter textPainter = TextPainter(textDirection: TextDirection.rtl);
textPainter.text = TextSpan(
text: String.fromCharCode(icon.codePoint),
style: TextStyle(fontSize: 20.0, fontFamily: icon.fontFamily));
textPainter.layout();
textPainter.paint(canvas, Offset(200.0, 200.0));
This works for Material Design Icons. However I would like to use the same techniqe to paint Font Awesome Icons. I am using the font_awesome_flutter 8.5.0 package, and this is my code:
final icon = Icon(FontAwesomeIcons.fish, size: 20, color: Colors.teal[700]);
TextPainter textPainter = TextPainter(textDirection: TextDirection.rtl);
textPainter.text = TextSpan(
text: String.fromCharCode(icon.codePoint),
style: TextStyle(fontSize: 20.0, fontFamily: icon.fontFamily));
textPainter.layout();
textPainter.paint(canvas, Offset(200.0, 200.0));
I am getting a message from the IDE saying that "The getter 'codePoint' isn't defined for the class 'Icon'". How can I fix this please?
Thanks to #pskink for helping me figure out this answer:
final fishCodePoint = FontAwesomeIcons.fish.codePoint;
print('The codePoint is $fishCodePoint');
final fishFontPackage = FontAwesomeIcons.fish.fontPackage;
print('The fontPackage is $fishFontPackage');
final fishFontFamily = FontAwesomeIcons.fish.fontFamily;
print('The fontFamily is $fishFontFamily');
TextPainter textPainter = TextPainter(textDirection: TextDirection.rtl);
textPainter.text = TextSpan(
text: String.fromCharCode(fishCodePoint),
style: TextStyle(
fontSize: 40.0,
fontFamily: fishFontFamily,
package: fishFontPackage));
textPainter.paint(canvas, Offset(50.0, 50.0));

How to create text inside marker with google_maps_flutter?

How to implement a marker on Google Maps flutter with text on the marker. Image for reference:
https://i.stack.imgur.com/W6oqG.jpg
I managed to solve with this method
Future<BitmapDescriptor> createCustomMarkerBitmap(String title) async {
TextSpan span = new TextSpan(
style: new TextStyle(
color: Prefs.singleton().getTheme() == 'Dark'
? Colors.white
: Colors.black,
fontSize: 35.0,
fontWeight: FontWeight.bold,
),
text: title,
);
TextPainter tp = new TextPainter(
text: span,
textAlign: TextAlign.center,
textDirection: TextDirection.ltr,
);
tp.text = TextSpan(
text: title.toStringAsFixed(0),
style: TextStyle(
fontSize: 35.0,
color: Theme.of(context).accentColor,
letterSpacing: 1.0,
fontFamily: 'Roboto Bold',
),
);
PictureRecorder recorder = new PictureRecorder();
Canvas c = new Canvas(recorder);
tp.layout();
tp.paint(c, new Offset(20.0, 10.0));
/* Do your painting of the custom icon here, including drawing text, shapes, etc. */
Picture p = recorder.endRecording();
ByteData pngBytes =
await (await p.toImage(tp.width.toInt() + 40, tp.height.toInt() + 20))
.toByteData(format: ImageByteFormat.png);
Uint8List data = Uint8List.view(pngBytes.buffer);
return BitmapDescriptor.fromBytes(data);
}
how to use:
BitmapDescriptor bitmapDescriptor = await createCustomMarkerBitmap(...);
Marker marker = Marker(
/* in addition to your other properties: */
icon: bitmapDescriptor
);
You can either do it manually using Canvas and Painter. Or you could use this package which does the exact same thing asked in the question.
https://pub.dev/packages/label_marker
Just go through the overview once and you can see if it is for you or not. I really hope it helps. And yeah. I made it :)

Drawing text in a specific spot using flutter

For text drawing on canvas, a fairly simple construction can be used:
void drawName(Canvas context, String name, double x, double y)
{
TextSpan span = new TextSpan(
style: new TextStyle(color: Colors.blue[800], fontSize: 24.0,
fontFamily: 'Roboto'), text: name);
TextPainter tp = new TextPainter(
text: span, textAlign: TextAlign.left, textDirection: `
` TextDirection.ltr);
tp.layout();
tp.paint(context, new Offset(x, y));
}
Is it possible to draw text at an angle, for example 45 degrees, or 90 degrees (vertically from the bottom up)?
To rotate text on a canvas, you can use canvas transforms rather than rotating the entire canvas.
That looks something like this:
#override
void paint(Canvas canvas, Size size) {
// save is optional, only needed you want to draw other things non-rotated & translated
canvas.save();
canvas.translate(100.0, 100.0);
canvas.rotate(3.14159/4.0);
TextSpan span = new TextSpan(
style: new TextStyle(color: Colors.blue[800], fontSize: 24.0,
fontFamily: 'Roboto'), text: "text");
TextPainter tp = new TextPainter(
text: span, textDirection: TextDirection.ltr);
tp.layout();
tp.paint(canvas, new Offset(0.0, 0.0));
// optional, if you saved earlier
canvas.restore();
}
Note that I'm translating then rotating, because if you translate after or even use the offset you'll probably get a different result than what you want. Also, once you start using transforms (translate & rotate) you probably want to save the transform state and then restore after you draw whatever you want transformed, at least if you're drawing anything other than the rotated text.
It sounds like you are looking for a Transformation. There is a general Transform Widget, but there is also a more specific RotatedBox Widget that sounds like it will be a perfect fit for you.
new RotatedBox(
quarterTurns: 3,
child: const Text('Hello World!'),
)
If you need more control over the rotation (to use something other than 90 degree increments) you should be able to achieve this the Transform Widget and a Matrix4.rotationZ
new Container(
color: Colors.blue,
child: new Transform(
transform: new Matrix4.rotationZ(-0.785398),
child: new Container(
padding: const EdgeInsets.all(8.0),
color: const Color(0xFFE8581C),
child: const Text('Apartment for rent!'),
),
),
)
A function that draws text at a specified angle:
void drawText(Canvas context, String name, double x, double y, double angleRotationInRadians)
{
context.save();
context.translate(x, y);
context.rotate(angleRotationInRadians);
TextSpan span = new TextSpan(
style: new TextStyle(color: Colors.blue[800], fontSize: 24.0,
fontFamily: 'Roboto'), text: name);
TextPainter tp = new TextPainter(
text: span, textAlign: TextAlign.left,
textDirection: TextDirection.ltr);
tp.layout();
tp.paint(context, new Offset(0.0,0.0));
context.restore();
}
PS."rmtmckenzie", thank a lot for your help.