flutter - “IconData”类型不是“Widget”类型的子类型
问题描述
浏览“向 Flutter 应用程序添加交互性”教程时,我一直运行到此错误消息:
“IconData”类型不是“Widget”类型的子类型
由于我没有经验,我不确定补救措施是什么。作为一个教程,我找到了答案代码并将其复制粘贴到 Visual Studio 中,只是为了看看它的外观。我收到了同样的错误。我做了一些查看堆栈溢出和github。我找到的示例在故障代码中专门要求 IconData。在教程代码中,它只调用一次 IconData,它是一个完全不同的小部件。
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
Widget titleSection = Container(
padding: const EdgeInsets.all(32),
child: Row(
children: [
Expanded(
/*1*/
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
/*2*/
Container(
padding: const EdgeInsets.only(bottom: 8),
child: Text(
'Oeschinen Lake Campground',
style: TextStyle(
fontWeight: FontWeight.bold,
),
),
),
Text(
'Kandersteg, Switzerland',
style: TextStyle(
color: Colors.grey[500],
),
),
],
),
),
FavoriteWidget(),
],
),
);
Color color = Theme.of(context).primaryColor;
Widget buttonSection = Container(
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
_buildButtonColumn(color, Icons.call, 'CALL'),
_buildButtonColumn(color, Icons.near_me, 'ROUTE'),
_buildButtonColumn(color, Icons.share, 'SHARE'),
],
),
);
Widget textSection = Container(
padding: const EdgeInsets.all(32),
child: Text(
'Lake Oeschinen lies at the foot of the Blüemlisalp in the Bernese '
'Alps. Situated 1,578 meters above sea level, it is one of the '
'larger Alpine Lakes. A gondola ride from Kandersteg, followed by a '
'half-hour walk through pastures and pine forest, leads you to the '
'lake, which warms to 20 degrees Celsius in the summer. Activities '
'enjoyed here include rowing, and riding the summer toboggan run.',
softWrap: true,
),
);
return MaterialApp(
title: 'Flutter layout demo',
home: Scaffold(
appBar: AppBar(
title: Text('Flutter layout demo'),
),
body: ListView(
children: [
Image.asset(
'images/lake.jpg',
width: 600,
height: 240,
fit: BoxFit.cover,
),
titleSection,
buttonSection,
textSection,
],
),
),
);
}
Column _buildButtonColumn(Color color, IconData icon, String label) {
return Column(
mainAxisSize: MainAxisSize.min,
mainAxisAlignment: MainAxisAlignment.center,
children: [
Icon(icon, color: color),
Container(
margin: const EdgeInsets.only(top: 8),
child: Text(
label,
style: TextStyle(
fontSize: 12,
fontWeight: FontWeight.w400,
color: color,
),
),
),
],
);
}
}
// #docregion FavoriteWidget
class FavoriteWidget extends StatefulWidget {
@override
_FavoriteWidgetState createState() => _FavoriteWidgetState();
}
// #enddocregion FavoriteWidget
// #docregion _FavoriteWidgetState, _FavoriteWidgetState-fields, _FavoriteWidgetState-build
class _FavoriteWidgetState extends State<FavoriteWidget> {
// #enddocregion _FavoriteWidgetState-build
bool _isFavorited = true;
int _favoriteCount = 41;
// #enddocregion _FavoriteWidgetState-fields
// #docregion _toggleFavorite
void _toggleFavorite() {
setState(() {
if (_isFavorited) {
_favoriteCount -= 1;
_isFavorited = false;
} else {
_favoriteCount += 1;
_isFavorited = true;
}
});
}
// #enddocregion _toggleFavorite
// #docregion _FavoriteWidgetState-build
@override
Widget build(BuildContext context) {
return Row(
mainAxisSize: MainAxisSize.min,
children: [
Container(
padding: EdgeInsets.all(0),
child: IconButton(
padding: EdgeInsets.all(0),
alignment: Alignment.centerRight,
icon: (_isFavorited ? Icon(Icons.star) : Icon(Icons.star_border)),
color: Colors.red[500],
onPressed: _toggleFavorite,
),
),
SizedBox(
width: 18,
child: Container(
child: Text('$_favoriteCount'),
),
),
],
);
}
// #docregion _FavoriteWidgetState-fields
}
该错误还专门引用了第 34 行:第 11 行,即 FavoriteWidget() 行。
感谢您的时间
解决方案
推荐阅读
- javascript - Canvas API - 为什么所有的计数值对于 rating_sum 都是“未定义的”?
- ruby-on-rails - 如何编写 Rails finder 方法来搜索 has_many 没有特定属性的模型?
- c# - 如何修复 MVC 下拉列表的对象错误
- c# - 允许数据在 WPF 中以多种不同方式显示的 MVVM 方式
- c++ - 如何将参数传递给使用模板的类
- google-colaboratory - Google Colab 无法访问启用了第三方限制的 github 私有“组织”存储库
- android - AWS Facial Rekognition 尝试在 Android 应用程序中使用 BasicAWSCredentials 描述集合
- c++ - C++ 中的 dynamic_cast 问题
- google-apps-script - 根据价值在谷歌表中发送多行的电子邮件
- r - R Scripting - 在循环期间更新 DataFrame 的值