如何将数据传递到另一个小部件内部的小部件

时间:2019-09-27 15:30:14

标签: flutter dart

我能够将数据<div class="input-res"> <input readonly id="original" type="text" value="Original Package Price" /> <input readonly id="discount" type="text" value="Discount" /> <input readonly id="totals" type="text" value="Total Package Price" /> </div> <h4 id="selectbelow"> Please Select Items From The Table Below </h4> <div class="tab"> <button class="tablinks" onclick="openCity(event, 'Sofas')">Sofas</button> <button class="tablinks" onclick="openCity(event, 'Armchairs')">Armchairs</button> <button class="tablinks" onclick="openCity(event, 'Mattresses')">Mattresses</button> </div> <div id="Sofas" class="tabcontent"> <table> <tbody> <tr> <td><input class="checkproduct" name="product1" type="checkbox" value="1000" /> Sofa 1 €1000</td> </tr> <tr> <td><input class="checkproduct" name="product1" type="checkbox" value="100" /> Sofa 2 €100</td> </tr> </tbody> </table> </div> <div id="Armchairs" class="tabcontent"> <table style="display:block"> <tbody> <tr> <td><input class="checkproduct" name="product1" type="checkbox" value="400" /> Armchair 1 €400</td> </tr> <tr> <td><input class="checkproduct" name="product1" type="checkbox" value="200" /> Armchair 2 €200</td> </tr> </tbody> </table> </div> <div id="Mattresses" class="tabcontent"> <table style="display:block"> <tbody> <tr> <td><input class="checkproduct" name="product1" type="checkbox" value="400" /> Mattress 1 €400</td> </tr> <tr> <td><input class="checkproduct" name="product1" type="checkbox" value="200" /> Mattress 2 €200</td> </tr> </tbody> </table> </div> <script> function openCity(evt, cityName) { var i, tabcontent, tablinks; tabcontent = document.getElementsByClassName("tabcontent"); for (i = 0; i < tabcontent.length; i++) { tabcontent[i].style.display = "none"; } tablinks = document.getElementsByClassName("tablinks"); for (i = 0; i < tablinks.length; i++) { tablinks[i].className = tablinks[i].className.replace(" active", ""); } document.getElementById(cityName).style.display = "block"; evt.currentTarget.className += " active"; } </script> <script language="javascript" src="http://code.jquery.com/jquery-1.4.2.min.js"></script> <script language="javascript"> $(document).ready(function() { var amount = 0; var totalBefore = 0 ; var totalAmount = 0; var discount = 0; $('.checkproduct').click(function(){ amount=0; totalAmount =0; discount = 0; totalBefore =0; $('.checkproduct').each(function() { debugger; if($(this).attr('checked')){ amount=parseFloat($(this).val()) totalBefore +=amount; if(totalBefore>=1800){ totalAmount = totalBefore -(totalBefore*0.1); discount = totalBefore*0.1; } else if(totalBefore>=2000){ totalAmount = totalBefore -(totalBefore*0.15); discount = discount*0.15; } else{ totalAmount = totalBefore ; } } }); $('#original').val("Original Package Price €" +totalBefore ); $('#discount').val("Discount €" +discount ); $('#totals') .val("Total Package Price €" +totalAmount ); }); }); if (totals >= 1000 && productCost < 1600) { discount = 0.9; } </script> widget.value传递到FirstPageSecondPage中有一个名为thirdWidget的小部件。 如何将SecondPage传递给widget.value

thirdWidget

2 个答案:

答案 0 :(得分:1)

在您的SecondPage

中使用它
Row(
  children: thirdWidget(widget.value),
)

并像这样更新您的thirdWidget

List<Widget> thirdWidget(var data) {
  // data is widget.value
  return [];
}

答案 1 :(得分:0)

只需将该信息传递到状态类中即可。像这样:

class SecondPage extends StatefulWidget {
  final String value;

  ThirdRoute({Key key, this.value})
      : super(key: key);

  @override
  SecodpageState createState() => SecodpageState(value);
}

class SecodpageState extends State< SecondPage > {
  final String value;

  SecodpageState(this.value);

  @override
  void initState() {
    super.initState();
  }


  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Calendar Page"),
      ),
      body: Column(
        children: <Widget>[
          Text("${widget.value}"),
          Row(
            children: thirdWidget(),
          ),
          Center(
            child: RaisedButton(
              onPressed: () {
                Navigator.pop(context);
              },
              child: Text('Go back!'),
            ),
          ),
        ],
      ),
    );
  }
}


List<Widget> thirdWidget() {
   return Text(value);
}