英文:
Map List of cars only for the previous selected country
问题
Hello fellow Stackers,
我想要实现的是,当我选择一个国家,比如匈牙利,然后匈牙利的汽车会显示在第二个DropdownFormField中,例如卡车和汽车3。我找不到解决这个问题的方法,我只能实现每个国家的所有汽车都显示出来。我尝试在第二个Dropdown中对model.countrys
使用forEach,但这是不可能的,因为它的返回类型是void。
import 'package:flutter/material.dart';
import '../../domain/model/vignette_model/country_model.dart';
import '../../domain/model/vignette_model/vignette_model.dart';
class HomeScreen extends StatefulWidget {
const HomeScreen({
super.key,
});
@override
State<HomeScreen> createState() => _HomeScreenState();
}
class _HomeScreenState extends State<HomeScreen> {
String? selectedValue;
String? selectedVehic;
VignetteModel model = VignetteModel(
countrys: [
const CountryModel(
country: 'Austria',
cars: [
'Truck',
'Car1',
],
),
const CountryModel(
country: 'Germany',
cars: [
'Truck',
'Car2',
],
),
const CountryModel(
country: 'Hungary',
cars: [
'Truck',
'Car3',
],
),
],
);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Home'),
),
body: SingleChildScrollView(
padding: const EdgeInsets.all(25),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
const SizedBox(height: 30),
DropdownButtonFormField(
items: model.countrys?.map((e) {
return DropdownMenuItem(
value: e.country,
child: Text(e.country.toString()),
);
}).toList(),
onChanged: (value) {
setState(() {
selectedValue = value;
});
},
value: selectedValue,
),
if (selectedValue != null)
DropdownButtonFormField(
items: model.countrys?.map((e) {
return DropdownMenuItem(
value: e.cars,
child: Text('${e.cars}'),
);
}).toList(),
onChanged: (value) {
setState(() {
debugPrint(value.toString());
// selectedVehic = value.toString();
});
},
value: selectedVehic,
)
else
const Text('empty'),
],
),
),
);
}
}
请注意,这只是代码的翻译部分,不包括问题的答案。
英文:
Hello fellow Stackers,
I'd like to achieve that when I select a Country, let's say Hungary that then the car for Hungary are is displayed, in that case Truck and Car3, in the 2nd DropdownFormField. I can't find a Solution for that, I only can achieve that all cars from each country is displayed. I tried to use a forEach on the model.countrys
at the 2nd Dropdown, but this is not possible because it does have a return type of void.
import 'package:flutter/material.dart';
import '../../domain/model/vignette_model/country_model.dart';
import '../../domain/model/vignette_model/vignette_model.dart';
class HomeScreen extends StatefulWidget {
const HomeScreen({
super.key,
});
@override
State<HomeScreen> createState() => _HomeScreenState();
}
class _HomeScreenState extends State<HomeScreen> {
String? selectedValue;
String? selectedVehic;
VignetteModel model = VignetteModel(
countrys: [
const CountryModel(
country: 'Austria',
cars: [
'Truck',
'Car1',
],
),
const CountryModel(
country: 'Germany',
cars: [
'Truck',
'Car2',
],
),
const CountryModel(
country: 'Hungary',
cars: [
'Truck',
'Car3',
],
),
],
);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Home'),
),
body: SingleChildScrollView(
padding: const EdgeInsets.all(25),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
const SizedBox(height: 30),
DropdownButtonFormField(
items: model.countrys?.map((e) {
return DropdownMenuItem(
value: e.country,
child: Text(e.country.toString()),
);
}).toList(),
onChanged: (value) {
setState(() {
selectedValue = value;
});
},
value: selectedValue,
),
if (selectedValue != null)
DropdownButtonFormField(
items: model.countrys?.map((e) {
return DropdownMenuItem(
value: e.cars,
child: Text('${e.cars}'),
);
}).toList(),
onChanged: (value) {
setState(() {
debugPrint(value.toString());
// selectedVehic = value.toString();
});
},
value: selectedVehic,
)
else
const Text('empty'),
],
),
),
);
}
}
答案1
得分: 1
以下是您的代码的中文翻译部分:
我为您提供了一个解决方案:
class HomeScreen extends StatefulWidget {
const HomeScreen({
super.key,
});
@override
State<HomeScreen> createState() => _HomeScreenState();
}
class _HomeScreenState extends State<HomeScreen> {
CountryModel? selectedCountryModel;
String? selectedVehicle;
VignetteModel model = VignetteModel(
countries: [
CountryModel(
country: '奥地利',
cars: [
'卡车',
'车型1',
],
),
CountryModel(
country: '德国',
cars: [
'卡车',
'车型2',
],
),
CountryModel(
country: '匈牙利',
cars: [
'卡车',
'车型3',
],
),
],
);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('首页'),
),
body: SingleChildScrollView(
padding: const EdgeInsets.all(25),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
const SizedBox(height: 30),
DropdownButtonFormField<CountryModel>(
items: model.countries.map((e) {
return DropdownMenuItem<CountryModel>(
value: e,
child: Text(e.country.toString()),
);
}).toList(),
onChanged: (value) {
setState(() {
selectedCountryModel = value;
});
},
value: selectedCountryModel,
),
if (selectedCountryModel != null)
DropdownButtonFormField<String>(
items: selectedCountryModel?.cars.map((car) {
return DropdownMenuItem<String>(
value: car,
child: Text(car),
);
}).toList(),
onChanged: (value) {
setState(() {
debugPrint(value.toString());
selectedVehicle = value;
});
},
)
else
const Text('空白'),
],
),
),
);
}
}
希望对您有所帮助! :)
英文:
I have got a solution for you:
class HomeScreen extends StatefulWidget {
const HomeScreen({
super.key,
});
@override
State<HomeScreen> createState() => _HomeScreenState();
}
class _HomeScreenState extends State<HomeScreen> {
CountryModel? selectedCountryModel;
String? selectedVehicle;
VignetteModel model = VignetteModel(
countries: [
CountryModel(
country: 'Austria',
cars: [
'Truck',
'Car1',
],
),
CountryModel(
country: 'Germany',
cars: [
'Truck',
'Car2',
],
),
CountryModel(
country: 'Hungary',
cars: [
'Truck',
'Car3',
],
),
],
);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Home'),
),
body: SingleChildScrollView(
padding: const EdgeInsets.all(25),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
const SizedBox(height: 30),
DropdownButtonFormField<CountryModel>(
items: model.countries.map((e) {
return DropdownMenuItem<CountryModel>(
value: e,
child: Text(e.country.toString()),
);
}).toList(),
onChanged: (value) {
setState(() {
selectedCountryModel = value;
});
},
value: selectedCountryModel,
),
if (selectedCountryModel != null)
DropdownButtonFormField<String>(
items: selectedCountryModel?.cars.map((car) {
return DropdownMenuItem<String>(
value: car,
child: Text(car),
);
}).toList(),
onChanged: (value) {
setState(() {
debugPrint(value.toString());
selectedVehicle = value;
});
},
)
else
const Text('empty'),
],
),
),
);
}
}
Hope it helps!
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论