flutter 打印出ThemeData的当前属性

rkkpypqq  于 2023-11-21  发布在  Flutter
关注(0)|答案(1)|浏览(110)

是否可以打印出您的主题数据的值?
我喜欢打印(ThemeData);
把每一处房产都列出来
我需要找出谁是罪魁祸首tinging我的对话框紫色,你可以看到下面。
FYI:我已经定制了对话框主题数据,所以我怀疑它是:

dialogTheme: const DialogTheme(
            backgroundColor: canvasColor,
            shadowColor: Colors.black,
            surfaceTintColor: Colors.transparent,
        ),

字符串
这是来自名为:datetime_picker_formfield_new x1c 0d1x的包

bvk5enib

bvk5enib1#

所以我想通了,你需要设置新的材料3属性surfacetint为透明,然后让日期选择器表单脱颖而出,改变标题和背景颜色如下:

final ColorScheme colorScheme = const ColorScheme.light().copyWith(
        primary: primaryColor,
        secondary: secondaryColor,
        background: backgroundColor,
        error: errorColor,
        surfaceTint: Colors.transparent,
    );
        datePickerTheme: const DatePickerThemeData(
            headerBackgroundColor: primaryColor,
            backgroundColor: canvasColor,
        ),

字符串
以下是我的完整主题数据,如果有人感兴趣:

//flutter packages
import 'package:flutter/material.dart';
import 'package:zandu_mobile_admin/constants/main.dart';

//issues
//ignore_for_file: constant_identifier_names

//notes
// https://resocoder.com/2019/08/09/switch-themes-with-flutter-bloc-dynamic-theming-tutorial-dark-light-theme/
//fonts that are loaded that we can use are: Jost, Muli, Oswald, WorkSans, Comfortaa, Baloo2, RalewayDotsRegular, Spartan, DancingScript, Poppins, GoogleSans
//WorkSans i like a lot, Muli is good but looks very similar to WorksSans, Comfortaa is pretty good, GoogleSans sucks, Poppins is ok, Oswald sucks, DancingScript is handwritting and is mostly used to test the themes

//set the main font name here instead of in 3 places
const String mainFont = 'Poppins';

enum AppTheme {
    DefaultLight,
    DefaultDark,
}

final appThemeData = {
    AppTheme.DefaultLight: _buildLightTheme(),
    AppTheme.DefaultDark: _buildDarkTheme(),
};

ThemeData _buildLightTheme() {
    const Color primaryColor = Color(colors_zanduadminprimary);
    const Color secondaryColor = Color(colors_zanduadminaccent);
    const Color errorColor = Color(0xFFB00020);
    const Color canvasColor = Colors.white;
    const Color dividerColor = Color(0xFF999999);
    const Color scaffoldBackgroundColor = Colors.white;
    const Color splashColor = Colors.white24;
    const Color backgroundColor = Colors.white;
    const Color indicatorColor = Colors.white;
    //const Color testingColor = Colors.purple;
    final ColorScheme colorScheme = const ColorScheme.light().copyWith(
        primary: primaryColor,
        secondary: secondaryColor,
        background: backgroundColor,
        error: errorColor,
        surfaceTint: Colors.transparent,
    );
    
    final ThemeData base = ThemeData(
        useMaterial3: true,
        brightness: Brightness.light,
        primaryColor: primaryColor,
        primaryColorLight: primaryColor,
        primaryColorDark: primaryColor,
        canvasColor: canvasColor,
        scaffoldBackgroundColor: scaffoldBackgroundColor,
        dividerColor: dividerColor,
        splashColor: splashColor,
        splashFactory: InkRipple.splashFactory,
        dialogBackgroundColor: Colors.orange,
        buttonTheme: ButtonThemeData(
            colorScheme: colorScheme,
            textTheme: ButtonTextTheme.primary,
        ),
        dividerTheme: const DividerThemeData(
            color: dividerColor,
        ),
        indicatorColor: indicatorColor,
        fontFamily: mainFont,
        textTheme: const TextTheme(
            displayLarge: TextStyle(fontSize: 26.0, fontWeight: FontWeight.w500, color: Colors.black),
            displayMedium: TextStyle(fontSize: 24.0, fontWeight: FontWeight.w500, color: Colors.black),
            displaySmall: TextStyle(fontSize: 22.0, fontWeight: FontWeight.w500, color: Colors.black),
            headlineLarge: TextStyle(fontSize: 20.0, fontWeight: FontWeight.w500, color: Colors.black),
            headlineMedium: TextStyle(fontSize: 18.0, fontWeight: FontWeight.w500, color: Colors.black),
            headlineSmall: TextStyle(fontSize: 16.0, fontWeight: FontWeight.w500, color: Colors.black),
            titleLarge: TextStyle(fontSize: 14.0, fontWeight: FontWeight.w500, color: Colors.black),
            titleMedium: TextStyle(fontSize: 14.0, fontWeight: FontWeight.w500, color: Colors.black),
            titleSmall: TextStyle(fontSize: 12.0, fontWeight: FontWeight.w500, color: Colors.black),
            bodyLarge: TextStyle(fontSize: 16.0, fontWeight: FontWeight.w500, color: Colors.black), //default text when no style is defined
            bodyMedium: TextStyle(fontSize: 14.0, fontWeight: FontWeight.w500, color: Colors.black),
            bodySmall: TextStyle(fontSize: 12.0, fontWeight: FontWeight.w500, color: Colors.black),
            labelLarge: TextStyle(color: Colors.black),//not used anywhere and the font size was never set
            labelMedium: TextStyle(color: Colors.black),//not used anywhere and the font size was never set
            labelSmall: TextStyle(color: Colors.black),//not used anywhere and the font size was never set
        ),
        appBarTheme: const AppBarTheme(
            color: primaryColor,//same as backgroundColor
            //backgroundColor: Colors.xxx,
            //foregroundColor: Colors.xxx,
            iconTheme: IconThemeData(
                color: Colors.white,
            )
        ),
        tabBarTheme: TabBarTheme(
            indicatorColor: Colors.white,
            dividerColor: primaryColor,
            labelColor: Colors.white,
            unselectedLabelColor: Colors.white.withOpacity(0.5),
        ),
        listTileTheme: ListTileThemeData(
            iconColor: Colors.black.withOpacity(0.5),
        ),
        switchTheme: SwitchThemeData(
            //the color inside the switch //FLUTTER-SENSEI-0025
            trackColor: MaterialStateProperty.resolveWith((states) => states.contains(MaterialState.selected) ? primaryColor : Colors.black.withOpacity(0.2)),
            //the border around the switch //FLUTTER-SENSEI-0025
            trackOutlineColor: MaterialStateProperty.resolveWith((states) => states.contains(MaterialState.selected) ? Colors.transparent : Colors.transparent),
            //we were never able to match the grey of the track color using Colors.black.withOpacity(0.2). Something is happening to the colors when it is applied.
            //trackOutlineColor: MaterialStateProperty.resolveWith((states) => states.contains(MaterialState.selected) ? primaryColor : Colors.black.withOpacity(0.2)),
            thumbColor: MaterialStateProperty.all(Colors.white),//the round selector we slide
            overlayColor: MaterialStateProperty.all(Colors.transparent),//when you grab it to slide it
        ),
        inputDecorationTheme: InputDecorationTheme(
            border: OutlineInputBorder(
                borderRadius: BorderRadius.circular(10),
                borderSide: BorderSide(width: 1.0, color: Colors.black.withOpacity(0.5)),
            ),
            enabledBorder: OutlineInputBorder(
                borderRadius: BorderRadius.circular(10),
                borderSide: BorderSide(width: 1.0, color: Colors.black.withOpacity(0.5))
            ),
            focusedBorder: OutlineInputBorder(
                borderRadius: BorderRadius.circular(10),
                borderSide: const BorderSide(width: 2, color: primaryColor)
            ),
            disabledBorder: OutlineInputBorder(
                borderRadius: BorderRadius.circular(10),
                borderSide: const BorderSide(width: 1.0)
            ),
        ),
        drawerTheme: const DrawerThemeData(
            backgroundColor: canvasColor,
        ),
        dialogTheme: const DialogTheme(
            backgroundColor: canvasColor,
            shadowColor: Colors.black,
        ),
        datePickerTheme: const DatePickerThemeData(
            headerBackgroundColor: primaryColor,
            backgroundColor: canvasColor,
        ),
        colorScheme: colorScheme,
    );
    
    return base.copyWith(
        textTheme: _buildTextTheme(base.textTheme),
        primaryTextTheme: _buildTextTheme(base.primaryTextTheme),
    );
    
}

ThemeData _buildDarkTheme() {
    const Color primaryColor = Color(colors_zanduadminprimary);
    const Color secondaryColor = Color(colors_zanduadminaccent);
    const Color errorColor = Color(0xFFB00020);
    const Color canvasColor = Color(0xFF202124);
    const Color dividerColor = Color(0xFF999999);
    const Color scaffoldBackgroundColor = Color(0xFF202124);
    const Color splashColor = Colors.white24;
    const Color backgroundColor = Color(0xFF202124);
    const Color indicatorColor = Colors.white;
    //const Color testingColor = Colors.purple;
    final ColorScheme colorScheme = const ColorScheme.dark().copyWith(
        primary: primaryColor,
        secondary: secondaryColor,
        background: backgroundColor,
        error: errorColor,
        surfaceTint: Colors.transparent,
    );
    
    final ThemeData base = ThemeData(
        useMaterial3: true,
        brightness: Brightness.dark,
        primaryColor: primaryColor,
        primaryColorLight: primaryColor,
        primaryColorDark: primaryColor,
        canvasColor: canvasColor,
        scaffoldBackgroundColor: scaffoldBackgroundColor,
        dividerColor: dividerColor,
        splashColor: splashColor,
        splashFactory: InkRipple.splashFactory,
        dialogBackgroundColor: Colors.orange,
        buttonTheme: ButtonThemeData(
            colorScheme: colorScheme,
            textTheme: ButtonTextTheme.primary,
        ),
        dividerTheme: const DividerThemeData(
            color: dividerColor,
        ),
        indicatorColor: indicatorColor,
        //hintColor: Colors.xxx,
        fontFamily: mainFont,
        textTheme: const TextTheme(
            displayLarge: TextStyle(fontSize: 26.0, fontWeight: FontWeight.w600, color: Colors.white),
            displayMedium: TextStyle(fontSize: 24.0, fontWeight: FontWeight.w600, color: Colors.white),
            displaySmall: TextStyle(fontSize: 22.0, fontWeight: FontWeight.w600, color: Colors.white),
            headlineLarge: TextStyle(fontSize: 20.0, fontWeight: FontWeight.w500, color: Colors.white),
            headlineMedium: TextStyle(fontSize: 18.0, fontWeight: FontWeight.w600, color: Colors.white),
            headlineSmall: TextStyle(fontSize: 16.0, fontWeight: FontWeight.w600, color: Colors.white),
            titleLarge: TextStyle(fontSize: 14.0, fontWeight: FontWeight.w600, color: Colors.white),
            titleMedium: TextStyle(fontSize: 14.0, fontWeight: FontWeight.w500, color: Colors.white),
            titleSmall: TextStyle(fontSize: 12.0, fontWeight: FontWeight.w500, color: Colors.white),
            bodyLarge: TextStyle(fontSize: 16.0, fontWeight: FontWeight.w500, color: Colors.white), //default text when no style is defined
            bodyMedium: TextStyle(fontSize: 14.0, fontWeight: FontWeight.w500, color: Colors.white),
            bodySmall: TextStyle(fontSize: 12.0, fontWeight: FontWeight.w500, color: Colors.white),
            labelLarge: TextStyle(color: Colors.white),//not used anywhere and the font size was never set
            labelMedium: TextStyle(color: Colors.white),//not used anywhere and the font size was never set
            labelSmall: TextStyle(color: Colors.white),//not used anywhere and the font size was never set
        ),
        appBarTheme: const AppBarTheme(
            color: primaryColor,//same as backgroundColor
            //backgroundColor: Colors.xxx,
            //foregroundColor: Colors.xxx,
            iconTheme: IconThemeData(
                color: Colors.white,
            )
        ),
        tabBarTheme: TabBarTheme(
            indicatorColor: Colors.white,
            dividerColor: primaryColor,
            labelColor: Colors.white,
            unselectedLabelColor: Colors.white.withOpacity(0.5),
        ),
        listTileTheme: ListTileThemeData(
            iconColor: Colors.white.withOpacity(0.5),
        ),
        switchTheme: SwitchThemeData(
            //the color inside the switch //FLUTTER-SENSEI-0025
            trackColor: MaterialStateProperty.resolveWith((states) => states.contains(MaterialState.selected) ? primaryColor : Colors.white.withOpacity(0.2)),
            //the border around the switch //FLUTTER-SENSEI-0025
            trackOutlineColor: MaterialStateProperty.resolveWith((states) => states.contains(MaterialState.selected) ? Colors.transparent : Colors.transparent),
            //we were never able to match the grey of the track color using Colors.white.withOpacity(0.2). Something is happening to the colors when it is applied.
            //trackOutlineColor: MaterialStateProperty.resolveWith((states) => states.contains(MaterialState.selected) ? primaryColor : Colors.white.withOpacity(0.2)),
            thumbColor: MaterialStateProperty.all(Colors.white),//the round selector we slide
            overlayColor: MaterialStateProperty.all(Colors.transparent),//when you grab it to slide it
        ),
        inputDecorationTheme: InputDecorationTheme(
            border: OutlineInputBorder(
                borderRadius: BorderRadius.circular(10),
                borderSide: BorderSide(width: 1.0, color: Colors.white.withOpacity(0.5)),
            ),
            enabledBorder: OutlineInputBorder(
                borderRadius: BorderRadius.circular(10),
                borderSide: BorderSide(width: 1.0, color: Colors.white.withOpacity(0.5))
            ),
            focusedBorder: OutlineInputBorder(
                borderRadius: BorderRadius.circular(10),
                borderSide: const BorderSide(width: 2, color: primaryColor)
            ),
            disabledBorder: OutlineInputBorder(
                borderRadius: BorderRadius.circular(10),
                borderSide: const BorderSide(width: 1.0)
            ),
        ),
        drawerTheme: const DrawerThemeData(
            backgroundColor: canvasColor,
        ),
        dialogTheme: const DialogTheme(
            backgroundColor: canvasColor,
            shadowColor: Colors.black,
        ),
        datePickerTheme: const DatePickerThemeData(
            headerBackgroundColor: primaryColor,
            backgroundColor: canvasColor,
        ),
        colorScheme: colorScheme,
    );
    
    return base.copyWith(
        textTheme: _buildTextTheme(base.textTheme),
        primaryTextTheme: _buildTextTheme(base.primaryTextTheme),
    );
    
}

TextTheme _buildTextTheme(TextTheme base) {
    
    return base.copyWith(
        bodyMedium: base.bodyMedium!.copyWith(
            fontFamily: mainFont,
        ),
    );
    
}

相关问题