我如何从Flutter中的提供商调用软件包的回调功能?

wmtdaxz3  于 2023-08-07  发布在  Flutter
关注(0)|答案(2)|浏览(90)
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) {},
                      ),

字符串
上面的代码可以很好地处理新条目,但是当我要编辑现有记录时,状态被禁用。因此,我必须首先改变这个国家。那么有没有办法在初始状态下从provider类触发CSCPicker小部件的onCountryChanged

mrphzbgm

mrphzbgm1#

尝试设置CSCPickerdefaultCountry属性与所选国家-这样你应该有预先选择的更改。

ngynwnxp

ngynwnxp2#

这个应该可以了

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) {},
    );
  }
}

字符串

相关问题