简体   繁体   English

Flutter AppBarTheme 类构造函数已弃用

[英]Flutter AppBarTheme Class Constructor Deprecated

In beginning to learn Flutter cross-platform application development, I am following along Nick Manning's Flutter Course - Full Tutorial for Beginners (Build iOS and Android Apps) .在开始学习 Flutter 跨平台应用程序开发时,我正在学习 Nick Manning 的Flutter Course - Full Tutorial for Beginners (Build iOS and Android Apps) However, at 43:47 of the video, I am getting the following error .但是,在视频的 43:47,我收到以下错误

Essentially, the App class is the class called by the runApp() method of Main's runApp method.本质上,App 类就是 Main 的 runApp 方法的 runApp() 方法调用的类。 The code in question is the appBarTheme parameter of the ThemeData widget.有问题的代码是 ThemeData 小部件的 appBarTheme 参数。 A constant named AppBarTextStyle is given as the argument for the TextTheme constructor, which is in turn an argument for AppBarTheme.一个名为 AppBarTextStyle 的常量作为 TextTheme 构造函数的参数给出,而该构造函数又是 AppBarTheme 的一个参数。 At the moment, the program would not really achieve any constructive output, but I am trying to set the text theme for the remainder of the program which does not work.目前,该程序不会真正实现任何建设性的输出,但我正在尝试为程序的其余部分设置文本主题,但它不起作用。

The code for app.dart and style.dart is given below. app.dart 和 style.dart 的代码如下所示。

// app.dart

import 'package:flutter/material.dart';
import 'screens/location_detail/location_detail.dart';
import 'style.dart';

class App extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: LocationDetail(),
      theme: ThemeData(
        appBarTheme: AppBarTheme(
          textTheme: TextTheme(title: AppBarTextStyle),
        ),
      ),
    );
  }
}
import 'package:flutter/material.dart';

const LargeTextSize = 26.0;
const MediumTextSize = 20.0;
const bodytextSize = 16.0;

const String FontNameDefault = "Montserrat";

const AppBarTextStyle = TextStyle(
  fontFamily: FontNameDefault,
  fontWeight: FontWeight.w300,
  fontSize: MediumTextSize,
  color: Colors.white,
);

After looking at the FlutterAppBarTheme class API documentation , it appears that the constructor for AppBarTheme had been deprecated since the release of the video.查看 FlutterAppBarTheme 类 API 文档后,似乎 AppBarTheme 的构造函数自视频发布以来已被弃用。 This is the deprecation message given in the Flutter documentation:这是 Flutter 文档中给出的弃用消息:

@Deprecated('This property is no longer used, please use systemOverlayStyle instead. ' 'This feature was deprecated after v2.4.0-0.0.pre.') @Deprecated('此属性不再使用,请改用systemOverlayStyle。''此功能在v2.4.0-0.0.pre之后被弃用。')

As prompted, I tried replacing the default constructor with the suggested named constructor, systemOverlayStyle.根据提示,我尝试用建议的命名构造函数 systemOverlayStyle 替换默认构造函数。 But this was to no avail, as I received the following error message .但这无济于事,因为我收到了以下错误消息

I have no idea how to fix my code and would very much appreciate some help.我不知道如何修复我的代码,非常感谢一些帮助。 Thanks in advance!提前致谢!

In order to change appBar text theme, you need to use toolbarTextStyle and titleTextStyle .为了更改 appBar 文本主题,您需要使用toolbarTextStyletitleTextStyle

theme: ThemeData(
  appBarTheme: AppBarTheme(
    toolbarTextStyle: TextStyle(..),
    titleTextStyle: TextStyle(..),
  ),
),

It is better with extending parent theme 扩展父主题更好

A better rich content is cookbook/design/themes更好的丰富内容是食谱/设计/主题

You should use it like this instead of textTheme :您应该像这样使用它而不是textTheme

 MaterialApp(
      theme: ThemeData(
        appBarTheme: AppBarTheme(
          color: darkBlue,
          toolbarTextStyle: const TextTheme(
            headline6: TextStyle(
              color: Colors.white,
              fontSize: 20,
            ),
          ).bodyText2,
          titleTextStyle: const TextTheme(
            headline6: const TextStyle(
              color: Colors.white,
              fontSize: 20,
            ),
          ).headline6,
        ),
      ),
      home: SafeArea(
        child: const Scaffold(
          body: MyStatefulWidget(),
        ),
      ),
    );

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

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