【问题标题】:Is there a way to use colour in ColorScheme in other ThemeData?有没有办法在其他 ThemeData 的 ColorScheme 中使用颜色?
【发布时间】:2022-01-19 15:34:07
【问题描述】:
import 'package:flutter/material.dart';
import 'package:google_fonts/google_fonts.dart';

const ColorScheme colorScheme = ColorScheme(
  primary: Color.fromRGBO(48, 48, 48, 1),
  primaryVariant: Color.fromRGBO(38, 38, 38, 1),
  onPrimary: Colors.white,
  secondary: Color.fromRGBO(232, 163, 72, 1),
  secondaryVariant: Color.fromRGBO(168, 121, 59, 1),
  onSecondary: Colors.white,
  surface: Colors.white,
  onSurface: Color.fromRGBO(48, 48, 48, 1),
  background: Colors.white,
  onBackground: Color.fromRGBO(48, 48, 48, 1),
  error: Color.fromRGBO(214, 84, 84, 1),
  onError: Colors.white,
  brightness: Brightness.light,
);

const FloatingActionButtonThemeData fabTheme = FloatingActionButtonThemeData(
  
);

我正在为主题创建一个单独的 theme.dart 文件,并希望将所有主题保留在该文件中。

在这里,我想在FloatingActionButtonThemeData 中使用ColorScheme 中的颜色。我知道我只能在其祖先具有此方案的上下文中使用 ColorScheme。但是有没有办法实现我想要的?

【问题讨论】:

    标签: flutter dart


    【解决方案1】:

    你可以这样做

    return MaterialApp(
      theme: Theme.of(context).copyWith(
        colorScheme: colorScheme,
        floatingActionButtonTheme: fabTheme,
       // or Theme.of(context).floatingActionButtonTheme.copyWith(...),
      ),
      ....
    

    对于特定的小部件,用Theme小部件包装并提供主题

    Theme(
      data: Theme.of(context).copyWith(
        colorScheme: colorScheme,
        floatingActionButtonTheme: fabTheme,
      ),
      child: ...
    

    更多关于themes 的信息,我正在关注extending-the-parent-theme,这似乎是更好的选择。

    【讨论】:

      猜你喜欢
      • 2020-07-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-05
      • 1970-01-01
      • 2022-01-08
      相关资源
      最近更新 更多