首页 > 解决方案 > 如何从对象状态传递到对象,然后将其传递到另一个屏幕

问题描述

我需要你的帮助,起初我有两个页面,它们都是有状态的小部件,其中一个是主屏幕,第二页有数据,我想将它传递到主屏幕

主页

code

  @override
  _PriceScreenState createState() => _PriceScreenState();
}

class _PriceScreenState extends State<PriceScreen> {

  String selectedCurrency = 'USD';

  String bitcoinValueInUSD;

  int dropNumber;

  void getCurrenciesBitcoin() async {

    try {
      CoinData coinData = CoinData();

      double usdPrice = await coinData.getCurrencies();

      setState(() {
        bitcoinValueInUSD = usdPrice.toStringAsFixed(0);
      });
    } catch (e) {
      print(e);
    }
  }

  @override
  Widget build(BuildContext context) {
    getCurrenciesBitcoin();

    return Scaffold(
      appBar: AppBar(
        title: Text(' Coin Ticker'),
      ),
      body: Column(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        crossAxisAlignment: CrossAxisAlignment.stretch,
        children: <Widget>[
          Padding(
            padding: EdgeInsets.fromLTRB(18.0, 18.0, 18.0, 0),
            child: Card(
              color: Colors.lightBlueAccent,
              elevation: 5.0,
              shape: RoundedRectangleBorder(
                borderRadius: BorderRadius.circular(10.0),
              ),
              child: Padding(
                padding: EdgeInsets.symmetric(vertical: 15.0, horizontal: 28.0),
                child: Text(
                  '1 BTC = $bitcoinValueInUSD $selectedCurrency',
                  textAlign: TextAlign.center,
                  style: TextStyle(
                    fontSize: 20.0,
                    color: Colors.white,
                  ),
                ),
              ),
            ),
          ),
          Container(
            height: 150.0,
            alignment: Alignment.center,
            padding: EdgeInsets.only(bottom: 30.0),
            color: Colors.lightBlue,
            child: GetDropDownMenu(
              selectedCurrency: ,
            ),
          ),
        ],
      ),
    );
  }
}

这是第二页

class GetDropDownMenu extends StatefulWidget {
  
  @override
  _GetDropDownMenuState createState() => _GetDropDownMenuState();
}

class _GetDropDownMenuState extends State<GetDropDownMenu> {
  String selectedCurrency;

  List<DropdownMenuItem<String>> getDropDownItem() {
    List<DropdownMenuItem<String>> dropDownItems = [];

    for (String currency in currenciesList) {
      var newItem = DropdownMenuItem(
        child: Text(currency),
        value: currency,
      );
      dropDownItems.add(newItem);
    }
    return dropDownItems;
  }

  @override
  Widget build(BuildContext context) {
    return DropdownButton<String>(
        value: selectedCurrency,
        items: getDropDownItem(),
        onChanged: (value) {
          setState(() {
            selectedCurrency = value;
          });
        });
  }
}

,我需要将 selectedCurrency 值从第 2 页传递到主页以等于那里的 selectedCurrency 变量

标签: flutterandroid-studiodart

解决方案


最简单的方法:

1. 从第一个屏幕导航到其他屏幕:

    Navigator.pushNamed(context, "second",arguments: selectedCurrency);
    },

2.在build方法的第二个屏幕上获取为:

@override
  Widget build(BuildContext context) {
    var passedValue = ModalRoute.of(context).settings.arguments;
    return Scaffold(
      appBar: AppBar(title: Text("Second")),
      body: Container(
        child: Column(
          children: <Widget>[
            Text("PassedValue : $passedValue"),
          ],
        ),
      ),
    );
}

希望它会有用


推荐阅读