首页 > 解决方案 > IconData 没有作为参数传递给类实例化

问题描述

我定义了以下类:

    import 'package:flutter/material.dart';

class ItemHiddenMenu extends StatelessWidget {
  /// name of the menu item
  final String name;

  /// callback to recibe action click in item
  final Function onTap;

  final Color colorLineSelected;

  /// Base style of the text-item.
  final TextStyle baseStyle;

  /// style to apply to text when item is selected
  final TextStyle selectedStyle;

  final bool selected;

  final IconData icon;

  ItemHiddenMenu({
    Key key,
    this.name,
    this.selected = false,
    this.onTap,
    this.colorLineSelected = Colors.blue,
    this.baseStyle,
    this.selectedStyle,
    this.icon,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      width: double.infinity,
      margin: EdgeInsets.only(bottom: 15.0),
      child: InkWell(
        onTap: onTap,
        child: Row(
          children: <Widget>[
            ClipRRect(
              borderRadius: BorderRadius.only(
                  topRight: Radius.circular(4.0),
                  bottomRight: Radius.circular(4.0)),
              child: Container(
                height: 40.0,
                color: selected ? colorLineSelected : Colors.transparent,
                width: 5.0,
              ),
            ),
            Expanded(
              child: Container(
                margin: EdgeInsets.only(left: 20.0),
                child: Row(
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: [
                    Icon(icon),
                    SizedBox(
                      width: 15,
                    ),
                    Text(
                      name,
                      style: (this.baseStyle ??
                              TextStyle(color: Colors.grey, fontSize: 25.0))
                          .merge(this.selected
                              ? this.selectedStyle ??
                                  TextStyle(color: Colors.white)
                              : null),
                    ),
                  ],
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}

我正在尝试将 IconData 作为命名参数传递icon,如下所示:

new ItemHiddenMenu(
          icon: Icons.dashboard,
          name: "Investments by category",
          baseStyle:
              TextStyle(color: Colors.white.withOpacity(0.8), fontSize: 20.0),
          colorLineSelected: Colors.teal,
        )

代码运行良好,但图标不显示。出于测试目的,我尝试icon在类本身内部定义它并且它可以工作,但是我所有的实例当然都会有相同的图标,这不是我想要的。

我在这里想念什么?

标签: flutterdart

解决方案


我通过传递它来尝试您的代码IconData它有效,图标可见。

您可以尝试传递和Icon小部件而不是IconData


推荐阅读