我厌倦了一些事情。我正在学习一门课程,我正在尝试构建一个 BMI 计算器应用程序,但是当我按下它时,男女卡片的颜色没有更新。
我还使用了 print('Male is selected') 并且它在控制台中向我显示选择了 Gender.male 或 Gender.female 但颜色没有改变。我究竟做错了什么?我是扑的新手。谢谢。
这是代码
reusable_card.dart
import 'package:flutter/material.dart';
class ReusableCard extends StatelessWidget {
ReusableCard({@required this.colour, this.cardChild, this.onPress});
final Color colour;
final Widget cardChild;
final Function onPress;
@override
Widget build(BuildContext context) {
return GestureDetector(
onTap: onPress,
child: Container(
child: cardChild,
margin: EdgeInsets.all(15.0),
decoration: BoxDecoration(
color: Color(0xFF1D1E33),
borderRadius: BorderRadius.circular(10.0),
),
),
);
}
}
input_page.dart
Gender selectedGender;
enum Gender {
male,
female,
}
Expanded(
child: ReusableCard(
onPress: () {
setState(() {
selectedGender = Gender.female;
});
},
colour: selectedGender == Gender.female
? kActiveCardColour
: kInactiveCardColour,
cardChild: IconContent(
icon: FontAwesomeIcons.venus,
label: 'FEMALE',
),
),
),
常量.dart
const kActiveCardColour = Color(0xFF1D1E33);
const kInactiveCardColour = Color(0xFF111328);
答案 0 :(得分:0)
你没有使用你传递的颜色:
class ReusableCard extends StatelessWidget {
ReusableCard({@required this.colour, this.cardChild, this.onPress});
final Color colour;
final Widget cardChild;
final Function onPress;
@override
Widget build(BuildContext context) {
return GestureDetector(
onTap: onPress,
child: Container(
child: cardChild,
margin: EdgeInsets.all(15.0),
decoration: BoxDecoration(
//pass the colour here
color: Color(0xFF1D1E33),
borderRadius: BorderRadius.circular(10.0),
),
),
);
}
}
你的最终小部件应该是这样的:
class ReusableCard extends StatelessWidget {
ReusableCard({@required this.colour, this.cardChild, this.onPress});
final Color colour;
final Widget cardChild;
final Function onPress;
@override
Widget build(BuildContext context) {
return GestureDetector(
onTap: onPress,
child: Container(
child: cardChild,
margin: EdgeInsets.all(15.0),
decoration: BoxDecoration(
color: colour,
borderRadius: BorderRadius.circular(10.0),
),
),
);
}
}