0

I'm trying to use the country code variable that I declared and stat updated into my Text Field but somehow when I try using it in my Simple map Widget it says that the named parameter isn't defined. Can someone explaine me what I'm doing wrong please? Not sur how to fix that problem...

here is the code :

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

  @override
  State<WorldMap> createState() => _WorldMapState();
}

class _WorldMapState extends State<WorldMap> {
  late String _countryName;
  late String _countryCode;
  List<Map<String, String>> countries = [
    {"name": "Afghanistan", "code": "aF"},
    {"name": "land Islands", "code": "aX"},
    {"name": "Albania", "code": "aL"},
    {"name": "Algeria", "code": "dZ"},
    {"name": "American Samoa", "code": "aS"},
];

@override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: Column(
          children: [
            TextField(
              decoration: const InputDecoration(
                border: OutlineInputBorder(),
                hintText: "Enter the name of a country",
              ),
              onSubmitted: (String userInput) {
                setState(() {
                  _countryName = userInput;
                });
                for (var element in countries) {
                  if (element["name"] == _countryName) {
                    setState(() {
                      _countryCode = element["code"]!;
                    });
                    print(_countryCode);
//change color instead of print
                  }
                }
              },
            ),
            const SizedBox(height: 35),
            SimpleMap(
              // String of instructions to draw the map.
              instructions: SMapWorld.instructions,

              // Default color for all countries.
              defaultColor: Colors.grey,

              // Matching class to specify custom colors for each area.
              colors: const SMapWorldColors(
                //_countryCode: Colors.green, // This line don't work why?
                uS: Colors.green,
                bE: Colors.red,
              ).toMap(),
            ),
          ],
        ),
      ),
    );
  }
}
danronmoon
  • 3,814
  • 5
  • 34
  • 56
Coca95
  • 31
  • 5

1 Answers1

0

remove const, because the _countryCode isnot const.

 colors: SMapWorldColors(

For color, try to create a map variable on state class.

  Map colors = SMapWorldColors(
    uS: Colors.green,
    bE: Colors.red,
  ).toMap();

And onSubmitted

onSubmitted: (String userInput) {
  setState(() {
    _countryName = userInput;
  });
  for (var element in countries) {
    if (element["name"] == _countryName) {
      setState(() {
        _countryCode = element["code"]!;
        colors[_countryCode] = Colors.green;
      });
      print(_countryCode);
//change color instead of print
    }
  }
},

And use on SimpleMap like

 colors: colors,
Md. Yeasin Sheikh
  • 54,221
  • 7
  • 29
  • 56