android - 如何在 Flutter 中使用不同色调的色板?

标签 android dart material-design flutter

在默认的 Flutter 应用程序代码中,我尝试更改以下代码

来自:

primarySwatch: Colors.blueGrey

到:

primarySwatch: Colors.blueGrey[500]

但这会引发错误:

 ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
I/flutter ( 4512): The following assertion was thrown building MyApp(dirty):
I/flutter ( 4512): type 'Color' is not a subtype of type 'MaterialColor' of 'primarySwatch' where
I/flutter ( 4512):   Color is from dart:ui
I/flutter ( 4512):   MaterialColor is from package:flutter/src/material/colors.dart
I/flutter ( 4512):   int is from dart:core
I/flutter ( 4512): 
I/flutter ( 4512): Either the assertion indicates an error in the framework itself, or we should provide substantially
I/flutter ( 4512): more information in this error message to help you determine and fix the underlying cause.
I/flutter ( 4512): In either case, please report this assertion by filing a bug on GitHub:
I/flutter ( 4512):   https://github.com/flutter/flutter/issues/new
I/flutter ( 4512): 
I/flutter ( 4512): When the exception was thrown, this was the stack:
I/flutter ( 4512): #0      new ThemeData (package:flutter/src/material/theme_data.dart:78:19)
I/flutter ( 4512): #1      MyApp.build (/data/user/0/com.hackathon.gunbanana/cache/gun_bananaEMVSSI/gun_banana/lib/main.dart:11:18)
I/flutter ( 4512): #2      StatelessElement.build (package:flutter/src/widgets/framework.dart:3678:28)
I/flutter ( 4512): #3      ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3625:15)
I/flutter ( 4512): #4      Element.rebuild (package:flutter/src/widgets/framework.dart:3478:5)
I/flutter ( 4512): #5      ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3605:5)
I/flutter ( 4512): #6      ComponentElement.mount (package:flutter/src/widgets/framework.dart:3600:5)
I/flutter ( 4512): #7      Element.inflateWidget (package:flutter/src/widgets/framework.dart:2890:14)
I/flutter ( 4512): #8      Element.updateChild (package:flutter/src/widgets/framework.dart:2693:12)
I/flutter ( 4512): #9      RenderObjectToWidgetElement._rebuild (package:flutter/src/widgets/binding.dart:852:16)
I/flutter ( 4512): #10     RenderObjectToWidgetElement.mount (package:flutter/src/widgets/binding.dart:823:5)
I/flutter ( 4512): #11     RenderObjectToWidgetAdapter.attachToRenderTree.<anonymous closure> (package:flutter/src/widgets/binding.dart:769:17)
I/flutter ( 4512): #12     BuildOwner.buildScope (package:flutter/src/widgets/framework.dart:2205:19)
I/flutter ( 4512): #13     RenderObjectToWidgetAdapter.attachToRenderTree (package:flutter/src/widgets/binding.dart:768:13)
I/flutter ( 4512): #14     BindingBase&GestureBinding&ServicesBinding&SchedulerBinding&PaintingBinding&RendererBinding&WidgetsBinding.attachRootWidget (package:flutter/src/widgets/binding.dart:657:7)
I/flutter ( 4512): #15     runApp (package:flutter/src/widgets/binding.dart:699:7)
I/flutter ( 4512): #16     main (/data/user/0/com.hackathon.gunbanana/cache/gun_bananaEMVSSI/gun_banana/lib/main.dart:3:16)
I/flutter ( 4512): #17     _startIsolate.<anonymous closure> (dart:isolate-patch/dart:isolate/isolate_patch.dart:279)
I/flutter ( 4512): #18     _RawReceivePortImpl._handleMessage (dart:isolate-patch/dart:isolate/isolate_patch.dart:165)
I/flutter ( 4512): ════════════════════════════════════════════════════════════════════════════════════════════════════

Message on the mobile

如何使用阴影?

最佳答案

TLDR

ThemeData(primarySwatch: Colors.lime),

不要

ThemeData(primarySwatch: Colors.lime.shade700),

primarySwatch 不是 一种 颜色。这是所有可能的 Material 色调。

如果您查看 ThemeData 的文档,它会说:

The primary color palette (the [primarySwatch]), chosen from one of the swatches defined by the material design spec. This should be one of the maps from the [Colors] class that do not have "accent" in their name.

这意味着在需要时 Material 组件将使用主要[500],但也可能使用其他色调!

其实primarySwatch是设置一堆不同颜色的捷径:

  • 原色
  • primaryColorLight/Dark
  • accentColor
  • ...

但是您可以根据需要单独覆盖它们,使用 Color(而不是 primarySwatch 需要的 MaterialColor)

关于android - 如何在 Flutter 中使用不同色调的色板?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49319333/

相关文章:

android - 如何制作底部应用栏或底部导航栏,如谷歌家庭应用?

android - 如何制作 ImageView "match_parent"?

json - _CastError(类型 '_InternalLinkedHashMap<dynamic, dynamic>' 不是类型转换中类型 'Map<String, dynamic>' 的子类型)

html - 如何使 Material Design 抽屉标题固定?

flutter - 为什么扩展的小部件在将其发布到 Play 商店后会破坏我的 UI

dart - CustomPainter Path Canvas 上的多余行

reactjs - Material-UI : Rating Component, 根据值改变颜色

java.lang.IllegalStateException : ViewPager 错误

android - 删除 arraylist 中自定义对象的重复值

android - Crashlytics 找不到 list