skip to Main Content

I simplified my code so my question would be easier to ask. Basically, I have a value that goes through an if else statement and then text is printed based on that value.

class Number {

  static void compareVal  () {
    final int a = 5;
      if (a == 1) {
        print('Number is 1');
        //String i = 'Number is 1';
      }
      else {
        print('Number is not 1');
        //String i = 'Number is 1';
      }
    }

}

class RequirementTenWidgetState extends State<RequirementTenWidget>{
  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
return Scaffold(
      body: Container(
        width: double.infinity,
        height: double.infinity,
        child: const Text('Number',
          style: TextStyle(
            fontSize: 50,
            color: Colors.white,
          ),
          textAlign: TextAlign.center,
        ),
      ),
      floatingActionButton: FloatingActionButton(
        child: Text('something'),
        backgroundColor: Colors.blue[600],
        onPressed: () => {
          Number.caompareVal()
      },

      ),
    );

`

I wanted to add a button that when pressed displays the text on the screen, currently when the button is pressed, the text displays on the terminal and not in the app. How would I get the app to display the text?

2

Answers


  1. For that you have to (1) take one string value which display your text (2) make your number function return type String. (3) Update the string based on that.

    Here is your updated code :

    
    class Number {
      static String compareVal() {
        final int a = 5;
        if (a == 1) {
          print('Number is 1');
          return "Number is 1";
        } else {
          print('Number is not 1');
          return "Number is Not 1";
        }
      }
    }
    
    class RequirementTenWidgetState extends State<RequirementTenWidget> {
      @override
      void initState() {
        super.initState();
      }
    
      String stringNumber = "";
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          body: Container(
            width: double.infinity,
            height: double.infinity,
            child: Text(
              "Number",
              style: TextStyle(
                fontSize: 50,
                color: Colors.black,
              ),
              textAlign: TextAlign.center,
            ),
          ),
          floatingActionButton: FloatingActionButton(
            child: Text(stringNumber),
            backgroundColor: Colors.blue[600],
            onPressed: () => {
              setState(() {
                stringNumber = Number.compareVal();
              })
            },
          ),
        );
      }
    }
    
    
    Login or Signup to reply.
  2. in-order to show text on the app, use Text() widget

    I am changed the compareVal() return type to String, and kept in the same class for testing, you can use anywhere, just call accordingly

    to update the text in realtime used setState() method

     class _StringDemoState extends State<StringDemo> {
    
          //method declaration : String is there return type
          String compareVal() {
            final int a = 5;
            if (a == 1) {
              return 'Number is 1';
             
            } else {
              return 'Number is not 1';
              
            }
          }
    
      //variable result to store and show response from compareVal()
      String result = '';
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(title: Text('Demo')),
          body: Column(
    
            //Alignments
            mainAxisAlignment: MainAxisAlignment.center,
            crossAxisAlignment: CrossAxisAlignment.stretch,
    
            //used column to show Text() and Button() in Vertical order
            children: [
              Center(
                //result value will be shown in app,
                // initially '' 
                // once button clicked will update the result
                child: Text(result),
              ),
    
              MaterialButton(
                child: Text('check'),
                onPressed: () {
    
                  //setState to update values in realtime 
                  setState(() {
                    
                    //return value of compareVal() assigned to result variable, since it is in setState it will be updated immediately 
                    result = compareVal();
                  });
                },
              )
            ],
          ),
        );
      }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search