断言失败:第 5143 行第 16 行:'child is! ParentDataElement<ParentData>':不正确
Failed assertion: line 5143 pos 16: 'child is! ParentDataElement<ParentData>': is not true
我正在尝试使用卡片上的 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
。
对于底部 Container
和 height: double.infinity
,您需要删除它。如果您只想要背景颜色,请使用 backgroundColor
return Scaffold(
backgroundColor: yourColor,
body: ...
或者 customizing/image 使用 Stack
小部件。
您可以检查Layouts in Flutter。
我正在尝试使用卡片上的 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
。
对于底部 Container
和 height: double.infinity
,您需要删除它。如果您只想要背景颜色,请使用 backgroundColor
return Scaffold(
backgroundColor: yourColor,
body: ...
或者 customizing/image 使用 Stack
小部件。
您可以检查Layouts in Flutter。