【发布时间】:2021-10-15 02:00:50
【问题描述】:
我正在为我的列子项使用扩展小部件:
可重复使用的卡片:
class ReusableCard extends StatelessWidget {
final Widget cardChild;
final VoidCallback onTap;
final Color color;
ReusableCard({required this.cardChild, required this.onTap, required this.color});
@override
Widget build(BuildContext context) {
return GestureDetector(
onTap: onTap,
child: Container(
child: cardChild,
decoration: BoxDecoration(
color: color,
borderRadius: BorderRadius.circular(20)
),
margin: EdgeInsets.all(20),
),
);
}
}
这是我的主要专栏:
class BMICalculator extends State<MainBmi>{
Gender selectedGender = Gender.MALE;
Color maleColorSelected = Colors.black45;
Color femaleColorSelected = Colors.black45;
String _sliderValue = "180";
int _weightValue = 50;
int _ageValue = 20;
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData.dark(),
home: SafeArea(
child: Scaffold(
appBar: AppBar(
title: Text(
"BMI Calculator"
),
),
body: Center(
child: Column(
children: [
Expanded(
child: Row(
children: [
Expanded(
child: ReusableCard(
color: selectedGender == Gender.MALE ? Colors.black12 : maleColorSelected,
onTap: (){
setState(() {
selectedGender = Gender.MALE;
});
},
cardChild: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text("MALE", style: TextStyle(fontSize: 20),),
SizedBox(
height: 30,
),
Icon(
FontAwesomeIcons.mars,
size: 80,
)
],
),
),
),
Expanded(
child: ReusableCard(
color: selectedGender == Gender.FEMALE ? Colors.black12 : femaleColorSelected,
onTap: (){
setState(() {
selectedGender = Gender.FEMALE;
});
},
cardChild: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text("FEMALE", style: TextStyle(fontSize: 20),),
SizedBox(
height: 30,
),
Icon(
FontAwesomeIcons.venus,
size: 80,
)
],
),
)
)
]
),
),
Expanded(
child: ReusableCard(
color: reusableCardColor,
onTap: (){
print("Hello");
},
cardChild: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
"HEIGHT",
style: TextStyle(fontSize: 20),
textAlign: TextAlign.center,
),
SizedBox(
height: 30,
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
textBaseline: TextBaseline.alphabetic,
children: [
Text(
_sliderValue,
style: TextStyle(
fontSize: 35,
fontWeight: FontWeight.bold,
textBaseline: TextBaseline.alphabetic,
),
),
Text(
"cm",
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 25,
textBaseline: TextBaseline.alphabetic,
),
)
],
),
SliderTheme(
data: SliderThemeData(
activeTrackColor: Colors.white,
overlayColor: Colors.pink.shade50,
inactiveTrackColor: Colors.grey,
thumbColor: Colors.pink,
thumbShape: RoundSliderThumbShape(
enabledThumbRadius: 15,
),
overlayShape: RoundSliderOverlayShape(
overlayRadius: 30,
)
),
child: Slider(
value: double.parse(_sliderValue),
min: 125,
max: 264,
onChanged: (a){
setState(() {
_sliderValue = a.toStringAsFixed(1);
});
},
),
)
],
)
)
),
Expanded(
child: Row(
children: [
Expanded(
child: ReusableCard(
cardChild: Column(
children: [
Text(
"WEIGHT",
style: TextStyle(
fontSize: 20,
color: Colors.white
),
),
SizedBox(
height: 20,
),
Text(
_weightValue.toString(),
style: TextStyle(
fontSize: 35,
fontWeight: FontWeight.bold,
),
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
ReusableFAB(
onTap: (){
setState(() {
_weightValue++;
});
},
iconData: FontAwesomeIcons.plus,
),
SizedBox(
width: 10,
),
ReusableFAB(
onTap: (){
setState(() {
_weightValue--;
});
},
iconData: FontAwesomeIcons.minus,
),
],
)
],
),
onTap: (){
print("hello world");
},
color: reusableCardColor,
),
),
Expanded(
child: ReusableCard(
cardChild: Column(
children: [
Text(
"AGE",
style: TextStyle(
fontSize: 20,
color: Colors.white
),
),
SizedBox(
height: 20,
),
Text(
_ageValue.toString(),
style: TextStyle(
fontSize: 35,
fontWeight: FontWeight.bold,
color: Colors.white
),
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
ReusableFAB(
iconData: FontAwesomeIcons.plus,
onTap: (){
setState(() {
_ageValue++;
});
},
),
SizedBox(
width: 20,
),
ReusableFAB(
iconData: FontAwesomeIcons.minus,
onTap: (){
setState(() {
_ageValue--;
});
},
)
],
)
],
),
onTap: (){
print("hello world");
},
color: reusableCardColor,
),
)
]
),
),
],
),
),
),
),
);
}
}
但是当我添加扩展的 Container 小部件时,一切都从屏幕溢出:
class BMICalculator extends State<MainBmi>{
Gender selectedGender = Gender.MALE;
Color maleColorSelected = Colors.black45;
Color femaleColorSelected = Colors.black45;
String _sliderValue = "180";
int _weightValue = 50;
int _ageValue = 20;
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData.dark(),
home: SafeArea(
child: Scaffold(
appBar: AppBar(
title: Text(
"BMI Calculator"
),
),
body: Center(
child: Column(
children: [
Expanded(
child: Row(
children: [
Expanded(
child: ReusableCard(
color: selectedGender == Gender.MALE ? Colors.black12 : maleColorSelected,
onTap: (){
setState(() {
selectedGender = Gender.MALE;
});
},
cardChild: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text("MALE", style: TextStyle(fontSize: 20),),
SizedBox(
height: 30,
),
Icon(
FontAwesomeIcons.mars,
size: 80,
)
],
),
),
),
Expanded(
child: ReusableCard(
color: selectedGender == Gender.FEMALE ? Colors.black12 : femaleColorSelected,
onTap: (){
setState(() {
selectedGender = Gender.FEMALE;
});
},
cardChild: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text("FEMALE", style: TextStyle(fontSize: 20),),
SizedBox(
height: 30,
),
Icon(
FontAwesomeIcons.venus,
size: 80,
)
],
),
)
)
]
),
),
Expanded(
child: ReusableCard(
color: reusableCardColor,
onTap: (){
print("Hello");
},
cardChild: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
"HEIGHT",
style: TextStyle(fontSize: 20),
textAlign: TextAlign.center,
),
SizedBox(
height: 30,
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
textBaseline: TextBaseline.alphabetic,
children: [
Text(
_sliderValue,
style: TextStyle(
fontSize: 35,
fontWeight: FontWeight.bold,
textBaseline: TextBaseline.alphabetic,
),
),
Text(
"cm",
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 25,
textBaseline: TextBaseline.alphabetic,
),
)
],
),
SliderTheme(
data: SliderThemeData(
activeTrackColor: Colors.white,
overlayColor: Colors.pink.shade50,
inactiveTrackColor: Colors.grey,
thumbColor: Colors.pink,
thumbShape: RoundSliderThumbShape(
enabledThumbRadius: 15,
),
overlayShape: RoundSliderOverlayShape(
overlayRadius: 30,
)
),
child: Slider(
value: double.parse(_sliderValue),
min: 125,
max: 264,
onChanged: (a){
setState(() {
_sliderValue = a.toStringAsFixed(1);
});
},
),
)
],
)
)
),
Expanded(
child: Row(
children: [
Expanded(
child: ReusableCard(
cardChild: Column(
children: [
Text(
"WEIGHT",
style: TextStyle(
fontSize: 20,
color: Colors.white
),
),
SizedBox(
height: 20,
),
Text(
_weightValue.toString(),
style: TextStyle(
fontSize: 35,
fontWeight: FontWeight.bold,
),
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
ReusableFAB(
onTap: (){
setState(() {
_weightValue++;
});
},
iconData: FontAwesomeIcons.plus,
),
SizedBox(
width: 10,
),
ReusableFAB(
onTap: (){
setState(() {
_weightValue--;
});
},
iconData: FontAwesomeIcons.minus,
),
],
)
],
),
onTap: (){
print("hello world");
},
color: reusableCardColor,
),
),
Expanded(
child: ReusableCard(
cardChild: Column(
children: [
Text(
"AGE",
style: TextStyle(
fontSize: 20,
color: Colors.white
),
),
SizedBox(
height: 20,
),
Text(
_ageValue.toString(),
style: TextStyle(
fontSize: 35,
fontWeight: FontWeight.bold,
color: Colors.white
),
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
ReusableFAB(
iconData: FontAwesomeIcons.plus,
onTap: (){
setState(() {
_ageValue++;
});
},
),
SizedBox(
width: 20,
),
ReusableFAB(
iconData: FontAwesomeIcons.minus,
onTap: (){
setState(() {
_ageValue--;
});
},
)
],
)
],
),
onTap: (){
print("hello world");
},
color: reusableCardColor,
),
)
]
),
),
Expanded(
child: Container(
margin: EdgeInsets.all(20),
color: Colors.pink,
),
)
],
),
),
),
),
);
}
}
在我将扩展容器添加为子项之前,一切都是正确的。这是什么原因?
这是错误图片:
【问题讨论】: