问题描述
我正在尝试将两个google_fonts添加到ThemeData中。我的所有主题都已应用,但Google字体未应用。为什么是这样?这是我的项目设置:
主题:
import 'package:flutter/material.dart';
import 'package:google_fonts/google_fonts.dart';
import 'theme_utils.dart';
MaterialColor primaryColorShades = generateMaterialColor(primaryColor);
MaterialColor accentColorShades = generateMaterialColor(accentColor);
const Color primaryColor = Color(0xFF62d9d5);
const Color accentColor = Color(0xFF27a562);
const Color blueText = Color(0xFF409491);
ThemeData blueTheme = ThemeData(
colorScheme: const ColorScheme(
brightness: Brightness.light,primary: primaryColor,secondary: accentColor,surface: Colors.white,background: primaryColor,error: Colors.red,onBackground: Colors.white,onError: Colors.white,onPrimary: Colors.white,onSecondary: Colors.white,onSurface: blueText,primaryVariant: Colors.white,secondaryVariant: Colors.white),visualDensity: VisualDensity.standard,canvasColor: primaryColor,textTheme: GoogleFonts.varelaRoundTextTheme().copyWith(
headline1: const TextStyle(
fontSize: 30.0,fontWeight: FontWeight.bold,color: Colors.white),subtitle1: TextStyle(
fontSize: 18.0,color: Colors.white.withOpacity(0.7)),button: GoogleFonts.hind(
textStyle: const TextStyle(
fontSize: 12.0,fontWeight: FontWeight.w500,color: blueText)),),);
main.dart:
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'app/routes/app_pages.dart';
import 'app/theme/theme.dart';
void main() {
runApp(GetMaterialApp(
title: 'Application',initialRoute: AppPages.INITIAL,getPages: AppPages.routes,theme: blueTheme,));
}
在我看来,像这样使用它们:
Text('Hi there,\nI\'m Vepo',style: Theme.of(context).textTheme.headline1,textAlign: TextAlign.center),Text('I can help you find vegan things in your local area',style: Theme.of(context).textTheme.subtitle1,RaisedButton(padding: const EdgeInsets.symmetric(
vertical: 10,horizontal: 60),elevation: 20.00,shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(40.0)),child: Text(
'COOL,SIGN ME UP!',style: Theme.of(context).textTheme.button,onPressed: () {}),
在我的pubspec.yaml中:
flutter:
uses-material-design: true
assets:
- assets/images/
- google_fonts/
在我的项目的根目录中:
为什么不应用字体?
在google_fonts发布页面上,似乎我可能需要从ThemeData中引用上下文,但是由于它是一个不同的文件,所以我无权访问,并且我真的不想将ThemeData更改为像他们的文档一样内联:
MaterialApp(
theme: ThemeData(
textTheme: GoogleFonts.latoTextTheme(
Theme.of(context).textTheme,);
解决方法
我认为我必须以google_fonts的方式进行操作,但是我没有。 google_fonts方法似乎需要ThemeData中某些内容的上下文。更改为颤动方式:
在pubspec.yaml中:
Option Explicit
Sub RowNames()
' Define worksheet.
Dim ws As Worksheet
Set ws = ThisWorkbook.Sheets("MonthlySales")
' Define Source Range.
Dim rng As Range
Set rng = ws.Range("B2:N41")
' Define Columns Count.
Dim ColumnsCount As Long
ColumnsCount = rng.Columns.Count
Dim RowRange As Range ' Current Row Range
Dim r As Long ' Source Range Rows Counter
Dim c As Long ' Source Range / Current Row Range Columns Counter
' Loop through rows of Source Range.
For r = 1 To rng.Rows.Count
' Create a reference to the current Row Range.
Set RowRange = rng.Rows(r)
' Check if first cell of current Row Range contains a value,' making it a possible candidate for a defined name.
If RowRange.Cells(1).Value <> "" Then
' Loop through cells (columns) of current Row Range backwards.
For c = ColumnsCount To 2 Step -1
' Check if current cell in current Row Range contains a value.
If RowRange.Cells(c) <> "" Then
' Create a named range from value in first cell. The range
' is defined from the second cell to to current cell
' in current Row Range.
RowRange.Cells(1).Resize(,c).CreateNames Left:=True
' Exit loop,we got what we came for (the named range).
Exit For
End If
Next c
End If
Next r
End Sub
在我的主题中:
flutter:
uses-material-design: true
fonts:
- family: VarelaRound
fonts:
- asset: assets/fonts/VarelaRound-Regular.ttf
然后在视图中像这样使用:
import 'package:flutter/material.dart';
import 'theme_utils.dart';
MaterialColor primaryColorShades = generateMaterialColor(primaryColor);
MaterialColor accentColorShades = generateMaterialColor(accentColor);
const Color primaryColor = Color(0xFF62d9d5);
const Color accentColor = Color(0xFF27a562);
const Color blueText = Color(0xFF409491);
ThemeData blueTheme = ThemeData(
colorScheme: const ColorScheme(
brightness: Brightness.light,primary: primaryColor,secondary: accentColor,surface: Colors.white,background: primaryColor,error: Colors.red,onBackground: Colors.white,onError: Colors.white,onPrimary: Colors.white,onSecondary: Colors.white,onSurface: blueText,primaryVariant: Colors.white,secondaryVariant: Colors.white),visualDensity: VisualDensity.standard,canvasColor: primaryColor,fontFamily: 'varelaRound',textTheme: TextTheme(
headline1: const TextStyle(
fontSize: 30.0,fontWeight: FontWeight.bold,color: Colors.white,fontFamily: 'varelaRound'),subtitle1: TextStyle(
fontSize: 18.0,color: Colors.white.withOpacity(0.7)),caption: TextStyle(
fontSize: 14.0,fontWeight: FontWeight.w600,fontFamily: 'hind',button: const TextStyle(
fontSize: 12.0,fontWeight: FontWeight.w500,color: blueText,fontFamily: 'hind'),),);