Why do I got 'Invalid constant value.' while trying to apply the theme that separated to another dart file? [Dart, Flutter]

huangapple go评论62阅读模式
英文:

Why do I got 'Invalid constant value.' while trying to apply the theme that separated to another dart file? [Dart, Flutter]

问题

当我尝试从另一个Dart文件加载主题数据时,出现了"无效的常量值"错误。

对于我的英语能力有限以及使用ChatGPT上传问题,我深感抱歉。
ChatGPT在这个问题上没有提供帮助。

我正试图将主题分离到一个单独的文件中,以便重构和重用源代码。然而,如标题中所述,我遇到了一个错误。有没有办法解决这个问题?

以下是main.dart和selfmade_basic_interface.dart文件。

main.dart

import 'package:flutter/material.dart';
import './interface/000_selfmade_basic_interface.dart';

void main() => runApp(const MyApp());

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

  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
      theme: selfmadeTheme, // <- 这是发生错误的地方。
      home: Scaffold(
        body: SizedBox(),
      ),
    );
  }
}

selfmade_basic_interface.dart

import 'package:flutter/material.dart';

ThemeData selfmadeTheme = ThemeData(
  primarySwatch: const MaterialColor(
    0xff424242,
    {
      50: Color(0xfffafafa),
      100: Color(0xfff5f5f5),
      200: Color(0xffeeeeee),
      300: Color(0xffe0e0e0),
      350: Color(0xffd6d6d6),
      400: Color(0xffbdbdbd),
      500: Color(0xff9e9e9e),
      600: Color(0xff757575),
      700: Color(0xff616161),
      800: Color(0xff424242),
      850: Color(0xff303030),
      900: Color(0xff212121)
    },
  ),
  scaffoldBackgroundColor: const Color(0xff303030),
  drawerTheme: const DrawerThemeData(
    backgroundColor: Color(0xff424242),
  ),
  listTileTheme: const ListTileThemeData(
    textColor: Colors.white,
    iconColor: Colors.white,
  ),
  textTheme: const TextTheme(
    bodySmall: TextStyle(
      color: Colors.white,
    ),
    bodyMedium: TextStyle(
      color: Colors.white,
    ),
  ),
  scrollbarTheme: ScrollbarThemeData(
    thumbColor: MaterialStateProperty.all(Colors.grey),
    thumbVisibility: MaterialStateProperty.all<bool>(true),
  ),
);
英文:

When I'm trying to load theme data from another dart file, 'Invalid constant value.' is occured.

I apologize for my limited proficiency in English and for using ChatGPT to upload my question.
And ChatGPT did not help in this issue.

I am attempting to separate the theme into a separate file for refactoring and reusability of the source code. However, as mentioned in the title, I am encountering an error. Is there any way to resolve this issue?

Here are main.dart and selfmade_basic_interface.dart files.

main.dart

import &#39;package:flutter.material.dart&#39;;
import &#39;./interface/000_selfmade_basic_interface.dart&#39;;

void main() =&gt; runApp(const MainApp());

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

  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
      theme: selfmadeTheme, // &lt;- Here is where an error occured.
      home: Scaffold(
        body: SizedBox(),
      ),
    );
  }
}

selfmade_basic_interface.dart

import &#39;package:flutter/material.dart&#39;;

ThemeData selfmadeTheme = ThemeData(
  primarySwatch: const MaterialColor(
    0xff424242,
    {
      50: Color(0xfffafafa),
      100: Color(0xfff5f5f5),
      200: Color(0xffeeeeee),
      300: Color(0xffe0e0e0),
      350: Color(0xffd6d6d6),
      400: Color(0xffbdbdbd),
      500: Color(0xff9e9e9e),
      600: Color(0xff757575),
      700: Color(0xff616161),
      800: Color(0xff424242),
      850: Color(0xff303030),
      900: Color(0xff212121)
    },
  ),
  scaffoldBackgroundColor: const Color(0xff303030),
  drawerTheme: const DrawerThemeData(
    backgroundColor: Color(0xff424242),
  ),
  listTileTheme: const ListTileThemeData(
    textColor: Colors.white,
    iconColor: Colors.white,
  ),
  textTheme: const TextTheme(
    bodySmall: TextStyle(
      color: Colors.white,
    ),
    bodyMedium: TextStyle(
      color: Colors.white,
    ),
  ),
  scrollbarTheme: ScrollbarThemeData(
    thumbColor: MaterialStateProperty.all(Colors.grey),
    thumbVisibility: MaterialStateProperty.all&lt;bool&gt;(true),
  ),
);

答案1

得分: 0

问题出在 const 关键字上。当您使用 const 关键字将 MaterialApp 小部件设为常量时,它要求主题也是常量。但它不是常量。因此,在 MaterialApp 之前移除 const 关键字将解决您的问题。

英文:

The issue was the const keyword. As you made the MaterialApp widget a constant using the const keyword, it expects the theme also a constant. But it is not constant. So removing const keyword before the MaterialApp will resolve your issue.

huangapple
  • 本文由 发表于 2023年6月16日 14:22:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/76487431.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定