Flutter: Tame those TextStyles!

栏目: IT技术 · 发布时间: 6年前

内容简介:One of the most verbose parts of Flutter is handling of various font styles, sizes, and families. In this post, we’ll show a couple of the tricks we’re using in production to ease this paint point.The first step we do in nearly every project, is to define

One of the most verbose parts of Flutter is handling of various font styles, sizes, and families. In this post, we’ll show a couple of the tricks we’re using in production to ease this paint point.

Styles.dart

The first step we do in nearly every project, is to define a base Styles file, that will hold a variety of small classes, defining our core Fonts and TextStyles:

class Fonts {
  static String get body => "Lato";
  static String get title => "Roboto";
}

class FontSizes {
  static double scale = 1;
  
  static double get body => 14 * scale;
  static double get bodySm => 12 * scale;

  static double get title => 16 * scale;
}

class TextStyles {
  static TextStyle get bodyFont => TextStyle(fontFamily: Fonts.body);
  static TextStyle get titleFont => TextStyle(fontFamily: Fonts.title);

  static TextStyle get title => titleFont.copyWith(fontSize: FontSizes.title);
  static TextStyle get titleLight => title.copyWith(fontWeight: FontWeight.w300);

  static TextStyle get body => bodyFont.copyWith(fontSize: FontSizes.body, fontWeight: FontWeight.w300);
  static TextStyle get bodySm => body.copyWith(fontSize: FontSizes.bodySm);
}

Some things to note:

  • Why the get functions instead of variables? Hot Reload ! By using static functions, we can change these values at run-time for extremely fast iteration and tweaking.
  • We can change the font style, or font sizes globally, extremely easily and quickly . All styling code is consolidated in one tight package.
  • FontSizes.scale – A bit outside the scope of this article, but we like to expose a global scale modifier to our fonts. Normally this is 1, but we can tweak it for different form factors.

With the above class, we can now define a Text widget like so:

Text("I am Text.", style: TextStyles.body);

That’s not only a lot more readable, it’s also is significantly easier to maintain as your project scales.

Ok, so that’s pretty good… but it can be better!

While the above approach is an excellent foundation, it could still spiral out of control if you tried to define every single combination of bold/italic/character spacing. There can be a lot of ‘one off’ styles in an app, and it would be nice to be able to easily tweak these styles on the fly…

Dart Extensions to the rescue!

Flutter: Tame those TextStyles!

Modifying a text style is actually pretty easy, using the copyWith API, it’s just kinda verbose:

TextStyles.body.copyWith(fontWeight: FontWeight.bold, fontStyle: FontStyle.italic, letterSpacing: 1.6)

Oof. That’s a lot of code to just add bold, italic and tweak the letter spacing. It’s not super readable either, the important stuff ( body, bold, italic, 1.6 ) represent only 15% of the characters, the rest is boilerplate.

We define a super simple TextStyle Extension to ease this pain:

extension TextStyleHelpers on TextStyle {
  TextStyle get bold => copyWith(fontWeight: FontWeight.bold);
  TextStyle get italic => copyWith(fontStyle: FontStyle.italic);
  TextStyle letterSpace(double value) => copyWith(letterSpacing: value);
}

Now, we can re-write it like this:

TextStyles.body.bold.italic.letterSpace(1.6)

Much better! Boilerplate is only about 50% of the character, and the intent stands out much better.

We thought of creating a package for this, but decided that it should probably live in the excellent styled_widget package , so we’ll try and make that happen soon.

Thanks for checking out the post!


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Unity 3D游戏开发(第2版)

Unity 3D游戏开发(第2版)

宣雨松 / 人民邮电出版社 / 2018-9 / 89.00元

Unity 是一款市场占有率非常高的商业游戏引擎,横跨25 个主流游戏平台。本书基于Unity 2018,结合2D 游戏开发和3D 游戏开发的案例,详细介绍了它的方方面面,内容涉及编辑器、游戏脚本、UGUI 游戏界面、动画系统、持久化数据、静态对象、多媒体、资源加载与优化、自动化与打包等。 本书适合初学者或者有一定基础的开发者阅读。一起来看看 《Unity 3D游戏开发(第2版)》 这本书的介绍吧!

图片转BASE64编码
图片转BASE64编码

在线图片转Base64编码工具

XML 在线格式化
XML 在线格式化

在线 XML 格式化压缩工具

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器