skip to Main Content
  CSCPicker(
                        showCities: false,
                        onCountryChanged: (country) {
                          List<String> parts = country.split(" ");
                          String code = parts.first;
                          String name = parts.last;
                          stateAction.updateCountry(name, code);
                        },
                        onStateChanged: (stateName) {
                          if (stateName != null) {
                            stateAction.updateCity(stateName);
                          }
                        },
                        onCityChanged: (value) {},
                      ),

The above code is perfectly working for new entry but when I am going to edit the existing record, the state is disabled. Hence I have to change the country first. So is there any way to trigger onCountryChanged of CSCPicker widget from provider class at initial state ?

2

Answers


  1. Try setting defaultCountry property of CSCPicker with the selected country – this way you should have it preselected on change.

    Login or Signup to reply.
  2. This should do the trick

    import 'package:csc_picker/csc_picker.dart';
    import 'package:flutter/material.dart';
    
    class MyWidget extends StatefulWidget {
      const MyWidget({Key? key}) : super(key: key);
    
      @override
      State<MyWidget> createState() => _MyWidgetState();
    }
    
    class _MyWidgetState extends State<MyWidget> {
      CscCountry? initialCountry;
    
      @override
      void initState() {
        super.initState();
        initialCountry = CscCountry.Aland_Islands;
      }
    
      @override
      Widget build(BuildContext context) {
        return CSCPicker(
          defaultCountry: initialCountry,
          showCities: false,
          onCountryChanged: (country) {
            List<String> parts = country.split(" ");
            String code = parts.first;
            String name = parts.last;
            // your action => stateAction.updateCountry(name, code);
          },
          onStateChanged: (stateName) {
            if (stateName != null) {
              // your action => stateAction.updateCity(stateName);
            }
          },
          onCityChanged: (value) {},
        );
      }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search