简体   繁体   English

如何在自定义文本 Flutter 中使用 GoogleFonts?

[英]How to use GoogleFonts in custom text Flutter?

I've multiple text in app with different fontFamily So I've created a custom text class but how to use GoogleFonts in custom text class?我在应用程序中有多个具有不同fontFamily的文本所以我创建了一个自定义文本 class 但是如何在自定义文本 class 中使用GoogleFonts So that I can easily use fontWeight , color , fontSize of GoogleFonts .这样我就可以轻松使用GoogleFontsfontWeightcolorfontSize了。

Here is my CustomText class这是我的CustomText class

 class CustomText extends StatelessWidget {
  final String text;
  final double? size;
  final FontWeight? fontWeight;
  final Color? color;
  final double? wordSpacing;
  final VoidCallback? onClick;

  const CustomText({
    Key? key,
    required this.text,
    this.size,
    this.fontWeight,
    this.color,
    this.wordSpacing,
    this.onClick,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      child: onClick == null
          ? Text(
              text,
              style: TextStyle(
                fontSize: size ?? 14,
                fontWeight: fontWeight ?? FontWeight.w500,
                color: color ?? Colors.grey,
                wordSpacing: wordSpacing,
              ),
            )
          : TextButton(
              onPressed: () {
                onClick!.call();
              },
              child: Text(
                text,
                style: TextStyle(
                  fontSize: size ?? 16,
                  fontWeight: fontWeight ?? FontWeight.w500,
                  color: color ?? Colors.grey,
                  wordSpacing: wordSpacing,
                ),
              ),
            ),
    );
  }
}

Please refer to below code请参考以下代码


class MyApp extends StatelessWidget {
  MyApp({Key key}) : super(key: key);

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primaryColor: Color(0xfff00B074),
        textTheme: const TextTheme(
          bodyText1: TextStyle(
              fontSize: 18.0,
              fontFamily: 'Barlow-Medium',
              color: Color(0xff464255)),
        ),
      ),
      home: CustomTextDemo(),
    );
  }
}

class CustomTextDemo extends StatelessWidget {
  const CustomTextDemo({Key key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: CustomText(
          text: "Custom Text Demo",
          googleFontFamily: GoogleFonts.lato(
            fontStyle: FontStyle.italic,
            fontSize: 22.0,
            color: Colors.blue,
          ),
        ),
      ),
    );
  }
}

class CustomText extends StatelessWidget {
  final String text;
  final double size;
  final FontWeight fontWeight;
  final Color color;
  final double wordSpacing;
  final VoidCallback onClick;
  final String fontFamily; /* Add this Font Family param */
  final TextStyle googleFontFamily;

  const CustomText({
    Key key,
    @required this.text,
    this.size,
    this.fontWeight,
    this.color,
    this.wordSpacing,
    this.onClick,
    this.fontFamily,
    this.googleFontFamily,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      child: onClick == null
          ? Text(
              text,
              style: googleFontFamily,
              // TextStyle(
              //     fontSize: size ?? 14,
              //     fontWeight: fontWeight ?? FontWeight.w500,
              //     color: color ?? Colors.grey,
              //     wordSpacing: wordSpacing,
              //     fontFamily: 'Roboto'), /* Add this Font Family param */
            )
          : TextButton(
              onPressed: () {
                onClick.call();
              },
              child: Text(
                text,
                style: TextStyle(
                  fontSize: size ?? 16,
                  fontWeight: fontWeight ?? FontWeight.w500,
                  color: color ?? Colors.grey,
                  wordSpacing: wordSpacing,
                ),
              ),
            ),
    );
  }
}

I am not sure if I got it right, as I never tried to apply as a use-case, but I think you can combine by using the getFont and checking before using its implementation.我不确定我是否做对了,因为我从未尝试将其作为用例应用,但我认为您可以通过使用getFont并在使用其实现之前进行检查来组合。

 class CustomText extends StatelessWidget {
  final String text;
  final double? size;
  final String googleFont;
  final FontWeight? fontWeight;
  final Color? color;
  final double? wordSpacing;
  final VoidCallback? onClick;
  TextStyle? myTextStyle;

  CustomText({
    Key? key,
    required this.text,
    this.size,
    this.fontWeight,
    this.color,
    this.googleFont = '',
    this.wordSpacing,
    this.onClick,
  }) : super(key: key);




  @override
  Widget build(BuildContext context) {
   

    myTextStyle = TextStyle(
                fontSize: size ?? 14,
                fontWeight: fontWeight ?? FontWeight.w500,
                color: color ?? Colors.grey,
                wordSpacing: wordSpacing,
              );

    return Container(
      child: onClick == null
          ? Text(
              text,
              style: googleFont.isEmpty ? myTextStyle:GoogleFonts.getFont(
                googleFont, textStyle: myTextStyle),
            )
          : TextButton(
              onPressed: () {
                onClick!.call();
              },
              child: Text(
                text,
                style: googleFont.isEmpty ? myTextStyle : GoogleFonts.getFont(
                googleFont, textStyle: myTextStyle),
              ),
            ),
    );
  }
}


声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM