gpt4 book ai didi

flutter - 断言失败 : line 5143 pos 16: 'child is! ParentDataElement' : is not true

转载 作者:行者123 更新时间:2023-12-02 15:56:25 25 4
gpt4 key购买 nike

我正在尝试使用卡片上的 Gesturedetector 小部件来启动 onTap 属性。我有两张卡片,男性和女性,每张卡片小部件都被按下一次,并且在我完全重新启动应用程序之前再也不会工作。我的意思是,如果我先按男卡,我将不得不运行 gradle 并重新启动才能按女卡。

这是我的代码:我是 flutter 的新手...我需要帮助

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
import 'package:bmi_calculator/Icon_content.dart';
import 'package:bmi_calculator/Reusable_card.dart';

const bottomContainerHeight = 70.0;
const activeCardColor = Color(0xFF272A4E);
const inactiveCardColor = Color(0xFF111328);
const bottomContainerColor = Color(0xffEB1555);

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

class _InputPageState extends State<InputPage> {

Color maleCardColor = inactiveCardColor;
Color femaleCardColor = inactiveCardColor;

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



@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('BMI CALCULATOR'),
),
body: Column(
children: [
Expanded(child: Row(
children: [
Expanded(
child: GestureDetector(
onTap: (){
setState(() {
updateColor(1);
});
print('male card was pressed');
},
child: Reusable_card(changeColor: maleCardColor,
cardChild: TopCardContent(
gender: FontAwesomeIcons.mars,
label: 'MALE',
),
),
),
),
Expanded(
child: GestureDetector(
onTap: (){
setState(() {
updateColor(2);
});
print('female was pressed');
},
child: Reusable_card(changeColor: femaleCardColor,
cardChild: TopCardContent(
label: 'FEMALE',
gender: FontAwesomeIcons.venus,
),
),
),
)
],
)),
Expanded(child: Expanded(
child: Reusable_card(changeColor: activeCardColor,),
)),
Expanded(child: Row(
children: [
Expanded(
child: Reusable_card(changeColor: activeCardColor,),
),
Expanded(
child: Reusable_card(changeColor: activeCardColor,),
)
],
)),
Container(
color: bottomContainerColor,
margin: EdgeInsets.only(top: 10.0),
width: double.infinity,
height: bottomContainerHeight,
)
],
)
);
}
}

这些是我得到的错误:

======== Exception caught by widgets library =======================================================
The following assertion was thrown while looking for parent data.:
Incorrect use of ParentDataWidget.

The following ParentDataWidgets are providing parent data to the same RenderObject:
- Expanded(flex: 1) (typically placed directly inside a Flex widget)
- Expanded(flex: 1) (typically placed directly inside a Flex widget)
However, a RenderObject can only receive parent data from at most one ParentDataWidget.

还有这个

======== Exception caught by widgets library =======================================================
The following assertion was thrown building _BodyBuilder:
'package:flutter/src/widgets/framework.dart': Failed assertion: line 5143 pos 16: 'child is! ParentDataElement<ParentData>': is not true.


Either the assertion indicates an error in the framework itself, or we should provide substantially more information in this error message to help you determine and fix the underlying cause.
In either case, please report this assertion by filing a bug on GitHub:
https://github.com/flutter/flutter/issues/new?template=2_bug.md

最佳答案

在您的代码段上替换

 Expanded(child: Expanded(
child: Reusable_card(changeColor: activeCardColor,),
)),

 Expanded(
child: Reusable_card(
changeColor: activeCardColor,),
)

从这里删除一个 Expanded,我们不需要用另一个 Expanded 包裹 Expanded

对于 height: double.infinity 的底部 Container,您需要删除它。如果您只想要背景颜色,请使用 backgroundColor

    return Scaffold(
backgroundColor: yourColor,
body: ...

或用于自定义/图像使用Stack小部件。

可以查看Layouts in Flutter .

关于flutter - 断言失败 : line 5143 pos 16: 'child is! ParentDataElement<ParentData>' : is not true,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71458205/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com