Flutter : icon ! = null 不正确

标签 flutter dart

我不知道为什么当我将此代码添加到 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/

相关文章:

javascript - 如何通过在 flutter 中知道其值来删除 key ?

xcode - 无法在我的 Mac 中的设备或模拟器上构建 flutter

flutter - 如何在 flutter 中按键隐藏或显示小部件?

flutter - 在支架主体中显示TabBar

flutter - 如何在Flutter中编写网络通话?

flutter 底部溢出无限像素

list - 使用 ListView 时出现错误 : The named parameter 'children' isn't defined,

android - Dart Flutter Firebase FirebaseException([cloud_firestore/permission-denied] 调用者无权执行指定操作。)

flutter - 传递小部件类和简单传递返回小部件的函数有什么区别?

input - 如何在 Material 输入中验证输入文本的长度?