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

Ism*_*ace 1 dart flutter

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

    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),
      ...
Run Code Online (Sandbox Code Playgroud)

Ese*_*met 7

这里有你需要的:

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,
        );
}
Run Code Online (Sandbox Code Playgroud)