如何从对象状态传递到对象,然后传递到另一个屏幕



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

主页

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值从第二页到主页等于selectedCurrency变量那里

可以在构造函数中添加回调函数来返回下拉列表的值。

例如

class GetDropDownMenu extends StatefulWidget {
final Function(String) onSubmitted;
const GetDropDownMenu({Key key, this.onSubmitted}) : super(key: key);
@override
_GetDropDownMenuState createState() => _GetDropDownMenuState();
}
class _GetDropDownMenuState extends State<GetDropDownMenu> {
String selectedCurrency;
@override
Widget build(BuildContext context) {
return DropdownButton<String>(
value: selectedCurrency,
items: getDropDownItem(),
onChanged: (value) {
setState(() {
selectedCurrency = value;
});
widget.onSubmitted(value);
});
}
}

如何使用:

GetDropDownMenu(
onSubmitted: (val) {
print("Result value on dropdown: $val");
},
)

最简单的方法:

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

希望对大家有帮助

相关内容

最新更新