首页 > 解决方案 > 错误:找不到吸气剂:“小部件”。小部件.icon

问题描述

我正在创建一个卡片组件,其中包含作为儿童小部件的图标和文本。我初始化了这些图标并想在其子小部件中使用它们,但它给出了错误 在此处输入图像描述

错误显示在图像上方。它发生在 widget.icon、widget.title 上,如果我在 widget.subtitle 上使用它也会发生。以下是完整代码

import 'package:flutter/rendering.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
import 'package:api_example_app/constants.dart';
import 'package:flutter/widgets.dart';

class CardsParent extends StatefulWidget {
 const CardsParent({
   Key key,
   @required this.size,
   this.icon,
   this.title,
   this.subtitle,
 }) : super(key: key);

 final Size size;
 final IconData icon;
 final String title;
 final String subtitle;

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

class _CardsParentState extends State<CardsParent> {
 @override
 Widget build(BuildContext context) {
   return Container(
     width: 140,
     height: 100,
     child: Card(
       shape: RoundedRectangleBorder(
         borderRadius: BorderRadius.circular(15.0),
       ),
       color: Colors.white,
       elevation: 10,
       child: Column(
         mainAxisSize: MainAxisSize.min,
         children: <Widget>[
           const ListTile(
             leading: Icon(
               widget.icon,
               size: 50,
               color: kOrangeColor,
             ),
             title: Text('Temp',
                 style: TextStyle(fontSize: 18.0, color: kOrangeColor)),
             subtitle: Text('33C', style: TextStyle(color: kOrangeColor)),
           ),
         ],
       ),
     ),
   );
 }
}

我做错了什么吗?

标签: androidflutterwidget

解决方案


您必须const从 ListTile 中删除。


推荐阅读