Search code examples
flutterstrikethrough

How to add a Strikethrough to Icon in Flutter?


I need to add a strikethougth in my icon. I read about custom strikethougth using padding but it is working on text

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),
  )

How can I add it to this code?

Icon(
 Icons.list_alt,
 color: Colors.white,
)

Edit 1

I tried :

Container(
 padding: EdgeInsets.symmetric(horizontal: 8),
 child: Icon(
 Icons.shopping_cart,
 color: Colors.white,
 size: 200,
),
)

And it is not working


Solution

  • enter image description here

    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: Icon(Icons.shopping_cart, color: Colors.orangeAccent, size: 200),
              ),
            ),
          ),
        );
      }
    }
    
    class StrikeThroughWidget extends StatelessWidget {
      final Widget child;
    
      StrikeThroughWidget({Key key, @required this.child}) : super(key: key);
    
      @override
      Widget build(BuildContext context) {
        return Container(
          child: child,
          padding: EdgeInsets.symmetric(horizontal: 8),
          foregroundDecoration: BoxDecoration(
            image: DecorationImage(image: AssetImage('assets/strikethrough.png'), fit: BoxFit.fitWidth),
          ),
        );
      }
    }