Flutter 2.5中AppBarTheme的textTheme属性出现废弃警告该如何处理?
Flutter 2.5 AppBarTheme textTheme 废弃警告解决方案
从Flutter 2.5版本开始,AppBarTheme下的textTheme属性已被官方标记为废弃,该属性被拆分为两个独立的样式属性,分工更明确,避免全局样式污染。
适配方案
将appBarTheme配置段中废弃的textTheme,替换为两个新的专属样式属性即可:
toolbarTextStyle:控制AppBar工具栏中除标题外的文本样式,例如右上角操作按钮的文字titleTextStyle:专门控制AppBar的标题文本样式
你已经在全局ThemeData中配置了fontFamily,AppBar的两个文本样式会自动继承该字体配置,无需额外重复设置。
修改后的完整代码如下
return MaterialApp( // home: homePage(), themeMode: ThemeMode.light, theme: ThemeData( primarySwatch: Colors.purple, fontFamily: GoogleFonts.lato().fontFamily, appBarTheme: AppBarTheme( color: Colors.white, elevation: 0, iconTheme: IconThemeData(color: Colors.black), // 替换原废弃的textTheme配置 toolbarTextStyle: Theme.of(context).textTheme.bodyText2, titleTextStyle: Theme.of(context).textTheme.headline6, // 如需自定义样式直接传入TextStyle即可,示例: // titleTextStyle: TextStyle(color: Colors.black, fontSize: 18, fontWeight: FontWeight.bold), ) ), darkTheme: ThemeData(brightness: Brightness.dark, primarySwatch: Colors.red), debugShowCheckedModeBanner: false, initialRoute: MyRoutes.homeRoutes, routes: { "/": (context) => LoginPage(), MyRoutes.homeRoutes: (context) => homePage(), MyRoutes.loginRoutes: (context) => LoginPage(), }, );
内容的提问来源于stack exchange,提问作者Amit sharan
相关产品推荐
相关产品推荐

