相同的代码,两个不同的结果|颤抖地导航到另一条路线

时间:2018-10-21 17:42:37

标签: flutter

我有此函数可返回类的新对象,但是两个应用程序中的相同代码将返回两个不同的结果,

情况1:可以正常工作并返回预期结果:

_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()
  ],
);
}

案例2:它在哪里工作但没有结果(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();
    },
  ),
);
}

该课程是一个新的路线(页面),用户将在其中看到一些内容。 这是该类的代码:

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',
                        ))),
              ),
            )
          ],
        ),
      ),
    );
  }
}

我已经用(print())检查了这两种情况,似乎一切正常,但是在第二种情况下没有任何变化并可以导航到下一个屏幕。

1 个答案:

答案 0 :(得分:1)

我发现第二种方法存在一些问题。我认为您想在onTap处理程序内部的层次结构中添加一个新的小部件,这是错误的。您可以做的就是使用setState在onTap处理程序中设置一个标志,并根据标志的值在其中添加小部件,就像在第一种情况下一样。