Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

setState() or markNeedBuild() called during build. #19

Open
devayush20 opened this issue Jul 7, 2022 · 1 comment
Open

setState() or markNeedBuild() called during build. #19

devayush20 opened this issue Jul 7, 2022 · 1 comment

Comments

@devayush20
Copy link

devayush20 commented Jul 7, 2022

I have implemented the functions as First Order Objects, but getting the error when running the app. I am implementing the Gender Selection buttons-

import 'package:flutter/material.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
import 'icon_content.dart';
import 'package:calcbmi/reusable_cart.dart';

class InputPage extends StatefulWidget {
  const InputPage({Key? key}) : super(key: key);
  @override
  State<InputPage> createState() => _InputPageState();
}

const Color inactiveCardColor = Color(0xFF281B54);
const Color activeCardColor = Color(0xFF7D49C4);

enum Gender {
  male,
  female,
}

class _InputPageState extends State<InputPage> {
  late Gender selectedGender = Gender.male;   **//Gives error if not assigned a value even when late is used.**
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('CALC BMI'),
        centerTitle: true,
      ),
      body: Column(
        crossAxisAlignment: CrossAxisAlignment.stretch,
        children: <Widget>[
          /*
          Widget for gender
           */
          Expanded(
            child: Row(
              children: <Widget>[
                Expanded(
                  child: ReusableCard(
                    cardColor: selectedGender == Gender.male
                        ? activeCardColor
                        : inactiveCardColor,
                    cardChild: IconContent(FontAwesomeIcons.mars, "MALE"),
                    onPress: () {
                      setState(() {
                        selectedGender = Gender.male;
                      });
                    },
                  ),
                ),
                Expanded(
                  //Female card
                  child: ReusableCard(
                    cardColor: selectedGender == Gender.female
                        ? activeCardColor
                        : inactiveCardColor,
                    cardChild: IconContent(FontAwesomeIcons.venus, "FEMALE"),
                    onPress: () {
                      setState(() {
                        selectedGender = Gender.female;
                      });
                    },
                  ),
                ),
              ],
            ),
          ),
          /*
          Widget for height
           */
          Expanded(
            child: ReusableCard(
              activeCardColor,
            ),
          ),
          Expanded(
            child: Row(
              children: <Widget>[
                Expanded(
                  child: ReusableCard(activeCardColor),
                ),
                Expanded(
                  child: ReusableCard(activeCardColor),
                ),
              ],
            ),
          ),
          Container(
            margin: EdgeInsets.symmetric(vertical: 20, horizontal: 30),
            height: 50,
            width: 60,
            child: ElevatedButton(
              onPressed: () {},
              child: const Text('Calculate BMI'),
            ),
          ),
        ],
      ),
    );
  }
}


![Screenshot_20220707-165211](https://user-images.githubusercontent.com/78680067/177763632-5643ab74-89eb-4aa0-99e4-f97245468084.jpg)
@OfficialEcho95
Copy link

Hello have you resolved this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants