【发布时间】:2026-01-21 13:40:01
【问题描述】:
我正在尝试创建类似这样的自定义主题数据。
class CustomTheme {
static ThemeData get lightTheme {
return ThemeData(
textTheme: TextTheme(
headline1: h1.copyWith(color: Color(0xff444444)),
headline2: h2.copyWith(color: Color(0xff444444)),
headline3: h3.copyWith(color: Color(0xff444444)),
headline4: h4.copyWith(color: Color(0xff444444)),
headline5: h5.copyWith(color: Color(0xff444444)),
headline6: h6.copyWith(color: Color(0xff444444)),
subtitle1: TextStyle(color: Color(0xff444444)),
subtitle2: TextStyle(color: Color(0xff444444)),
bodyText1: TextStyle(color: Color(0xff444444)),
bodyText2: TextStyle(color: Color(0xff444444)),
),
textButtonTheme: TextButtonThemeData(
style: TextButton.styleFrom(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(0.0),
side: BorderSide(color: kButtonBorderColor)),
backgroundColor: kButtonBackgroundColor,
),
),
);
}
static ThemeData get darkTheme {
return ThemeData(
textTheme: TextTheme(
headline1: h1.copyWith(color: Color(0xffebebeb)),
headline2: h2.copyWith(color: Color(0xffebebeb)),
headline3: h3.copyWith(color: Color(0xffebebeb)),
headline4: h4.copyWith(color: Color(0xffebebeb)),
headline5: h5.copyWith(color: Color(0xffebebeb)),
headline6: h6.copyWith(color: Color(0xffebebeb)),
subtitle1: TextStyle(color: Color(0xffebebeb)),
subtitle2: TextStyle(color: Color(0xffebebeb)),
bodyText1: TextStyle(color: Color(0xffebebeb)),
bodyText2: TextStyle(color: Color(0xffebebeb)),
),
textButtonTheme: TextButtonThemeData(
style: TextButton.styleFrom(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(0.0),
side: BorderSide(color: kButtonBorderColor)),
backgroundColor: kButtonBackgroundColorDark,
),
),
);
}
}
我的要求是,我有两种不同的按钮样式(明暗模式各 2 个),如下所示。
从我的代码中可以看出,对于明暗主题,我只能为按钮样式提供一种背景颜色。我无法在主题数据中设置额外的 textbuttontheme。如何在我的主题数据中设置这两种按钮样式?还是有任何其他方法可以处理浅色和深色主题的 2 种不同样式?
【问题讨论】:
-
试试这个itnext.io/…
-
@ghostdeathrider 问题不在于如何制作明暗主题,而是如何在明暗模式下同时拥有 2 个不同的按钮主题。
标签: flutter dart darkmode flutter-theme