gpt4 book ai didi

flutter - 如何从有状态的小部件中获取参数

转载 作者:行者123 更新时间:2023-12-03 03:51:50 25 4
gpt4 key购买 nike

如何从另一个有状态类访问以下有状态类中的_selectedCurrency参数?

import 'dart:io' show Platform;

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

import 'coin_data.dart';

class OsPicker extends StatefulWidget {
@override
_OsPickerState createState() => _OsPickerState();
}

class _OsPickerState extends State<OsPicker> {
String _selectedCurrency = currenciesList[0];

DropdownButton<String> androidDropDown() {
List<DropdownMenuItem<String>> dropdownItems = [];

for (String currency in currenciesList) {
var newItem = DropdownMenuItem(
child: Text(currency),
value: currency,
);

dropdownItems.add(newItem);
}

return DropdownButton<String>(
value: _selectedCurrency,
items: dropdownItems,
onChanged: (value) {
setState(() {
_selectedCurrency = value;
});
},
);
}

CupertinoPicker iOSPicker() {
List<Text> pickerItems = [];

for (String currency in currenciesList) {
var newItem = Text(currency);
pickerItems.add(newItem);
}

return CupertinoPicker(
backgroundColor: Colors.lightBlue,
itemExtent: 32.0,
onSelectedItemChanged: (selectedIndex) {
setState(() {
_selectedCurrency = currenciesList[selectedIndex];
print(_selectedCurrency);
});
},
children: pickerItems);
}

@override
Widget build(BuildContext context) {
return Platform.isIOS ? iOSPicker() : androidDropDown();
}
}
以下是来自coin_dart文件的信息:
const List currencyList = [
'AUD',
“BRL”,
'CAD',
'中国新年',
'欧元',
'英镑',
“港元”,
“IDR”,
“ILS”,
“INR”,
'日元',
“MXN”,
'NOK',
“NZD”,
“PLN”,
'RON',
'擦',
“SEK”,
'SGD',
'美元',
'ZAR'
];
const List cryptoList = [
'BTC',
'ETH',
“LTC”,
];

最佳答案

您不能,Flutter中的数据仅递减。一种解决方法是创建一个全局文件来存储变量或使用诸如BLoC之类的设计模式,该模式允许您从应用程序中的任何位置访问数据。
全局的示例

// global.dart
String selectedCurrency;

// Then you can do as follow to access it
import './lib/global.dart' as Globals;

Globals.selectedCurrency = currenciesList[0];

关于flutter - 如何从有状态的小部件中获取参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62932297/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com