【问题标题】:Flutter - Change default font "undefined name context"Flutter - 更改默认字体“未定义的名称上下文”
【发布时间】:2020-01-15 14:16:07
【问题描述】:

我正在尝试更改应用程序的默认字体主题,但它给了我一个错误提示:

未定义名称“上下文”。
尝试将名称更正为已定义的名称,或定义名称。

Theme.of(context).textTheme, 这一行的错误是我的代码:

import 'package:flutter/material.dart';
import 'package:google_fonts/google_fonts.dart';
import 'package:nazar_mob/app_base/animations.dart';
import 'package:nazar_mob/pages/second_page.dart';
import 'package:nazar_mob/app_base/app_bar.dart';

void main() {
runApp(
    new MaterialApp(
    debugShowCheckedModeBanner: false,
    theme: ThemeData(
        textTheme: GoogleFonts.robotoCondensedTextTheme(
        Theme.of(context).textTheme,
        ),
    ),
    home: new MyFirstPage(),
    routes: <String, WidgetBuilder>{
        "/MySecondPage": (BuildContext context) => new MySecondPage()
    },
    )
);
}

【问题讨论】:

    标签: flutter


    【解决方案1】:

    您需要将您的 MaterialApp 封装在一个 Widget 中。小部件(无状态或有状态)提供build 函数,它为您提供context

    void main() => runApp(MyApp());
    
    class MyApp extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          debugShowCheckedModeBanner: false,
          theme: ThemeData(
            textTheme: GoogleFonts.robotoCondensedTextTheme(
            Theme.of(context).textTheme,
            ),
          ),
          home: new MyFirstPage(),
          routes: <String, WidgetBuilder>{
            "/MySecondPage": (BuildContext context) => new MySecondPage()
          },
        );
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2020-10-26
      • 2019-07-07
      • 2021-03-24
      • 1970-01-01
      • 1970-01-01
      • 2020-01-28
      • 1970-01-01
      • 2021-01-21
      • 1970-01-01
      相关资源
      最近更新 更多