简体   繁体   English

为什么我的切换按钮有问题

[英]Why am i having problem with my toggle button

Hello guys I am total new to flutter so I was trying to build a BMI calculator, so i was trying to create a toggle switch between "MALE" and "FEMALE", so that when one card is clicked the other becomes inactive and i was trying to use a ternary code to achieve that大家好,我是 flutter 的新手,所以我试图构建一个 BMI 计算器,所以我试图在“MALE”和“FEMALE”之间创建一个切换开关,这样当一张卡被点击时,另一张卡变得不活跃,我是试图使用三元代码来实现这一点

but the problem is if i run the app on my emulator and if i click on the "MALE" part it doesn't get selected but if i click on the "FEMALE" part the "MALE" card get selected and the "FEMALE card remain inactive and the "MALE" card still remains active unless i reload the app but it doesnt solve the problem.但问题是如果我在我的模拟器上运行应用程序并且如果我点击“MALE”部分它不会被选中但是如果我点击“FEMALE”部分“MALE”卡会被选中并且“FEMALE 卡”保持非活动状态并且“MALE”卡仍然保持活动状态,除非我重新加载应用程序但它不能解决问题。

so please if someone can show me on how to fix that error.所以请如果有人可以告诉我如何解决该错误。 i will be happy for that我会为此感到高兴在此处输入图像描述

 enum Gender {
  male,
  female,
  empty,
}
class ReuseableCard extends StatelessWidget {
  ReuseableCard({required this.colour, this.cardChild, this.onPress});
  final Color colour;
  final Widget? cardChild;
  final VoidCallback? onPress;
  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: onPress,
      child: Container(
          child: cardChild,
          margin: EdgeInsets.all(15),
          decoration: BoxDecoration(
              color: colour, borderRadius: BorderRadius.circular(15))),
    );
  }
}

class _InputPageState extends State<InputPage> {
  Gender selectedGender = Gender.empty;
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Center(child: Text('BMI CALCULATOR')),
      ),
      body: Column(
        crossAxisAlignment: CrossAxisAlignment.stretch,
        children: [
          Expanded(
              child: Row(
            children: [
              Expanded(
                child: ReuseableCard(
                  cardChild: cardTitle(
                    fontIcon: FontAwesomeIcons.mars,
                    cardText: 'MALE',
                  ),
               //I DONT KNOW IF I AM WRONG HERE
                  colour: selectedGender == Gender.male
                      ? activeCardColor
                      : inactiveCardColor,
                ),
              ),
              Expanded(
                child: ReuseableCard(
                  onPress: () {
                    setState(() {
                      selectedGender = Gender.male;
                    });
                  },
                  cardChild: cardTitle(
                    fontIcon: FontAwesomeIcons.venus,
                    cardText: 'FEMALE',
                  ),
                  //I DONT KNOW IF I AM WRONG HERE
                  colour: selectedGender == Gender.female
                      ? activeCardColor
                      : inactiveCardColor,
                ),
              )
            ],
          )),
          Expanded(
            child: ReuseableCard(
              onPress: () {
                selectedGender = Gender.female;
              },
              colour: activeCardColor,
              cardChild: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  Text(
                    'HEIGHT',
                    style: labelTextStyle,
                  ),
                  Row(
                    mainAxisAlignment: MainAxisAlignment.center,
                    crossAxisAlignment: CrossAxisAlignment.baseline,
                    textBaseline: TextBaseline.alphabetic,
                    children: [
                      Text(
                        height.toString(),
                        style: numberStyle,
                      ),
                      Text('cm'),
                    ],
                  ),
                  SliderTheme(
                    data: SliderTheme.of(context).copyWith(
                        inactiveTrackColor: Color(0xFF8D8E98),
                        thumbColor: Color(0xFFEB1555),
                        overlayColor: Color(0x29EB1555),
                        activeTrackColor: Colors.white,
                        thumbShape:
                            RoundSliderThumbShape(enabledThumbRadius: 20.0),
                        overlayShape:
                            RoundSliderOverlayShape(overlayRadius: 30.0)),
                    child: Slider(
                      value: height.toDouble(),
                      min: 120.0,
                      max: 220.0,
                      onChanged: (double newValue) {
                        setState(() {
                          height = newValue.round();
                        });
                      },
                    ),
                  ),
            ],

在此处输入图像描述

I can see that your male box does not have method onPress so it will not do anything and your female box onPress set the value to male so that clicking female box will make male active and does nothing to female box and for some reason your height box sets value to female .我可以看到你的男性盒子没有方法onPress所以它不会做任何事情,你的女性盒子onPress将值设置为male ,这样点击女性盒子会使男性活跃并且对女性盒子没有任何作用,并且由于某种原因你的height盒子将值设置为female

you need to move the onPress methods one level up ⬆️您需要将onPress方法上移一级⬆️

the one on female box move it to -> male box母盒上的那个将它移到 -> 公盒

the one on height box move it to -> female box高度框将其移至 -> 女性框

and this should fix the issue.这应该可以解决问题。

Yemi from the screenshot you posted make the following changes to the female card...从您发布的屏幕截图中,Yemi 对女性卡进行了以下更改...

// from this
onPress: (){
selectedGender = Gender.male;
}

// to this
onPress: (){
setState((){
selectedGender = Gender.female;
});
}

And I would suggest that you take your time and rewatch the module from the tutorial you are watching - I assume you are following Angela yu's course.我建议您花点时间重新观看您正在观看的教程中的模块 - 我假设您正在学习 Angela yu 的课程。 Plus if you feel stressed out just take a break and come back to it later另外,如果您感到压力很大,请休息一下,稍后再回来

add this line inside male onPress在男性onPress中添加这一行

setState((){
    selectedGender = Gender.male;
})

and add this line inside famele onPress并将这一行添加到 famele onPress

setState((){
   selectedGender = Gender.female;
})

It will be resolve your issue.它将解决您的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM