在 Flutter 中访问 Widget 中的 colorScheme
Accessing colorScheme in Widgets in Flutter
想为 Card 使用强调色,由于不推荐使用 accentColor,所以我改用了 colorScheme。在 MaterialApp 的 themeData 中描述了 colorScheme。但最终无法将其用于卡。
显示错误:“参数类型 'ColorScheme' 无法分配给参数类型 'Color'”
这是来自 MaterialApp 的主题数据
theme: ThemeData(
primarySwatch: Colors.green,
colorScheme: ColorScheme.fromSwatch().copyWith(secondary: Colors.red),
canvasColor: Color.fromRGBO(255, 245, 224, 1),
fontFamily: 'Raleway',
textTheme: ThemeData.light().textTheme.copyWith(
bodyText1: TextStyle(
color: Color.fromRGBO(23, 45, 23, 1),
),
bodyText2: TextStyle(
color: Color.fromRGBO(23, 45, 23, 1),
),
headline6: TextStyle(
fontSize: 20,
fontFamily: 'RobotoCondensed',
fontWeight: FontWeight.bold,
),
),
),
这是使用它的卡片
Card(
child: Text(selectedMeal.ingredients[i]),
color: Theme.of(context).colorScheme,//error shows here
),
colorScheme
属性 是 ColorScheme
类型,而 color
需要 Color
类型。
ColorScheme
持有不同的颜色可以访问,例如:
color: Theme.of(context).colorScheme.secondary,
下面是一个完整的例子:
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Demo',
theme: ThemeData(
primarySwatch: Colors.green,
colorScheme: ColorScheme.fromSwatch().copyWith(secondary: Colors.red),
canvasColor: const Color.fromRGBO(255, 245, 224, 1),
fontFamily: 'Raleway',
textTheme: ThemeData.light().textTheme.copyWith(
bodyText1: const TextStyle(
color: Color.fromRGBO(23, 45, 23, 1),
),
bodyText2: const TextStyle(
color: Color.fromRGBO(23, 45, 23, 1),
),
headline6: const TextStyle(
fontSize: 20,
fontFamily: 'RobotoCondensed',
fontWeight: FontWeight.bold,
),
),
),
home: const MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key}) : super(key: key);
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Card(
//child: Text(selectedMeal.ingredients[i]),
color: Theme.of(context).colorScheme.secondary,
),
),
);
}
}
想为 Card 使用强调色,由于不推荐使用 accentColor,所以我改用了 colorScheme。在 MaterialApp 的 themeData 中描述了 colorScheme。但最终无法将其用于卡。 显示错误:“参数类型 'ColorScheme' 无法分配给参数类型 'Color'”
这是来自 MaterialApp 的主题数据
theme: ThemeData(
primarySwatch: Colors.green,
colorScheme: ColorScheme.fromSwatch().copyWith(secondary: Colors.red),
canvasColor: Color.fromRGBO(255, 245, 224, 1),
fontFamily: 'Raleway',
textTheme: ThemeData.light().textTheme.copyWith(
bodyText1: TextStyle(
color: Color.fromRGBO(23, 45, 23, 1),
),
bodyText2: TextStyle(
color: Color.fromRGBO(23, 45, 23, 1),
),
headline6: TextStyle(
fontSize: 20,
fontFamily: 'RobotoCondensed',
fontWeight: FontWeight.bold,
),
),
),
这是使用它的卡片
Card(
child: Text(selectedMeal.ingredients[i]),
color: Theme.of(context).colorScheme,//error shows here
),
colorScheme
属性 是 ColorScheme
类型,而 color
需要 Color
类型。
ColorScheme
持有不同的颜色可以访问,例如:
color: Theme.of(context).colorScheme.secondary,
下面是一个完整的例子:
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Demo',
theme: ThemeData(
primarySwatch: Colors.green,
colorScheme: ColorScheme.fromSwatch().copyWith(secondary: Colors.red),
canvasColor: const Color.fromRGBO(255, 245, 224, 1),
fontFamily: 'Raleway',
textTheme: ThemeData.light().textTheme.copyWith(
bodyText1: const TextStyle(
color: Color.fromRGBO(23, 45, 23, 1),
),
bodyText2: const TextStyle(
color: Color.fromRGBO(23, 45, 23, 1),
),
headline6: const TextStyle(
fontSize: 20,
fontFamily: 'RobotoCondensed',
fontWeight: FontWeight.bold,
),
),
),
home: const MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key}) : super(key: key);
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Card(
//child: Text(selectedMeal.ingredients[i]),
color: Theme.of(context).colorScheme.secondary,
),
),
);
}
}