I want to set rounded corner of Textspan in flutter, I think class Paint
is needed, but I cannot figure out how to do that.
import 'package:flutter/material.dart';
void main() => runApp(new MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: new AppBar(),
body: new RichText(
text: new TextSpan(
text: null,
style: TextStyle(fontSize: 20.0, color: Colors.black),
children: <TextSpan>[
new TextSpan(
text: 'inactive ',),
new TextSpan(
text: 'active',
style: new TextStyle(
color: Colors.white,
background: Paint()
..color = Colors.redAccent,
)),
],
),
),
),
);
}
}
Is there a way to use Textspan to achieve that instead of using a Container
wrapping Text
?
Without using Container
or something else - I can see only one way to make corners rounded
TextSpan(
text: 'active',
style: TextStyle(
fontSize: 20.0,
color: Colors.white,
background: Paint()
..strokeWidth = 24.0
..color = Colors.red
..style = PaintingStyle.stroke
..strokeJoin = StrokeJoin.round))
But in this case there are paddings around text, so I doubt this is proper way
You can use RichText
with WidgetSpan
, then combine it with line height
RichText(
text: TextSpan(
children: [
WidgetSpan(
child: Container(
child: Text(
addressItem.deliveryAddressType == "home" ? "Nhà" : "Văn phòng",
style: TextStyle(
fontFamily: AppFonts.SFUITextMedium,
color: AppColors.wram_grey,
fontSize: AppFonts.textSizeSmall,
),
),
decoration: BoxDecoration(
color: AppColors.header_grey, borderRadius: BorderRadius.all(Radius.circular(20))),
padding: EdgeInsets.fromLTRB(6, 2, 6, 2),
margin: EdgeInsets.only(right: 5),
),
),
TextSpan(
text: '${addressItem.street}, ${addressItem.ward}, ${addressItem.city}, ${addressItem.region}',
style: TextStyle(
fontFamily: AppFonts.SFUITextMedium,
color: AppColors.wram_grey,
fontSize: AppFonts.textSizeMedium,
height: 1.5),
),
],
),
),
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With