Last active
July 29, 2020 00:14
-
-
Save ElZombieIsra/caa4b774d762199057fe9b68bb43cce4 to your computer and use it in GitHub Desktop.
Custom icon implementation to solve Flutters Icon Widget icon overflow
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
final Color darkBlue = Color.fromARGB(255, 18, 32, 47); | |
void main() { | |
runApp(MyApp()); | |
} | |
class MyApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
theme: ThemeData.dark().copyWith(scaffoldBackgroundColor: darkBlue), | |
debugShowCheckedModeBanner: false, | |
home: Scaffold( | |
body: Container( | |
constraints: BoxConstraints.expand(), | |
child: Column( | |
mainAxisSize: MainAxisSize.max, | |
mainAxisAlignment: MainAxisAlignment.center, | |
crossAxisAlignment: CrossAxisAlignment.center, | |
children: [ | |
CustomIcon( | |
Icons.person, | |
color: Colors.white, | |
size: 100, | |
), | |
], | |
), | |
), | |
), | |
); | |
} | |
} | |
class CustomIcon extends StatelessWidget { | |
final IconData icon; | |
final double size; | |
final Color color; | |
final String semanticLabel; | |
final TextDirection textDirection; | |
/// Fixes the overflow problem present in Flutters [Icon] widget | |
const CustomIcon( | |
this.icon, { | |
Key key, | |
this.size, | |
this.color, | |
this.semanticLabel, | |
this.textDirection, | |
}) : super(key: key); | |
@override | |
Widget build(BuildContext context) { | |
return Semantics( | |
label: semanticLabel, | |
child: ExcludeSemantics( | |
child: RichText( | |
overflow: TextOverflow.visible, // Never clip. | |
text: TextSpan( | |
text: String.fromCharCode(icon.codePoint), | |
style: TextStyle( | |
inherit: false, | |
color: color, | |
fontSize: size, | |
fontFamily: icon.fontFamily, | |
package: icon.fontPackage, | |
), | |
), | |
), | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment