如何从有状态小部件获取参数
How to do I get a parameter from a stateful widget
如何从另一个有状态 class 访问以下有状态 class 中的 _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 文件:
常量列表货币列表 = [
'AUD',
'BRL',
'CAD',
'CNY',
'EUR',
'GBP',
'HKD',
'IDR',
'ILS',
'INR',
'JPY',
'MXN',
'NOK',
'NZD',
'PLN',
'RON',
'RUB',
'SEK',
'SGD',
'USD',
'ZAR'
];
const 列表 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];
如何从另一个有状态 class 访问以下有状态 class 中的 _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 文件:
常量列表货币列表 = [ 'AUD', 'BRL', 'CAD', 'CNY', 'EUR', 'GBP', 'HKD', 'IDR', 'ILS', 'INR', 'JPY', 'MXN', 'NOK', 'NZD', 'PLN', 'RON', 'RUB', 'SEK', 'SGD', 'USD', 'ZAR' ];
const 列表 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];