flutter - 如何从对象状态传递到对象,然后将其传递到另一个屏幕
问题描述
我需要你的帮助,起初我有两个页面,它们都是有状态的小部件,其中一个是主屏幕,第二页有数据,我想将它传递到主屏幕
主页
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 变量
解决方案
最简单的方法:
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"),
],
),
),
);
}
希望它会有用
推荐阅读
- .net - powershell 脚本给出错误无法处理巧克力安装参数的参数转换
- visual-studio-code - VSCode 终端电力线字体无符号
- jhipster - 如何在 Jhipster 中为 needle-client-react 创建蓝图?
- flutter - 测试横幅广告无法在 Android 11.0 上加载 - Flutter
- flutter - Flutter 访问没有上下文的文本翻译
- html - CSS下拉菜单是水平的而不是垂直的
- twilio - Twilio:从连接的帐户发送短信
- rust - 防止未使用分配的优雅方法?
- node.js - 非法参数:数字、字符串
- ruby-on-rails - Rails Api 多对多如何发送和处理 json