Flutter:“GameNameEnum”类型的值不能分配给“GameNameEnum”类型的变量

时间:2021-05-05 09:40:24

标签: flutter dart types widget

我的代码中出现了一个非常奇怪的错误。当我尝试使用 _dialogPlatform = newValue;或 _dialogGameName = newValue;我通过 VS 代码得到一个红色突出显示,说我不能将具有某种类型的值分配给相同类型的变量。 完全错误:无法将“平台”类型的值分配给“平台”类型的变量。 尝试更改变量的类型,或将右侧类型转换为“平台”。 请帮忙。

import 'package:all_gta_cheats/enums/enums.dart';
import 'package:all_gta_cheats/widgets/drop_down_menu.dart';
import 'package:flutter/material.dart';

class FilterDialogBox extends StatefulWidget {
  const FilterDialogBox({
    Key? key,
    required this.initialPlatformValue,
    required this.initialGameNameValue,
    required this.initialIsFavValue,
    required this.onApply,
  }) : super(key: key);

  final Platform initialPlatformValue;
  final GameNameEnum initialGameNameValue;
  final bool initialIsFavValue;
  final Function(
    Platform platform,
    GameNameEnum gameNameEnum,
    bool isFav,
  ) onApply;

  @override
  _FilterDialogBoxState createState() => _FilterDialogBoxState();
}

class _FilterDialogBoxState extends State<FilterDialogBox> {
  final TextStyle actionsTextStyle = TextStyle(fontSize: 18);

  late GameNameEnum _dialogGameName;
  late Platform _dialogPlatform;
  late bool _dialogIsFav;

  @override
  void initState() {
    super.initState();
    _dialogGameName = widget.initialGameNameValue;
    _dialogPlatform = widget.initialPlatformValue;
    _dialogIsFav = widget.initialIsFavValue;
  }

  @override
  Widget build(BuildContext context) {
    return AlertDialog(
      title: Text('Search Filter'),
      content: SingleChildScrollView(
        child: ListBody(
          children: <Widget>[
            DropDownMenu<Platform>(
              initialValue: _dialogPlatform,
              itemsList: Platform.values,
              onChanged: <Platform>(newValue) {
                //Platform value changes here
                _dialogPlatform = newValue;
                print(newValue.toString());
              },
            ),
            DropDownMenu<GameNameEnum>(
              initialValue: _dialogGameName,
              itemsList: GameNameEnum.values,
              onChanged: <GameNameEnum>(newValue) {
                //GameName value changes here
                _dialogGameName = newValue;
                print(newValue.toString());
              },
            ),
            SwitchListTile(
              title: const Text('Show only favourites'),
              value: _dialogIsFav,
              onChanged: (bool value) {
                setState(() {
                  _dialogIsFav = value;
                  print(_dialogIsFav);
                  //widget.dialogIsFav = value;
                });
              },
              secondary: const Icon(Icons.favorite),
            ),
          ],
        ),
      ),
      actions: <Widget>[
        TextButton(
          child: Text(
            'Cancel',
            style: actionsTextStyle,
          ),
          onPressed: () {
            Navigator.of(context).pop();
          },
        ),
        TextButton(
          child: Text(
            'Apply',
            style: actionsTextStyle,
          ),
          onPressed: () {
            widget.onApply(_dialogPlatform, _dialogGameName, _dialogIsFav);
            Navigator.of(context).pop();
          },
        ),
      ],
    );
  }
}

0 个答案:

没有答案