flutter Theme.of(context).colorScheme没有预期的颜色

wi3ka0sx  于 2023-08-07  发布在  Flutter
关注(0)|答案(1)|浏览(149)

我决定写一个小应用程序来显示MaterialApp主题的ColorScheme中可用的所有颜色。
根据ColorScheme.fromSeed文档,这个构造函数生成一个ColorScheme,它是从给定的seedColor派生出来的。ColorScheme的30种颜色可以使用Theme.of(context).colorScheme.xxxx访问,其中xxxx是所需的颜色(例如初级、二级、三级等)。这是不可能的。下面的应用程序从绿色种子创建一个ColorScheme,并显示30个具有假定相应颜色的瓦片。颜色都变错了,大部分是蓝色。然而,appBar显示正确的绿色。我做错了什么?我到处寻找答案,但没有结果。如果你能帮忙的话,我将不胜感激。
相关文件:https://api.flutter.dev/flutter/material/ThemeData-class.htmlhttps://api.flutter.dev/flutter/material/ColorScheme-class.htmlhttps://api.flutter.dev/flutter/material/ColorScheme/ColorScheme.fromSeed.html

import 'package:flutter/material.dart';

void main() {
  runApp(const MainApp());
}

class MainApp extends StatelessWidget {
  const MainApp({super.key});

  @override
  Widget build(BuildContext context) {
    ColorScheme cs = Theme.of(context).colorScheme;
    return MaterialApp(
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.green),
      ),
      home: Scaffold(
        appBar: AppBar(title: const Center(child: Text('Colors'))),
        body: Center(
          child: ListView(children: [
            MyColorTile(cs.primary, 'primary'),
            MyColorTile(cs.onPrimary, 'onPrimary'),
            MyColorTile(cs.primaryContainer, 'primaryContainer'),
            MyColorTile(cs.onPrimaryContainer, 'onPrimaryContainer'),
            MyColorTile(cs.secondary, 'secondary'),
            MyColorTile(cs.onSecondary, 'onSecondary'),
            MyColorTile(cs.secondaryContainer, 'secondaryContainer'),
            MyColorTile(cs.onSecondaryContainer, 'onSecondaryContainer'),
            MyColorTile(cs.tertiary, 'tertiary'),
            MyColorTile(cs.onTertiary, 'onTertiary'),
            MyColorTile(cs.tertiaryContainer, 'tertiaryContainer'),
            MyColorTile(cs.onTertiaryContainer, 'onTertiaryContainer'),
            MyColorTile(cs.error, 'error'),
            MyColorTile(cs.onError, 'onError'),
            MyColorTile(cs.errorContainer, 'errorContainer'),
            MyColorTile(cs.onErrorContainer, 'onErrorContainer'),
            MyColorTile(cs.outline, 'outline'),
            MyColorTile(cs.outlineVariant, 'outlineVariant'),
            MyColorTile(cs.background, 'background'),
            MyColorTile(cs.onBackground, 'onBackground'),
            MyColorTile(cs.surface, 'surface'),
            MyColorTile(cs.onSurface, 'onSurface'),
            MyColorTile(cs.surfaceVariant, 'surfaceVariant'),
            MyColorTile(cs.onSurfaceVariant, 'onSurfaceVariant'),
            MyColorTile(cs.inverseSurface, 'inverseSurface'),
            MyColorTile(cs.onInverseSurface, 'onInverseSurface'),
            MyColorTile(cs.inversePrimary, 'inversePrimary'),
            MyColorTile(cs.shadow, 'shadow'),
            MyColorTile(cs.scrim, 'scrim'),
            MyColorTile(cs.surfaceTint, 'surfaceTint'),
          ]),
        ),
      ),
    );
  }
}

class MyColorTile extends StatelessWidget {
  final Color color;
  final String label;
  const MyColorTile(this.color, this.label, {super.key});

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.all(3.0),
      child: ListTile(
        tileColor: color,
        title: Text(label, style: const TextStyle(color: Colors.grey)),
      ),
    );
  }
}

字符串

4ktjp1zp

4ktjp1zp1#

您正在从尚未定义colorScheme的上下文初始化ColorScheme cs变量。试试这个

import 'package:flutter/material.dart';

void main(List<String> args) {
  runApp(MainApp());
}

class MainApp extends StatelessWidget {
  const MainApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.green),
      ),
      home: MainView(),
    );
  }
}

class MainView extends StatelessWidget {
  const MainView({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    ColorScheme cs = Theme.of(context).colorScheme;
    return Scaffold(
      appBar: AppBar(title: const Center(child: Text('Colors'))),
      body: Center(
        child: ListView(children: [
          MyColorTile(cs.primary, 'primary'),
          MyColorTile(cs.onPrimary, 'onPrimary'),
          MyColorTile(cs.primaryContainer, 'primaryContainer'),
          MyColorTile(cs.onPrimaryContainer, 'onPrimaryContainer'),
          MyColorTile(cs.secondary, 'secondary'),
          MyColorTile(cs.onSecondary, 'onSecondary'),
          MyColorTile(cs.secondaryContainer, 'secondaryContainer'),
          MyColorTile(cs.onSecondaryContainer, 'onSecondaryContainer'),
          MyColorTile(cs.tertiary, 'tertiary'),
          MyColorTile(cs.onTertiary, 'onTertiary'),
          MyColorTile(cs.tertiaryContainer, 'tertiaryContainer'),
          MyColorTile(cs.onTertiaryContainer, 'onTertiaryContainer'),
          MyColorTile(cs.error, 'error'),
          MyColorTile(cs.onError, 'onError'),
          MyColorTile(cs.errorContainer, 'errorContainer'),
          MyColorTile(cs.onErrorContainer, 'onErrorContainer'),
          MyColorTile(cs.outline, 'outline'),
          MyColorTile(cs.outlineVariant, 'outlineVariant'),
          MyColorTile(cs.background, 'background'),
          MyColorTile(cs.onBackground, 'onBackground'),
          MyColorTile(cs.surface, 'surface'),
          MyColorTile(cs.onSurface, 'onSurface'),
          MyColorTile(cs.surfaceVariant, 'surfaceVariant'),
          MyColorTile(cs.onSurfaceVariant, 'onSurfaceVariant'),
          MyColorTile(cs.inverseSurface, 'inverseSurface'),
          MyColorTile(cs.onInverseSurface, 'onInverseSurface'),
          MyColorTile(cs.inversePrimary, 'inversePrimary'),
          MyColorTile(cs.shadow, 'shadow'),
          MyColorTile(cs.scrim, 'scrim'),
          MyColorTile(cs.surfaceTint, 'surfaceTint'),
        ]),
      ),
    );
  }
}

class MyColorTile extends StatelessWidget {
  final Color color;
  final String label;
  const MyColorTile(this.color, this.label, {super.key});

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.all(3.0),
      child: ListTile(
        tileColor: color,
        title: Text(label, style: const TextStyle(color: Colors.grey)),
      ),
    );
  }
}

字符串

相关问题