我在一个屏幕中使用了卡片小部件和容器,并在两者中设置了白色,它在容器中显示正确,但在卡片小部件中显示不正确。我已附上屏幕截图。在此屏幕截图中,代码和模拟器都进行了比较。
提前谢谢您 请注意,我已将应用程序用于浅色主题和深色主题。
ThemeData lightTheme(){
return ThemeData.light(useMaterial3: true).copyWith(
scaffoldBackgroundColor: Colors.white,
primaryColor: Colors.black,
primaryColorDark: Colors.white,
appBarTheme: AppBarTheme(
backgroundColor: Colors.white,
shadowColor: Colors.black,
elevation: 15,
foregroundColor: Colors.black,
titleTextStyle:TextStyle().copyWith(color: Colors.black,fontFamily: EAFontName.Muli,fontWeight: EAFontWeight.muliMedium),
),
textTheme: TextTheme(
titleSmall: TextStyle().copyWith(color: Colors.black,fontFamily: EAFontName.Muli,fontWeight: EAFontWeight.muliRegular,fontSize: EAFontSize.size10),
titleMedium: TextStyle().copyWith(color: Colors.black,fontFamily: EAFontName.Muli,fontWeight: EAFontWeight.muliSemiBold,fontSize: EAFontSize.size12),
titleLarge: TextStyle().copyWith(color: Colors.black,fontFamily: EAFontName.Muli,fontWeight: EAFontWeight.muliBold,fontSize: EAFontSize.size16),
),
bottomNavigationBarTheme: BottomNavigationBarThemeData(
backgroundColor: Colors.white54,
selectedItemColor: Colors.black,
unselectedItemColor: Colors.black54,
elevation: 16,
),
cardColor: Colors.white,
cardTheme: CardTheme(
color: Colors.white,
shadowColor: Colors.black,
elevation: 6,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(16),
// side: BorderSide(color: Colors.black,width: 1)
),
),
);
}
ThemeData darkTheme(){
return ThemeData.light(useMaterial3: true).copyWith(
scaffoldBackgroundColor: Colors.black,
primaryColor: Colors.white,
primaryColorDark: Colors.black,
appBarTheme: AppBarTheme(
backgroundColor: Colors.black,
shadowColor: Colors.white,
elevation: 15,
foregroundColor: Colors.white,
titleTextStyle: TextStyle().copyWith(color: Colors.white,fontFamily: EAFontName.Muli,fontWeight: EAFontWeight.muliMedium),
),
textTheme: TextTheme(
titleSmall: TextStyle().copyWith(color: Colors.white,fontFamily: EAFontName.Muli,fontWeight: EAFontWeight.muliRegular,fontSize: EAFontSize.size10),
titleMedium: TextStyle().copyWith(color: Colors.white,fontFamily: EAFontName.Muli,fontWeight: EAFontWeight.muliSemiBold,fontSize: EAFontSize.size12),
titleLarge: TextStyle().copyWith(color: Colors.white,fontFamily: EAFontName.Muli,fontWeight: EAFontWeight.muliBold,fontSize: EAFontSize.size16),
),
bottomNavigationBarTheme: BottomNavigationBarThemeData(
backgroundColor: Colors.black54,
selectedItemColor: Colors.white,
unselectedItemColor: Colors.white54,
elevation: 16,
),
cardColor: Colors.black,
cardTheme: CardTheme(
color: Colors.black,
shadowColor: Colors.white,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(16),
// side: BorderSide(color: Colors.white,width: 1)
),
elevation: 8
),
);
}
我也尝试将主题颜色添加到卡宽,但得到相同的结果。
提前致谢
使用
material 3
有一个新选项,即 surfaceTintColor
将其添加到卡片小部件中,并设置为您想要的内容,在此处阅读更多内容以迁移到 material 3
return Card(
// color: Colors.white,
surfaceTintColor: Colors.white,
elevation: 20,
child: Padding(
padding: const EdgeInsets.all(32.0),
child: Text(
'Hello, World!',
style: Theme.of(context).textTheme.headlineMedium,
),
),
);
在 Material3 中,卡片以其 surfaceTintColor 属性的默认颜色显示。因此,如果您希望它们显示为白色,那么您应该更改该属性。 如果您希望应用程序中的所有卡片都是白色的,可以按如下方式修改ThemeData:
// Modify the ThemeData
ThemeData(
// Other theme configurations...
cardTheme: CardTheme(
surfaceTintColor: Colors.white,
),
)
这会将 CardTheme 的 surfaceTintColor 设置为白色,确保应用程序中的所有卡片都具有白色外观。有关迁移到 material 3 的更多信息!
我使用
quick and dirty
实现了 Card
小部件,它似乎工作正常并正确显示 Material design
颜色。这是我的代码:
white
//imported google's material design library
import 'package:flutter/material.dart';
void main() {
runApp(
/**Our App Widget Tree Starts Here**/
MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Card widget demo'),
backgroundColor: Colors.white,
centerTitle: true,
), //AppBar
body: Center(
/** Card Widget **/
child: Card(
elevation: 50,
color: Colors.white,
child: SizedBox(
width: 300,
height: 200,
child: Padding(
padding: const EdgeInsets.all(20.0),
child: Column(
children: [
const Text(
'My card widget',
style: TextStyle(
fontSize: 15,
color: Colors.black,
), //Textstyle
), //Text
],
), //Column
), //Padding
), //SizedBox
), //Card
), //Center
), //Scaffold
) //MaterialApp
);
}
是否应用了任何其他
Theme
或 color
。Widget
的
surfaceTintColor
和 color
属性以获得所需的颜色。