Flutter错误:每个孩子必须准确地布置一次。在点击手势检测器时

时间:2020-08-26 13:46:23

标签: android android-studio flutter flutter-layout

已经开始处理颤振,如果得到帮助,将会遇到此问题。

具有这样的代码:

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
import 'genderBtns_content.dart';
import 'reusableContainer_cards.dart';

const bottomContainerHeight = 80.0;
const activeCardColor = Color(0xFF1D1E33);
const inactiveCardColor = Color(0xFF111328);

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

class _InputPageState extends State<InputPage> {
  Color maleCardColor = inactiveCardColor;
  Color femaleCardColor = inactiveCardColor;

  void updateColor(int gender) {
    if (gender == 1) {
      if (maleCardColor == inactiveCardColor) {
        maleCardColor = activeCardColor;
        femaleCardColor = inactiveCardColor;
      } else {
        maleCardColor = inactiveCardColor;
      }
    } else if (gender == 2) {
      if (femaleCardColor == inactiveCardColor) {
        femaleCardColor = activeCardColor;
        maleCardColor = inactiveCardColor;
      } else {
        femaleCardColor = inactiveCardColor;
      }
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold (
      appBar: AppBar(
        title: Text('BMI CALCULATOR'),
      ),
      body: Column(
        children: <Widget>[
          Expanded(
            child: Row(
              children: <Widget>[
                Expanded(
                  child: GestureDetector(
                    onTap: () {
                      setState(() {
                        updateColor(1);
                      });
                    },
                    child: ContainerReuse(
                      colour: maleCardColor,
                      cardChild: GenderColReuse(
                        icon: FontAwesomeIcons.mars,
                        label: 'MALE',
                      ),
                    ),
                  ),
                ),
                Expanded(
                  child: GestureDetector(
                    onTap: () {
                      setState(() {
                        updateColor(2);
                      });
                    },
                    child: ContainerReuse(
                      colour: femaleCardColor,
                      cardChild: GenderColReuse(
                        icon: FontAwesomeIcons.venus,
                        label: 'FEMALE',
                      ),
                    ),
                  ),
                ),
              ],
            ),
          ),
          // End of Gender Cards

          Expanded(
            child: Expanded(
              child: ContainerReuse(colour: activeCardColor),
            ),
          ),

          Expanded(
            child: Row(
              children: <Widget>[
                Expanded(
                  child: ContainerReuse(
                    colour: activeCardColor,
                  ),
                ),
                Expanded(
                  child: ContainerReuse(
                    colour: activeCardColor,
                  ),
                ),
              ],
            ),
          ),

          Container(
            margin: EdgeInsets.only(top: 10.0),
            color: Color(0xFFEB1555),
            width: double.infinity,
            height: bottomContainerHeight,
          ),
        ],
      ),
    );
  }
}


运行代码后,一切正常,直到我单击GestureDetector。它被认为可以在点击时更改背景色,可以更改,但是在点击另一个GestureDetector后应该删除背景色,但是在按下另一个按钮时会抛出一个错误:“每个孩子都必须布置一次。”引用第40行return Scaffold。我真的需要帮助,因为我还在学习颤抖,并且因此而陷入困境。

0 个答案:

没有答案