Flutter - 如何管理一个文件中的 TextStyle 'text_style.dart' 函数以在另一个 View 中调用?

标签 flutter dart

我想用一些构造函数创建自定义 TextStyle,例如 Color 和 fontWeight,因此稍后在屏幕 View 中字体和大小的样式是固定的,但只能自定义颜色和字体粗细,

    class TextStyles {
  final Color fontColor;

  const TextStyles({
    this.fontColor = Colors.black;
  });

  static const TextStyle buttonText = const TextStyle(
      fontFamily: 'Montserrat',
      color: fontColor,
      fontWeight: FontWeight.w700,
      fontSize: 14.0
  );
}

class CustomButton extends StatelessWidget {
  ....
  final Function onPressed;

  const CustomButton({
    Key key,
   ...
    @required this.onPressed,
    this.textSize = 14.0,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    final CreateBlueButton = FlatButton(
        color: background,
        child: Text(
          text,
          style: TextStyleCustom,
        )
    );

    return Container(
      constraints: BoxConstraints.expand(height: 53),
      ...

最佳答案

这里是你需要的:

import 'package:flutter/material.dart';

class MyTextStyle extends TextStyle {
  final Color color;
  final FontWeight fontWeight;
  final double size;
  final String fontFamily;

  const MyTextStyle({
    @required this.color,
    @required this.fontWeight,
    this.size = 14,
    this.fontFamily = 'Montserrat',
  })  : assert(color != null && fontWeight != null),
        super(
          color: color,
          fontWeight: fontWeight,
          fontSize: size,
          fontFamily: fontFamily,
        );
}

关于Flutter - 如何管理一个文件中的 TextStyle 'text_style.dart' 函数以在另一个 View 中调用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56898118/

相关文章:

android - Dart中的图像处理

每个文档抖动的 Firebase-firestore 安全规则

flutter 边框半径 TabBar 指示器

http - 为 http : ^0. 12.0 包设置 ssl

flutter - 在提供者初始化时调用将来的方法

flutter - Dart : Index out of range: index should be less than N: N (where N is the size of file in bytes). 如何修复此运行时错误?

flutter - 如何在Flutter (`beacon_broadcast`库中捕获错误?常规方法不适用于此库

flutter - 将子窗口小部件定位在“列”父窗口小部件的中心和末尾

flutter - 如何在 vscode 中禁用 "Run|Debug"行?

dart - 如何将 CSS 类绑定(bind)到 Polymer 中的可观察对象或属性?