【问题标题】:Flutter Error: The element type 'Button' can't be assigned to the list type 'Widget'Flutter 错误:无法将元素类型“按钮”分配给列表类型“小部件”
【发布时间】:2020-06-09 21:45:58
【问题描述】:

我不知道为什么我会给出这个错误。我在这里写了 button.dart 文件:

 import 'package:flutter/material.dart';

class Button {
  double left;
  double top;
  String text;
  Button(this.text,this.top,
  this.left);

  Widget button(){
  return Positioned(
    left:left,

    top: top,
   child:Container(
          height: 54,
          width: 157,

          color: Colors.transparent,
          child:  Container(
            decoration: BoxDecoration(
              color: Colors.white,
              borderRadius: BorderRadius.all(Radius.circular(49))
            ),
            child:  Center(
            child:  Text(text,
            style: TextStyle(
              fontFamily: 'MontserratAlternates',
              fontStyle: FontStyle.normal,
              fontWeight: FontWeight.bold,
              fontSize: 20,
              letterSpacing: -0.02,
              color:Colors.black,

            ),
            ),
           )
         ),),
        );

  }
}

我在 main.dart 文件中对其进行了编码,但它不起作用,然后我有一个错误“元素类型 Button 无法分配给列表类型'Widget'”我不明白为什么我有这个错误。 这是 main.dart :

import 'package:flutter/material.dart';
import './number.dart';
import'./image.dart';
import './button.dart';
void main() {
  runApp(Main());
}

class Main extends StatelessWidget {

  @override 
  Widget build(BuildContext context){



    return MaterialApp(
      title: 'Main',
      home: Scaffold(
        body: Container(

          child: Stack(
          children:<Widget> [

            image(context),
            Number(),
            Button("Start",412,45),


          ],
      ),
        ),
    ),
    );
  }
}

系统在警告我,所以我不应该像大多数代码那样写。

【问题讨论】:

  • 正如它所说,你的类按钮没有扩展/实现小部件,你应该让你的按钮扩展一个无状态小部件并用你的定位小部件覆盖构建方法
  • 为什么?这些的逻辑是什么?为什么我不做一个没有无状态小部件的课程?
  • 如果你想使用你的类,应该是子列表里面的Button("Start",412,45).button(),它可以工作,也不是真的错,但是StatelessWidget/StatefulWidget是你可以用来实现你想要的东西的基本小部件对象

标签: flutter dart widget syntax-error flutter-dependencies


【解决方案1】:

您的自定义小部件应继承自 StatefulWidget 或 StatelessWidget。通过扩展其中任何一个,Flutter 将把你的类识别为一个小部件。

class Button extends StatelessWidget {
  final double left;
  final double top;
  final String text;

  Button(this.text, this.top, this.left);

  @override
  Widget build(BuildContext context) {
    return Positioned(
      left: left,
      top: top,
      child: Container(
        height: 54,
        width: 157,
        color: Colors.transparent,
        child: Container(
          decoration: BoxDecoration(
              color: Colors.white,
              borderRadius: BorderRadius.all(Radius.circular(49))),
          child: Center(
            child: Text(
              text,
              style: TextStyle(
                fontFamily: 'MontserratAlternates',
                fontStyle: FontStyle.normal,
                fontWeight: FontWeight.bold,
                fontSize: 20,
                letterSpacing: -0.02,
                color: Colors.black,
              ),
            ),
          ),
        ),
      ),
    );
  }
}

否则,如果您想使用 button 函数执行此操作,则需要在创建 Button 后调用该函数。

import 'package:flutter/material.dart';
import './number.dart';
import './image.dart';
import './button.dart';

void main() {
  runApp(Main());
}

class Main extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Main',
      home: Scaffold(
        body: Container(
          child: Stack(
            children: <Widget>[
              image(context),
              Number(),
              Button("Start", 412, 45).button(),
            ],
          ),
        ),
      ),
    );
  }
}

【讨论】:

    【解决方案2】:

    我会将您的按钮类更改为如下所示:

    class Button {
      double left;
      double top;
      String text;
    
      Widget button(this.text,this.top,this.left){
      return Positioned(
        left:left,
    
        top: top,
       child:Container(
              height: 54,
              width: 157,
    
              color: Colors.transparent,
              child:  Container(
                decoration: BoxDecoration(
                  color: Colors.white,
                  borderRadius: BorderRadius.all(Radius.circular(49))
                ),
                child:  Center(
                child:  Text(text,
                style: TextStyle(
                  fontFamily: 'MontserratAlternates',
                  fontStyle: FontStyle.normal,
                  fontWeight: FontWeight.bold,
                  fontSize: 20,
                  letterSpacing: -0.02,
                  color:Colors.black,
    
                ),
                ),
               )
             ),),
            );
    
      }
    }
    

    然后像这样调用那个方法:

    import 'package:flutter/material.dart';
    import './number.dart';
    import'./image.dart';
    import './button.dart';
    void main() {
      runApp(Main());
    }
    
    class Main extends StatelessWidget {
    
      @override 
      Widget build(BuildContext context){
    
    
    
        return MaterialApp(
          title: 'Main',
          home: Scaffold(
            body: Container(
    
              child: Stack(
              children:<Widget> [
    
                image(context),
                Number(),
                button("Start",412,45),
    
    
              ],
          ),
            ),
        ),
        );
      }
    }
    

    【讨论】:

    • 感谢亚历克斯,但我现在犯了不同的错误。 ** : 方法 'button' 没有为类型 'Main' 定义。尝试将名称更正为现有方法的名称,或定义名为 'button' 的方法" **
    • 尝试将 Widget button(this.text,this.top,this.left) 更改为 Widget Button(this.text,this.top,this.left) 并调用 Button("Start",412,45)
    猜你喜欢
    • 2021-11-25
    • 1970-01-01
    • 1970-01-01
    • 2021-12-21
    • 1970-01-01
    • 2021-01-06
    • 2021-08-05
    • 2022-10-15
    • 2021-11-25
    相关资源
    最近更新 更多