地图上只显示先前选择的国家的汽车列表。

huangapple go评论75阅读模式
英文:

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 &#39;package:flutter/material.dart&#39;;

import &#39;../../domain/model/vignette_model/country_model.dart&#39;;
import &#39;../../domain/model/vignette_model/vignette_model.dart&#39;;

class HomeScreen extends StatefulWidget {
  const HomeScreen({
    super.key,
  });

  @override
  State&lt;HomeScreen&gt; createState() =&gt; _HomeScreenState();
}

class _HomeScreenState extends State&lt;HomeScreen&gt; {
  String? selectedValue;
  String? selectedVehic;

  VignetteModel model = VignetteModel(
    countrys: [
      const CountryModel(
        country: &#39;Austria&#39;,
        cars: [
          &#39;Truck&#39;,
          &#39;Car1&#39;,
        ],
      ),
      const CountryModel(
        country: &#39;Germany&#39;,
        cars: [
          &#39;Truck&#39;,
          &#39;Car2&#39;,
        ],
      ),
      const CountryModel(
        country: &#39;Hungary&#39;,
        cars: [
          &#39;Truck&#39;,
          &#39;Car3&#39;,
        ],
      ),
    ],
  );
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text(&#39;Home&#39;),
      ),
      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(&#39;${e.cars}&#39;),
                  );
                }).toList(),
                onChanged: (value) {
                  setState(() {
                    debugPrint(value.toString());
                    // selectedVehic = value.toString();
                  });
                },
                value: selectedVehic,
              )
            else
              const Text(&#39;empty&#39;),
          ],
        ),
      ),
    );
  }
}

答案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&lt;HomeScreen&gt; createState() =&gt; _HomeScreenState();
}
class _HomeScreenState extends State&lt;HomeScreen&gt; {
CountryModel? selectedCountryModel;
String? selectedVehicle;
VignetteModel model = VignetteModel(
countries: [
CountryModel(
country: &#39;Austria&#39;,
cars: [
&#39;Truck&#39;,
&#39;Car1&#39;,
],
),
CountryModel(
country: &#39;Germany&#39;,
cars: [
&#39;Truck&#39;,
&#39;Car2&#39;,
],
),
CountryModel(
country: &#39;Hungary&#39;,
cars: [
&#39;Truck&#39;,
&#39;Car3&#39;,
],
),
],
);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text(&#39;Home&#39;),
),
body: SingleChildScrollView(
padding: const EdgeInsets.all(25),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
const SizedBox(height: 30),
DropdownButtonFormField&lt;CountryModel&gt;(
items: model.countries.map((e) {
return DropdownMenuItem&lt;CountryModel&gt;(
value: e,
child: Text(e.country.toString()),
);
}).toList(),
onChanged: (value) {
setState(() {
selectedCountryModel = value;
});
},
value: selectedCountryModel,
),
if (selectedCountryModel != null)
DropdownButtonFormField&lt;String&gt;(
items: selectedCountryModel?.cars.map((car) {
return DropdownMenuItem&lt;String&gt;(
value: car,
child: Text(car),
);
}).toList(), 
onChanged: (value) {
setState(() {
debugPrint(value.toString());
selectedVehicle = value;
});
},
)
else
const Text(&#39;empty&#39;),
],
),
),
);
}
}

Hope it helps! 地图上只显示先前选择的国家的汽车列表。

huangapple
  • 本文由 发表于 2023年2月7日 01:43:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/75364788.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定