I am looking for a way to add a custom strikethrough to a text widget like below
I looked at the Text Style API, but couldn't find any option to customize the Strikethrough graphic.
style: TextStyle(decoration: TextDecoration.lineThrough),
I am looking for a way to add a custom strikethrough to a text widget like below
I looked at the Text Style API, but couldn't find any option to customize the Strikethrough graphic.
style: TextStyle(decoration: TextDecoration.lineThrough),
as an option
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: Center(
child: StrikeThroughWidget(
child: Text('Apple Juice', style: TextStyle(fontSize: 30)),
),
),
),
);
}
}
class StrikeThroughWidget extends StatelessWidget {
final Widget _child;
StrikeThroughWidget({Key key, @required Widget child})
: this._child = child,
super(key: key);
@override
Widget build(BuildContext context) {
return Container(
child: _child,
padding: EdgeInsets.symmetric(horizontal: 8), // this line is optional to make strikethrough effect outside a text
decoration: BoxDecoration(
image: DecorationImage(image: AssetImage('graphics/strikethrough.png'), fit: BoxFit.fitWidth),
),
);
}
}
I have tried using decorationImage but couldn't get the desired result. The problem is that the strike is coming behind the text. So I have found a solution by using stack.
Stack(
children: [
Text(
"\$250",
overflow: TextOverflow.ellipsis,
style: GoogleFonts.roboto(
decorationColor: Colors.red,
fontSize: 20,
color: Colors.grey.shade300,
fontWeight: FontWeight.w400),
),
Positioned(
top: 2,
left: 3,
child: SizedBox(
width: 42,
child: Image.asset("assets/images/strike.png")),
)
],
),
This is the result.