gpt4 book ai didi

Flutter : icon ! = null 不正确

转载 作者:行者123 更新时间:2023-12-03 02:55:38 25 4
gpt4 key购买 nike

我不知道为什么当我将此代码添加到 GridView.count 时出现错误小工具

添加的代码是:

buildTile(context,1,'Conselors',null,Icons.person,'Contact with the',Colors.green,Colors.green[50]),

错误代码是:
The following assertion was thrown building Home(dirty, dependencies: [MediaQuery], state:
I/flutter ( 6241): HomeScreen#1419b):
I/flutter ( 6241): 'package:flutter/src/material/icon_button.dart': Failed assertion: line 159 pos 15: 'icon != null':
I/flutter ( 6241): is not true.
I/flutter ( 6241):
I/flutter ( 6241): Either the assertion indicates an error in the framework itself, or we should provide substantially
I/flutter ( 6241): more information in this error message to help you determine and fix the underlying cause.

完整代码是:
import 'package:flutter/material.dart';
import 'package:percent_indicator/percent_indicator.dart';

class Home extends StatefulWidget {

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

class HomeScreen extends State<Home> {
@override
Widget build(BuildContext context) {
return new Scaffold(
backgroundColor: Colors.white,
resizeToAvoidBottomPadding: false,
body: ListView(
children: <Widget>[
Stack(
children: <Widget>[
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
SizedBox(height: 20),
Container(
padding: EdgeInsets.fromLTRB(12, 0, 12, 0),
child: Center(
child: Text(
"The best application for gudance ",
style: TextStyle(
fontSize: 20, fontWeight: FontWeight.bold),
),
)),
SizedBox(height: 12),
Container(
child: LinearPercentIndicator(
padding: EdgeInsets.fromLTRB(17, 0, 20, 0),
fillColor: null,
progressColor: Colors.blue,
percent: 1,
width: MediaQuery.of(context).size.width,
lineHeight: 3,
backgroundColor: Colors.grey[200],
)),
SizedBox(
height: 12,
),
Container(
height: MediaQuery.of(context).size.height * 1,
child: buildGrid(context),
),
SizedBox(
height: 12,
),
Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(10),
gradient: LinearGradient(
begin: Alignment.topLeft,
end: Alignment.bottomRight,
colors: [
Colors.blue[600],
Colors.blue[500],
Colors.blue,
Colors.blue[400],
Colors.blue[300]
])),
margin: EdgeInsets.only(left: 12, right: 12),
)
],
)
],
)
],
));
}

@override
Widget buildGrid(BuildContext context) {
return GridView.count(
padding: EdgeInsets.fromLTRB(0, 5, 0, 5),
crossAxisCount: 2,
shrinkWrap: true,
childAspectRatio: (MediaQuery.of(context).size.width - 60 / 2) / 300,
children: <Widget>[
buildTile(context, 0, "Library", null, Icons.local_library, "Google Books",
Colors.blue, Colors.blue[50]),
buildTile(context,1,'Conselors',null,Icons.person,'Contact with the',Colors.green,Colors.green[50]),

],
);
}

int _selectedIndex = -1;

@override
Widget buildTile(BuildContext context, int index, String heading, Image image,
IconData icon, String itemCount, Color color, Color backgroundColor) {
return Container(
padding: EdgeInsets.only(
left: index == 0 || index == 2 || index == 4 ? 12 : 5,
top: 0,
right: index == 1 || index == 3 || index == 5 ? 12 : 5,
bottom: 10),
decoration: BoxDecoration(
color: Colors.white, borderRadius: BorderRadius.circular(10)),
child: GestureDetector(
onTap: () {
setState(() {
_selectedIndex = index;
});
switch(index){
case 0 :
print('its index 0');
break;
case 1 :
print('its index 1');
break;
case 2 :
print('its index 2');
break;
}
},
child: Card(
elevation: 3,
shape: RoundedRectangleBorder(
side: BorderSide(
width: 1.0,
style: BorderStyle.solid,
color: _selectedIndex == index && color != null
? color
: Colors.white),
borderRadius: BorderRadius.circular(10)),
color: Colors.white,
child: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Stack(
alignment: Alignment(0, 0),
children: <Widget>[
Container(
width: 43,
height: 43,
decoration: BoxDecoration(
color: backgroundColor,
borderRadius: BorderRadius.circular(15)),
),
index == 1
? IconButton(
icon: image,
onPressed: () {},
)
: Icon(
icon,
color: color,
),
index != 1
? SizedBox(
height: 4,
)
: SizedBox(
height: 0,
),
],
),
Text(
heading,
style: TextStyle(
color: Colors.black,
fontSize: 18,
fontWeight: FontWeight.w600),
),
SizedBox(
height: 4,
),
Text(
itemCount,
style: TextStyle(
color: Colors.grey,
fontSize: 12,
fontWeight: FontWeight.w300),
)
],
))),
),
// ),
);
}
}

最佳答案

你路过 IconButton Image ,当icon:参数必须是 Widget

关于Flutter : icon ! = null 不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60441597/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com