首页 > 解决方案 > Flutter 找不到小部件属性:'child' 未定义

问题描述

因为很少有人发布一些错误的解决方案(这不是坏主意),我回复了他们然后他们删除了他们的帖子。我通过适度获得了标志......所以我在这里发布每个答案。

1/ 我正在尝试使用 child 和 onPressed 属性制作一个简单的 ElevatedButton。

2/ 我尝试遵循此处提供的文档:https ://api.flutter.dev/flutter/material/ElevatedButton-class.html

我也使用了示例,但每次我得到相同的错误:“未定义命名参数'child'。” ('onPressed' 也没有定义......')

3/ 自动完成属性的图片: ElevatedButton Properties

使用的 Flutter 版本: Flutter 2.0.4 • 通道稳定 • https://github.com/flutter/flutter.git 框架 • 修订 b1395592de(10 天前) • 2021-04-01 14:25:01 -0700 引擎 • 修订2dce47073a 工具 • Dart 2.12.2

这是完整的代码,我可以再次运行和调试,但我的 Visual Studio 编译器中仍然有错误,正如您在屏幕截图中看到的那样......我不明白为什么。

我按 F5 运行模拟器,然后我使用“fultter run”启动我的应用程序。

问题

    import 'package:flutter/material.dart';

void main() => runApp(MaterialApp(
      home: Home(),
    ));

class Home extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("HelloWorld"),
        centerTitle: true,
        backgroundColor: Colors.red[600],
      ),

      body: Center(
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceEvenly,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            Text("Hello World"),
            ElevatedButton.icon(
              label: Text("Click"),
              icon: Icon(Icons.airplanemode_active),
              onPressed: () {},
            ),
            Container(
              color: Colors.lightBlue,
              padding: EdgeInsets.all(20.0),
              child: Text("container"),
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        child: Icon(Icons.add),
        backgroundColor: Colors.red[600],
        onPressed: () {},
      ),
      
      bottomNavigationBar: BottomAppBar(
        child: Container(
          height: 45,
          width: 100,
        ),
       
        color: Colors.pink,
      ),
      floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
    );
  }
}

class TestFul extends StatefulWidget {
  @override
  _TestFulState createState() => _TestFulState();
}

class _TestFulState extends State<TestFul> {
  @override
  Widget build(BuildContext context) {
    return Container();
  }
}

感谢大家快速回复我的帖子。

令人惊讶的是,我可以使用我的模拟器调试和运行我的代码。但我仍然有错误,你可以在图片链接中看到: 问题

我试图清理/缓存/重新启动等...但没有任何改变。当然,我正在导入 material.dart 是否可能是 Flutter 2.0.4 版本的原因?

PS:我正在使用 Visual Studio Code 进行编码。我不认为不看错误的编码是一个好习惯^^'。

标签: flutterdart

解决方案


在此处输入图像描述

我尝试了您的代码,它在我的系统上完美执行。如果您仍然对此小部件(ElevatedButton)有问题,那么只需尝试使用一个简单的按钮并将其包装在 Card() 中

Card(
      elevation: 5,
      child: //Todo: button
)

推荐阅读