提问者:小点点

类型'Color'不是Flutter中类型'MaterialColor'错误的子类型?


**

╞═══════════════════════════════════════════════════════════ 以下_TypeError被抛出构建器:类型'Color'不是类型'MaterialColor'的子类型

相关的导致错误的小部件是:MaterialApp文件:///D:/Flutter运动/Flutter运动/07/spanishaudioplayer/lib/main. dart:9:12

════════════════════════════════════════════════════════════════════════════════════════════════════ W/InputControlodManager(14486): start InputReason=8 W/IInputConnectionWrapper(14486):getExtractedText on inactive InputConnection W/IInputConnectionWrapper(14486):getTextBeforeCsor or on inactive InputConnection V/ActivityThread(14486):完成ActivityRecords{801579c token=android.os.BinderProxy@63e470c{com.example.spanishaudioplayer/com.example.spanishaudioplayer.MainActivity}}W/libEGL(14486):EGLNativeWindowType 0x76efee7010 disconnect失败W/libEGL(14486):EGLNativeWindowType 0x7713667010 disconnect失败V/ActivityThread(14486):处理窗口ActivityRecords{801579c token=android.os.BinderProxy@63e470c{com.example.spanishaudioplayer/com.example.sple

///////////////////////////////////////////////////////////////////////////////////////////////////// //main. dart

import 'package:flutter/material.dart';
import 'HomePage.dart';

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

class MyApp extends StatelessWidget {
    @override
    Widget build(BuildContext context) {
      return MaterialApp(
        debugShowCheckedModeBanner: false,
        title: 'Spanish Audio Numbers',
        theme: ThemeData(
          primarySwatch: Colors.teal,
        ),
        home: HomePage(),
      );
    }
  }

/////////////////////////////////////////////////////////////////////////////////////////////////////
//HomePage.dart

import 'package:flutter/material.dart';
import 'package:audioplayers/audio_cache.dart';
import 'NumberAudio.dart';

class HomePage extends StatefulWidget {
    @override
    _HomePageState createState() => _HomePageState();
  }

class _HomePageState extends State<HomePage> {
    AudioCache audioPlayer = AudioCache();
    List<NumberAudio> audioList = [
      NumberAudio("one.wav", Colors.red, "one"),
      NumberAudio("two.wav", Colors.blue, "two"),
      NumberAudio("three.wav", Colors.pink, "three"),
      NumberAudio("four.wav", Colors.orange, "four"),
      NumberAudio("five.wav", Colors.purple, "five"),
      NumberAudio("six.wav", Colors.cyan, "six"),
      NumberAudio("seven.wav", Colors.green, "seven"),
      NumberAudio("eight.wav", Colors.grey, "eight"),
      NumberAudio("nine.wav", Colors.yellow, "nine"),
      NumberAudio("ten.wav", Colors.black, "ten"),
    ];
    play(String audioFile) async {
      audioPlayer.play(audioFile);
    }

    @override
    Widget build(BuildContext context) {
      return Scaffold(
        appBar: AppBar(
          title: Text(
            'Spanish Numbers',
            style: TextStyle(
              color: Colors.white,
              fontSize: 24.0,
              fontWeight: FontWeight.bold,
            ),
          ),
          //backgroundColor: Colors.teal,
        ),
        body: Center(
          child: Column(
            children: [
              Image(
                image: AssetImage("images/logo.png"),
              ),
              Expanded(
                child: GridView.builder(
                  padding: EdgeInsets.all(10.0),
                  gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                    childAspectRatio: 1.0,
                    crossAxisCount: 2,
                    crossAxisSpacing: 5.0,
                    mainAxisSpacing: 5.0,
                  ),
                  itemCount: audioList.length,
                  itemBuilder: (context, index) => SizedBox(
                    height: 50.0,
                    width: 100.0,
                    child: RaisedButton(
                      child: Text(
                        audioList[index].buttonTitle,
                        style: TextStyle(
                          color: Colors.white,
                          fontSize: 24.0,
                          fontWeight: FontWeight.bold,
                        ),
                      ),
                      color: audioList[index].buttonColor,
                      onPressed: () {
                        play(audioList[index].audioFile);
                      },
                    ),
                  ),
                ),
              ),
            ],
          ),
        ),
      );
    }
}

/////////////////////////////////////////////////////////////////////////////////////////////////////
//NumberAudio.dart

import 'package:flutter/material.dart';

class NumberAudio {
    String audioFile;
    MaterialColor buttonColor;
    String buttonTitle;
    NumberAudio(String audioFile, MaterialColor buttonColor, String buttonTitle) {
      this.audioFile = audioFile;
      this.buttonColor = buttonColor;
      this.buttonTitle = buttonTitle;
    }
}

https://i.stack.imgur.com/LGfKS.png


共1个答案

匿名用户

Color不是MaterialColor的子类型。

theme: ThemeData(
  primarySwatch: Colors.teal,
),

这里主要Swatch的类型为MaterialColor,但您输入的Color. teal的类型为Color

如果你想创建MaterialColor的实例,请尝试这样做:

   static const MaterialColor colorPrimarySwatch = const MaterialColor(
    0xFFFFFFFF,
    const <int, Color>{
      50: const Color(0xFFFFFFFF),
      100: const Color(0xFFFFFFFF),
      200: const Color(0xFFFFFFFF),
      300: const Color(0xFFFFFFFF),
      400: const Color(0xFFFFFFFF),
      500: const Color(0xFFFFFFFF),
      600: const Color(0xFFFFFFFF),
      700: const Color(0xFFFFFFFF),
      800: const Color(0xFFFFFFFF),
      900: const Color(0xFFFFFFFF),
    },
  );

不要忘记将button onColor类型从MaterialColor更改为Color

class NumberAudio {
    String audioFile;
    Color buttonColor;
    String buttonTitle;

    NumberAudio(String audioFile, Color buttonColor, String buttonTitle) {
      this.audioFile = audioFile;
      this.buttonColor = buttonColor;
      this.buttonTitle = buttonTitle;
    }
}