I have this function that returns a new object of a class, but the same code in two apps returns two different results,

Case 1: where it works fine and returns the expected result:

_buildIt() {
return new ShowProduct('name', 12, "AFs", "ImagePath");
}

@override
Widget build(BuildContext context) {
return new Stack(
  fit: StackFit.expand,
  children: [
    new Column(
      children: <Widget>[
        new AnswerButton(true, () => handleAnswer(true)),
        new QuestionText(questionText, questionNumber),
        new AnswerButton(false, () => handleAnswer(false))
      ],
    ),
    overlayShouldBeVisibile == true ? _buildIt() : new Container()
  ],
);
}

Case 2: where it is working but with no result(nothing happens in the UI):

 _buildIt() {
   return new ShowProduct('name', 12, "AFs", "ImagePath");
  }

 _buildGridItem(BuildContext context, document) {
return new Card(
  elevation: 4.0,
  shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(15.0)),
  child: InkWell(
    child: new GridTile(
      footer: Padding(
        padding: const EdgeInsets.all(8.0),
        child: new Column(children: <Widget>[
          new Text(
            document['name'],
          ),
          new Text(
            document['price'].toString(),
          ),
        ]),
      ),
      child: new Icon(Icons.ac_unit,
          size: 100.0), //just for testing, will fill with image later
    ),
    onTap: () {
      _buildIt();
    },
  ),
);
}

the class is a new route (page) where the user will see some content. here is the code for the class:

import 'package:flutter/material.dart';

class ShowProduct extends StatefulWidget {
String _productName = "";
int _productPrice = 0;
String _productUnit = "";
String _productImagePath = "";
ShowProduct(this._productName, this._productPrice, this._productUnit,
  this._productImagePath);
@override
State createState() => new ShowProductState(
  _productName, _productPrice, _productUnit, _productImagePath);

 }

    class ShowProductState extends State<ShowProduct> {
      int _productQuantity = 0;
      int _productTotal = 0;

  int _productPrice = 0;
  String _productName = "";
  String _productUnit = "";
  String _productImagePath = "";

  ShowProductState(this._productName, this._productPrice, this._productUnit,
      this._productImagePath);

  @override
  Widget build(BuildContext context) {
    return Material(
      color: Colors.white54,
      child: Padding(
        padding: const EdgeInsets.only(
            top: 100.0, left: 50.0, right: 50.0, bottom: 100.0),
        child: Stack(
          alignment: Alignment.topCenter,
          children: <Widget>[
            Padding(
              padding: EdgeInsets.only(top: 150 / 2.0),
              child: Container(
                decoration: new BoxDecoration(
                  color: Colors.white,
                  borderRadius: BorderRadius.all(
                    Radius.circular(16.0),
                  ),
                  boxShadow: [
                    new BoxShadow(
                      color: Colors.black54,
                      blurRadius: 8.0,
                    )
                  ],
                ),
                height: 450.0,
                width: 400.0,
                child: new Column(
                  mainAxisAlignment: MainAxisAlignment.start,
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: [
                    Padding(
                      padding: const EdgeInsets.only(top: 85.0),
                      child: new Text(
                        '$_productName',
                        style: TextStyle(
                          color: Colors.black,
                          fontSize: 40.0,
                          fontWeight: FontWeight.w300,
                        ),
                      ),
                    ),
                    new Text(
                      '$_productPrice' + ' $_productUnit',
                      style: TextStyle(color: Colors.redAccent, fontSize: 24.0),
                    ),
                    Padding(
                      padding: const EdgeInsets.only(top: 20.0),
                      child: new Row(
                        mainAxisAlignment: MainAxisAlignment.spaceAround,
                        children: [
                          new OutlineButton(
                            highlightedBorderColor: Colors.red,
                            shape: new RoundedRectangleBorder(
                                borderRadius: new BorderRadius.circular(30.0)),
                            child: new Text(
                              "-",
                              style: TextStyle(
                                  fontSize: 40.0, fontWeight: FontWeight.w300),
                            ),
                            onPressed: () => setState(() {
                                  if (_productQuantity > 0) _productQuantity--;
                                }),
                          ),
                          new Text(
                            '$_productQuantity',
                            style:
                                TextStyle(color: Colors.black, fontSize: 50.0),
                          ),
                          new OutlineButton(
                            highlightedBorderColor: Colors.green,
                            shape: new RoundedRectangleBorder(
                                borderRadius: new BorderRadius.circular(30.0)),
                            child: new Text(
                              "+",
                              style: TextStyle(
                                  fontSize: 40.0, fontWeight: FontWeight.w300),
                            ),
                            onPressed: () => setState(() {
                                  if (_productQuantity < 1000)
                                    _productQuantity++;
                                  _productTotal =
                                      _productQuantity * _productPrice;
                                }),
                          ),
                        ],
                      ),
                    ),
                    Padding(
                      padding: const EdgeInsets.only(top: 20.0),
                      child: new Text(
                        "Total: " + '$_productTotal' + ' $_productUnit',
                        style: TextStyle(color: Colors.black87, fontSize: 24.0),
                      ),
                    ),
                    Padding(
                      padding: const EdgeInsets.only(top: 25.0),
                      child: RaisedButton.icon(
                        shape: new RoundedRectangleBorder(
                            borderRadius:
                                new BorderRadius.all(Radius.circular(30.0))),
                        color: Colors.green,
                        onPressed: () {},
                        icon: Padding(
                          padding: const EdgeInsets.only(
                              top: 8.0, left: 24.0, bottom: 8.0),
                          child: Icon(
                            Icons.add_shopping_cart,
                            color: Colors.white,
                            size: 34.0,
                          ),
                        ),
                        label: Padding(
                          padding: const EdgeInsets.only(
                              top: 8.0, right: 24.0, bottom: 8.0),
                          child: new Text(
                            "ADD TO CART",
                            style:
                                TextStyle(color: Colors.white, fontSize: 20.0),
                          ),
                        ),
                      ),
                    )
                  ],
                ),
              ),
            ),
            Container(
              decoration: new BoxDecoration(boxShadow: [
                new BoxShadow(
                  color: Colors.black54,
                  blurRadius: 8.0,
                ),
              ], borderRadius: BorderRadius.all(Radius.circular(110.0))),
              width: 150.0,
              height: 150.0,
              child: DecoratedBox(
                decoration: ShapeDecoration(
                    shape: CircleBorder(),
                    image: DecorationImage(
                        fit: BoxFit.cover,
                        image: NetworkImage(
                          'https://image.freepik.com/free-vector/fresh-tomato_1053-566.jpg',
                        ))),
              ),
            )
          ],
        ),
      ),
    );
  }
}

I have checked both of the cases with (print()) and it seems that everything is ok but there is no change and navigation to the next screen in the 2nd case.


Solution 1: aubykhan

I see some issue with the 2nd approach. I think you want to add a new widget in the hierarchy inside onTap handler which is wrong. What you can do is to set a flag in onTap handler using setState and add the widget there based on the value of the flag just like you did in the first case which is working.