简体   繁体   English

Flutter 中没有错误,但未显示自定义小部件

[英]No errors in Flutter but custom widgets not showing

I am trying to make a ResuseableCard custom widget and IconContent custom widget.我正在尝试制作 ResuseableCard 自定义小部件和 IconContent 自定义小部件。

Problem: Reuseable card are empty.问题:可重复使用的卡是空的。 IconContent is not showing inside ReusueableCard IconContent 未显示在 ReusueableCard 内

What I have tried: I also failed at creating a cardChild property within Reuseable Card that has a Column containing the icon, sized box and label.我尝试过的:我也未能在可重复使用的卡片中创建一个 cardChild 属性,该属性有一个包含图标、大小框和 label 的列。 I did not see any errors when I tried this but the cards remained empty!当我尝试这个时,我没有看到任何错误,但卡片仍然是空的!

What has worked: When everything is spelled out within ReuseableCard (no separate IconContent widget or cardChild property) it works.什么有效:当 ReuseableCard(没有单独的 IconContent 小部件或 cardChild 属性)中说明所有内容时,它就可以工作。 But then all the cards look the same.但是所有的卡片看起来都一样。 Another thing that works is to separately code each card.另一件有效的事情是分别对每张卡进行编码。 I don't want repeated code.我不想重复代码。 The tutorial I follow works so why doesn't my code?我遵循的教程有效,为什么我的代码不起作用?

import 'package:flutter/material.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';

const bottomContainerHeight = 80.0;
const activeCardColor = Color(0xFF1D1E33);
const bottomContainerColor = Color(0xFFEB1555);
const labelColor = Color(0xFF76FF03);

class InputPage extends StatefulWidget {
@override

_ InputPageState createState() => _InputPageState(); _ InputPageState createState() => _InputPageState(); } }

class _InputPageState extends State<InputPage> {
@override


Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          backgroundColor: Color(0xFF0A0E21),
          title: Text('BMI CALCULATOR'),
          centerTitle: true,
        ),
        body: Column(
          children: [
            Expanded(
                child: Row(children: [
              Expanded(
                child: ReuseableCard(
                  colour: activeCardColor,
                  cardChild: IconContent(FontAwesomeIcons.venus, 'Female'),
                ),
              ),
              Expanded(
                  child: ReuseableCard(
                colour: activeCardColor,
                cardChild: Column(children: [
                  Icon(FontAwesomeIcons.mars, size: 80.0),
                  SizedBox(height: 15.0),
                  Text('Male',
                      style: TextStyle(
                          fontSize: 18.0, color: (Color(0xFF8d8E98)))),
                ]),
              )),
            ])),
            Expanded(
              child: ReuseableCard(colour: activeCardColor),
            ),
            Expanded(
              child: Row(
                children: [
                  Expanded(
                    child: ReuseableCard(colour: activeCardColor),
                  ),
                  Expanded(
                    child: ReuseableCard(colour: activeCardColor),
                  )
                ],
              ),
            ),
            Container(
              color: bottomContainerColor,
              margin: EdgeInsets.only(top: 10.0),
              width: double.infinity,
              height: 80.0,
            )
          ],
        ));
  }
}

class ReuseableCard extends StatelessWidget {
  ReuseableCard({@required colour, cardChild});
  Color? colour;
  Widget? IconContent;

  @override
  Widget build(BuildContext context) {
    return Container(
        child: IconContent,
        margin: EdgeInsets.all(15.0),
        decoration: BoxDecoration(
            color: Color(0xFF1D1E33),
            borderRadius: BorderRadius.circular(10.0)));
  }
}

class IconContent extends StatelessWidget {
  IconData? data;
  String label = 'label';

  IconContent(data, label);

  @override
  Widget build(BuildContext context) {
    return Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
          Icon(data, size: 80.0),
          SizedBox(height: 15.0),
          Text(label,
              style: TextStyle(
                fontSize: 18.0,
                color: labelColor,
              ))
        ]);
  }
}

I think this will do.我认为这会做到。

class ReuseableCard extends StatelessWidget {
  ReuseableCard({Key? key,required this.colour, this.cardChild}) : super(key: key);
  final Color colour;
  final Widget? cardChild;

  @override
  Widget build(BuildContext context) {
    return Container(
        child: cardChild?? const SizedBox(),
        margin: EdgeInsets.all(15.0),
        decoration: BoxDecoration(
            color: Color(0xFF1D1E33),
            borderRadius: BorderRadius.circular(10.0)));
  }
}

when you use当你使用

required必需的

you don't have to say it can be null, also don't user widgets directly if it can be null.你不必说它可以是 null,如果它可以是 null,也不要直接使用小部件。 if you want more help with your design, provide us your UI design what you trying to create.如果您在设计方面需要更多帮助,请向我们提供您尝试创建的 UI 设计。

You have not defined cardChild properly and are not calling it in the ReuseableCard.您没有正确定义 cardChild 并且没有在 ReuseableCard 中调用它。 I have made all the fields as required in the below code.我已经按照以下代码中的要求填写了所有字段。

import 'package:flutter/material.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';

const bottomContainerHeight = 80.0;
const activeCardColor = Color(0xFF1D1E33);
const bottomContainerColor = Color(0xFFEB1555);
const labelColor = Color(0xFF76FF03);

class InputPage extends StatefulWidget {
@override
_ InputPageState createState() => _InputPageState(); }

class _InputPageState extends State<InputPage> {
@override
 


Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          backgroundColor: Color(0xFF0A0E21),
          title: Text('BMI CALCULATOR'),
          centerTitle: true,
        ),
        body: Column(
          children: [
            Expanded(
                child: Row(children: [
                  Expanded(
                    child: ReuseableCard(
                      colour: activeCardColor,
                      cardChild: IconContent(data:FontAwesomeIcons.venus,label: 'Female'),
                    ),
                  ),
                  Expanded(
                      child: ReuseableCard(
                        colour: activeCardColor,
                        cardChild: Column(children: [
                          Icon(FontAwesomeIcons.mars, size: 80.0),
                          SizedBox(height: 15.0),
                          Text('Male',
                              style: TextStyle(
                                  fontSize: 18.0, color: (Color(0xFF8d8E98)))),
                        ]),
                      )),
                ])),
            Expanded(
              child: ReuseableCard(colour: activeCardColor),
            ),
            Expanded(
              child: Row(
                children: [
                  Expanded(
                    child: ReuseableCard(colour: activeCardColor),
                  ),
                  Expanded(
                    child: ReuseableCard(colour: activeCardColor),
                  )
                ],
              ),
            ),
            Container(
              color: bottomContainerColor,
              margin: EdgeInsets.only(top: 10.0),
              width: double.infinity,
              height: 80.0,
            )
          ],
        ));
  }
}

   
class ReuseableCard extends StatelessWidget {
  ReuseableCard({@required this.colour,@required this.cardChild});
  Color? colour;
  Widget? cardChild;//cardChild defined here so the ReuseableCard can use it

  @override
  Widget build(BuildContext context) {
    return Container(
        child: cardChild,//called here
        margin: EdgeInsets.all(15.0),
        decoration: BoxDecoration(
            color: Color(0xFF1D1E33),
            borderRadius: BorderRadius.circular(10.0)));
  }
}

class IconContent extends StatelessWidget {
  IconData? data;
  String? label;

  IconContent({@required this.data, @required this.label});

  @override
  Widget build(BuildContext context) {
    return Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
          Icon(data, size: 80.0),
          SizedBox(height: 15.0),
          Text(label??"label",
              style: TextStyle(
                fontSize: 18.0,
                color: labelColor,
              ))
        ]);
  }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM