我想將一個物件串列傳遞給 CupertinoPicker,但它只接收一個字串串列,我該如何傳遞它?
如何接收此資料物件并正確處理將每個子物件轉換為物件字串,然后我需要將每個物件名稱添加到字串串列中
import 'package:flutter/cupertino.dart';
class MainCupertinoPicker extends StatefulWidget{
@override
State<StatefulWidget> createState() {
return _StateCupertinoPicker();
}
}
class _StateCupertinoPicker extends State<MainCupertinoPicker>{
int _selectedFruit = 0;
// This shows a CupertinoModalPopup with a reasonable fixed height which hosts CupertinoPicker.
void _showDialog(Widget child, {required Text}) {
showCupertinoModalPopup<void>(
context: context,
builder: (BuildContext context) => Container(
height: 216,
padding: const EdgeInsets.only(top: 6.0),
// The Bottom margin is provided to align the popup above the system navigation bar.
margin: EdgeInsets.only(
bottom: MediaQuery.of(context).viewInsets.bottom,
),
// Provide a background color for the popup.
color: CupertinoColors.systemBackground.resolveFrom(context),
// Use a SafeArea widget to avoid system overlaps.
child: SafeArea(
top: false,
child: child,
),
)
);
}
double _kItemExtent = 32.0;
List<String> _fruitNames = <String>[
'Crato',
'Juazeiro',
'Fortaleza'
];
@override
Widget build(BuildContext context) {
return Center(
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
const Text(
'Selecione o municipio: ',
style: TextStyle(
fontSize: 16,
fontWeight: FontWeight.bold
),
),
CupertinoButton(
padding: EdgeInsets.zero,
// Display a CupertinoPicker with list of fruits.
onPressed: () => _showDialog(
CupertinoPicker(
magnification: 1.22,
squeeze: 1.2,
useMagnifier: true,
itemExtent: _kItemExtent,
// This is called when selected item is changed.
onSelectedItemChanged: (int selectedItem) {
setState(() {
_selectedFruit = selectedItem;
print(_selectedFruit);
});
},
children:
List<Widget>.generate(_fruitNames.length, (int index) {
return Center(
child: Text(
_fruitNames[index],
),
);
}),
), Text: null,
),
// This displays the selected fruit name.
child: Text(
_fruitNames[_selectedFruit],
style: const TextStyle(
fontSize: 22.0,
),
),
),
],
),
);
}
}
我需要將物件字串名稱添加到 _fruitNames 串列中,然后向用戶顯示串列,當用戶選擇名稱時,我需要回傳完整的物件
這是我想收到的物件
class Tenant {
int id;
String name;
String siafi;
String url;
String username;
String password;
String driverClassName;
bool initialize;
Tenant({required this.id, required this.name, required this.siafi, required this.url, required this.driverClassName, required this.username, required this.initialize, required this.password});
factory Tenant.fromJson(Map<String, dynamic> json){
return Tenant(
id: json["id"],
name: json["name"],
siafi: json["siafi"],
url: json["url"],
driverClassName: json["driverClassName"],
username: json["username"],
initialize: json["initialize"],
password: json["password"]
);
}
}```
uj5u.com熱心網友回復:
您不需要創建另一個串列只是為了在CupertinoPicker中顯示該物件的名稱,您可以使用相同的串列并在_selectedfruit
您可以借助以下代碼實作預期的行為:-
class MainCupertinoPicker extends StatefulWidget {
@override
State<StatefulWidget> createState() {
return _StateCupertinoPicker();
}
}
class _StateCupertinoPicker extends State<MainCupertinoPicker> {
int _selectedFruit = 0;
// This shows a CupertinoModalPopup with a reasonable fixed height which hosts CupertinoPicker.
void _showDialog(Widget child, {required Text}) {
showCupertinoModalPopup<void>(
context: context,
builder: (BuildContext context) => Container(
height: 216,
padding: const EdgeInsets.only(top: 6.0),
// The Bottom margin is provided to align the popup above the system navigation bar.
margin: EdgeInsets.only(
bottom: MediaQuery.of(context).viewInsets.bottom,
),
// Provide a background color for the popup.
color: CupertinoColors.systemBackground.resolveFrom(context),
// Use a SafeArea widget to avoid system overlaps.
child: SafeArea(
top: false,
child: child,
),
),
);
}
final double _kItemExtent = 32.0;
/// this is the the list of your your tenant object which may be static or may be coming from api as per your usecase
List<Tenant> _tenants = [
Tenant(
id: 1,
name: "Name 1",
siafi: "Xyz",
url: "Xyz",
driverClassName: "Xyz",
username: "Xyz",
initialize: true,
password: "Xyz",
),
Tenant(
id: 1,
name: "Name 2",
siafi: "Xyz",
url: "Xyz",
driverClassName: "Xyz",
username: "Xyz",
initialize: true,
password: "Xyz",
),
Tenant(
id: 1,
name: "Name 3",
siafi: "Xyz",
url: "Xyz",
driverClassName: "Xyz",
username: "Xyz",
initialize: true,
password: "Xyz",
),
Tenant(
id: 1,
name: "Name 4",
siafi: "Xyz",
url: "Xyz",
driverClassName: "Xyz",
username: "Xyz",
initialize: true,
password: "Xyz",
),
Tenant(
id: 1,
name: "Name 5",
siafi: "Xyz",
url: "Xyz",
driverClassName: "Xyz",
username: "Xyz",
initialize: true,
password: "Xyz",
)
];
/// no need to make another list for just showing modal popup
// final List<String> _fruitNames = <String>['Crato', 'Juazeiro', 'Fortaleza'];
@override
Widget build(BuildContext context) {
return Center(
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
const Text(
'Selecione o municipio: ',
style: TextStyle(fontSize: 16, fontWeight: FontWeight.bold),
),
CupertinoButton(
padding: EdgeInsets.zero,
// Display a CupertinoPicker with list of fruits.
onPressed: () => _showDialog(
CupertinoPicker(
magnification: 1.22,
squeeze: 1.2,
useMagnifier: true,
itemExtent: _kItemExtent,
// This is called when selected item is changed.
onSelectedItemChanged: (int selectedItem) {
setState(
() {
_selectedFruit = selectedItem;
if (kDebugMode) {
print(_tenants[_selectedFruit]);
print(_tenants[_selectedFruit].name);
}
},
);
},
children: List<Widget>.generate(
_tenants.length,
(int index) {
return Center(
child: Text(
_tenants[index].name,
),
);
},
),
),
Text: null,
),
// This displays the selected fruit name.
child: Text(
_tenants[_selectedFruit].name,
style: const TextStyle(
fontSize: 22.0,
),
),
),
],
),
);
}
}
class Tenant {
int id;
String name;
String siafi;
String url;
String username;
String password;
String driverClassName;
bool initialize;
Tenant(
{required this.id,
required this.name,
required this.siafi,
required this.url,
required this.driverClassName,
required this.username,
required this.initialize,
required this.password});
factory Tenant.fromJson(Map<String, dynamic> json) {
return Tenant(
id: json["id"],
name: json["name"],
siafi: json["siafi"],
url: json["url"],
driverClassName: json["driverClassName"],
username: json["username"],
initialize: json["initialize"],
password: json["password"]);
}
@override
String toString() {
return "id: $id, name: $name, siafi: $siafi, url: $url, driverClassName: $driverClassName, username: $username, initialize: $initialize, password: $password";
}
}
uj5u.com熱心網友回復:
這是修改后的代碼,
我創建了物件串列,為簡化起見,我創建了自己的物件,稱為Person
List<Person> personList = <Person>[Person(id:1, name:'Alan'), Person(id:2, name:'Ben'), Person(id:3, name:'Cat')];
// whereas
class Person{
String name;
int id;
Person({required this.id, required this.name, });
factory Person.fromJson(Map<String, dynamic> json){
return Person(
id: json["id"],
name: json["name"],
);
}
}
您可以使用 訪問人名personList[index].name。這樣你的代碼就變成了
CupertinoButton(
padding: EdgeInsets.zero,
// Display a CupertinoPicker with list of fruits.
onPressed: () => _showDialog(
CupertinoPicker(
magnification: 1.22,
squeeze: 1.2,
useMagnifier: true,
itemExtent: kItemExtent,
// This is called when selected item is changed.
onSelectedItemChanged: (int selectedItem) {
},
children:
List<Widget>.generate(personList.length, (int index) {
return Center(
child: Text(
personList[index].name,
),
);
}),
),
context,
Text: null,
),
// This displays the selected fruit name.
child: Text(
personList[_selectedFruit].name,
style: const TextStyle(
fontSize: 22.0,
),
),
),
您在串列中有一個完整的物件,您可以根據索引值選擇物件并像這樣回傳整個物件personList[selectedIndex]
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/516010.html
上一篇:為什么iphone不支持max-width:100%?
下一篇:如何在靜音模式下停止聲音
