选择DropdownButtonFormField时的内容更改

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

Content change on selecting DropdownButtonFormField

问题

您可以使用一个Map来跟踪每个选项的状态,以确定哪些TextFormField应该显示。在onChanged回调中更新该Map,然后使用Visibility小部件根据选项的状态来显示或隐藏相应的TextFormField。以下是一个示例代码片段:

Map<String, bool> itemVisibility = {
  'A': false,
  'B': false,
  'C': false,
  'D': false,
};

// ...

DropdownButtonFormField(
  decoration: InputDecoration(
    labelText: 'Calculate',
  ),
  value: selectedValue,
  onChanged: (String? newValue) {
    setState(() {
      selectedValue = newValue!;
    });
    // 更新TextFormField的可见性
    itemVisibility.keys.forEach((key) {
      itemVisibility[key] = key == newValue;
    });
  },
  items: dropdownItems,
),

// ...

inputForm(
  title: locale.translate('AreaGeo')!,
  controller: _controller1,
  suffix: locale.translate('AreaSuffix')!,
  isVisible: itemVisibility['A'] ?? false,
),

inputForm(
  title: locale.translate('RadiusGeo')!,
  controller: _controller2,
  suffix: locale.translate('RadiusSuffix')!,
  isVisible: itemVisibility['A'] ?? false, // 仅在选择'A'时显示
),

// 重复上述模式以显示其他项目的TextFormField

这样,当用户选择不同的项目时,相应的TextFormField将会显示或隐藏,从而实现您的需求。

英文:

I have a container with DropdownButtonFormField containing 4 items and 4 TextFormField. I want to to show one or more TextFormField per each item inside DropdownButtonFormField. e.g. when user select item (A) only textfield of item (A) is shown for the user to input values or when item(B) is selected then both or all textfields are shown.

How can i acheive this. Below is some part of the code.

    return SingleChildScrollView(
  child: Container(
    child: Column(
        children: [
    Padding(
    padding: const EdgeInsets.symmetric(vertical: 8, horizontal: 15),
    child: Column(
      children: [
    DropdownButtonFormField(
    decoration: InputDecoration(
    labelText: &#39;Calculate&#39;,
      value: selectedValue,
      onChanged: (String? newValue) {
        setState(() {
          selectedValue = newValue!;
        });
      },
      items: dropdownItems,
    ),
    SizedBox(height: 20),
    inputForm(
      title: locale.translate(&#39;AreaGeo&#39;)!,
      controller: _controller1,
      suffix: locale.translate(&#39;AreaSuffix&#39;)!,
    ),
    inputForm(
      title: locale.translate(&#39;RadiusGeo&#39;)!,
      controller: _controller2,
      suffix: locale.translate(&#39;RadiusSuffix&#39;)!,
    ),
    GestureDetector(
      onTap: () {
        loanCalculation();
        Future.delayed(Duration.zero);
        showModalBottomSheet(
          isScrollControlled: true,
          isDismissible: false,
          shape: RoundedRectangleBorder(
            borderRadius: BorderRadiusDirectional.only(
              topEnd: Radius.circular(18),
              topStart: Radius.circular(18),
            ),
          ),
          context: context,
          builder: (context) =&gt; SingleChildScrollView(
            child: Wrap(
              children: [
                Container(
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      SizedBox(height: 20),
                      Center(
                        child: Container(
                          width: 80,
                          height: 5,
                          decoration: BoxDecoration(
                            color: Colors.grey[700],
                            borderRadius:
                            BorderRadius.circular(15),
                          ),
                        ),
                      ),
                      SizedBox(height: 15),
                      Padding(
                        padding: const EdgeInsets.fromLTRB(
                            20, 10, 0, 10),
                        child: Column(
                          children: [
                            result(
                                title: locale
                                    .translate(&#39;AreaGeo&#39;)!,
                                amount: area),
                            result(
                                title: locale
                                    .translate(&#39;RadiusGeo&#39;)!,
                                amount: radiusC),
                            result(
                                title:
                                locale.translate(&#39;DiameterGeo&#39;)!,
                                amount: diameter),
                            result(
                                title:
                                locale.translate(&#39;CircumferenceGeo&#39;)!,
                                amount: circumference),
                          ],
                        ),
                      ),
                      SizedBox(height: 15),
                      GestureDetector(
                        onTap: () {
                          Navigator.of(context).pop();
                        },
                        child: Padding(
                          padding: const EdgeInsets.fromLTRB(
                              30, 10, 30, 30),
                          child: Container(
                            height: 60,
                            width: double.infinity,
                            decoration: BoxDecoration(
                              color: UnitConverterApp.isDarkMode
                                  ? AppColors.accentDarkColor
                                  : AppColors.accentLightColor,
                              borderRadius:
                              BorderRadius.circular(10),
                            ),
                            child: Center(
                                child: Text(
                                  locale.translate(&#39;ReCalculate&#39;)!,
                                  style: style,
                                )),
                          ),
                        ),
                      ),
                    ],
                  ),
                ),
              ],
            ),
          ),
        );
      },
      child: Container(
        height: 60,
        width: double.infinity,
        decoration: BoxDecoration(
          color: UnitConverterApp.isDarkMode
              ? AppColors.accentDarkColor
              : AppColors.accentLightColor,
          borderRadius: BorderRadius.circular(10),
        ),
        child: Center(
            child: Text(
              locale.translate(&#39;Calculate&#39;)!,
              style: style,
            )),
      ),
    ),
    ],
  ),
),
],
),
),
);

What is the recommended way to do this? What should i write inside onchanged:

选择DropdownButtonFormField时的内容更改

答案1

得分: 0

以下是代码的翻译部分:

我已经自己解决了,以下是完整的代码列表,如果将来有人有相同的问题。

这是`DropDownButtonFormField`

DropdownButtonFormField(
  decoration: InputDecoration(
    labelText: 'Calculate',
    labelStyle: TextStyle(
      fontSize: 20,
      color: UnitConverterApp.isDarkMode
          ? AppColors.accentDarkColor
          : AppColors.accentLightColor,
    ),
    border: const OutlineInputBorder(),
    enabledBorder: OutlineInputBorder(
      borderSide: BorderSide(
          color: UnitConverterApp.isDarkMode
              ? Colors.white30
              : Colors.black26),
    ),
    focusedBorder: OutlineInputBorder(
      borderSide: BorderSide(
        color: UnitConverterApp.isDarkMode
            ? AppColors.accentDarkColor
            : AppColors.accentLightColor,
        width: 2,
      ),
    ),
  ),
  value: selectedValue,
  onChanged: (String? newValue) {
    setState(() {
      selectedValue = newValue!;
    });
  },
  items: dropdownItems,
)

项目列表

List<DropdownMenuItem<String>> get dropdownItems {
  List<DropdownMenuItem<String>> menuItems = [
    DropdownMenuItem(child: Text("Area"), value: "1"),
    DropdownMenuItem(child: Text("Radius"), value: "2"),
    DropdownMenuItem(child: Text("Diameter"), value: "3"),
    DropdownMenuItem(child: Text("Circumference"), value: "4"),
  ];
  return menuItems;
}

textWidget函数

textWidget() {
  final AppLocalizations locale = AppLocalizations.of(context)!;

  if (selectedValue == "1") {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        SizedBox(height: 10),
        Text(
          locale.translate('RadiusConvert')!,
          style: TextStyle(fontSize: 18),
        ),
        SizedBox(height: 10),
        inputForm(
          title: locale.translate('RadiusGeo')!,
          controller: _controller2,
          suffix: locale.translate('RadiusSuffix')!,
        ),
        SizedBox(height: 10),
        Text(
          locale.translate('DiameterConvert')!,
          style: TextStyle(fontSize: 18),
        ),
        SizedBox(height: 10),
        inputForm(
          title: locale.translate('DiameterGeo')!,
          controller: _controller3,
          suffix: locale.translate('DiameterSuffix')!,
        ),
        SizedBox(height: 10),
        Text(
          locale.translate('CircumferenceConvert')!,
          style: TextStyle(fontSize: 18),
        ),
        SizedBox(height: 10),
        inputForm(
          title: locale.translate('CircumferenceGeo')!,
          controller: _controller4,
          suffix: locale.translate('CircumferenceSuffix')!,
        ),
      ],
    );
  } else if (selectedValue == "2") {
    // ...
  } else if (selectedValue == "3") {
    // ...
  } else if (selectedValue == "4") {
    // ...
  } else {
    return Text('No Selection');
  }
}

请注意,这只是代码的翻译部分,不包括任何问题的回答。

英文:

I have solved on my own, here is the full list of code if anyone has the same question in future.

Here is the DropDownButtonFormField

                  DropdownButtonFormField(
decoration: InputDecoration(
labelText: &#39;Calculate&#39;,
labelStyle: TextStyle(
fontSize: 20,
color: UnitConverterApp.isDarkMode
? AppColors.accentDarkColor
: AppColors.accentLightColor,
),
border: const OutlineInputBorder(),
enabledBorder: OutlineInputBorder(
borderSide: BorderSide(
color: UnitConverterApp.isDarkMode
? Colors.white30
: Colors.black26),
),
focusedBorder: OutlineInputBorder(
borderSide: BorderSide(
color: UnitConverterApp.isDarkMode
? AppColors.accentDarkColor
: AppColors.accentLightColor,
width: 2,
),
),
),
value: selectedValue,
onChanged: (String? newValue) {
setState(() {
selectedValue = newValue!;
});
},
items: dropdownItems,
),

Items List

  List&lt;DropdownMenuItem&lt;String&gt;&gt; get dropdownItems {
List&lt;DropdownMenuItem&lt;String&gt;&gt; menuItems = [
DropdownMenuItem(child: Text(&quot;Area&quot;), value: &quot;1&quot;),
DropdownMenuItem(child: Text(&quot;Radius&quot;), value: &quot;2&quot;),
DropdownMenuItem(child: Text(&quot;Diameter&quot;), value: &quot;3&quot;),
DropdownMenuItem(child: Text(&quot;Circumference&quot;), value: &quot;4&quot;),
];
return menuItems;}

textWidget function

textWidget() {
final AppLocalizations locale = AppLocalizations.of(context)!;
if (selectedValue == &quot;1&quot;) {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
SizedBox(height: 10),
Text(
locale.translate(&#39;RadiusConvert&#39;)!,
style: TextStyle(fontSize: 18),
),
SizedBox(height: 10),
inputForm(
title: locale.translate(&#39;RadiusGeo&#39;)!,
controller: _controller2,
suffix: locale.translate(&#39;RadiusSuffix&#39;)!,
),
SizedBox(height: 10),
Text(
locale.translate(&#39;DiameterConvert&#39;)!,
style: TextStyle(fontSize: 18),
),
SizedBox(height: 10),
inputForm(
title: locale.translate(&#39;DiameterGeo&#39;)!,
controller: _controller3,
suffix: locale.translate(&#39;DiameterSuffix&#39;)!,
),
SizedBox(height: 10),
Text(
locale.translate(&#39;CircumferenceConvert&#39;)!,
style: TextStyle(fontSize: 18),
),
SizedBox(height: 10),
inputForm(
title: locale.translate(&#39;CircumferenceGeo&#39;)!,
controller: _controller4,
suffix: locale.translate(&#39;CircumferenceSuffix&#39;)!,
),
],
);
} else if (selectedValue == &quot;2&quot;) {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
SizedBox(height: 10),
Text(
locale.translate(&#39;AreaConvert&#39;)!,
style: TextStyle(fontSize: 18),
),
SizedBox(height: 10),
inputForm(
title: locale.translate(&#39;AreaGeo&#39;)!,
controller: _controller1,
suffix: locale.translate(&#39;AreaSuffix&#39;)!,
),
SizedBox(height: 10),
Text(
locale.translate(&#39;DiameterConvert&#39;)!,
style: TextStyle(fontSize: 18),
),
SizedBox(height: 10),
inputForm(
title: locale.translate(&#39;DiameterGeo&#39;)!,
controller: _controller3,
suffix: locale.translate(&#39;DiameterSuffix&#39;)!,
),
SizedBox(height: 10),
Text(
locale.translate(&#39;CircumferenceConvert&#39;)!,
style: TextStyle(fontSize: 18),
),
SizedBox(height: 10),
inputForm(
title: locale.translate(&#39;CircumferenceGeo&#39;)!,
controller: _controller4,
suffix: locale.translate(&#39;CircumferenceSuffix&#39;)!,
),
],
);
} else if (selectedValue == &quot;3&quot;) {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
SizedBox(height: 10),
Text(
locale.translate(&#39;AreaConvert&#39;)!,
style: TextStyle(fontSize: 18),
),
SizedBox(height: 10),
inputForm(
title: locale.translate(&#39;AreaGeo&#39;)!,
controller: _controller1,
suffix: locale.translate(&#39;AreaSuffix&#39;)!,
),
SizedBox(height: 10),
Text(
locale.translate(&#39;RadiusConvert&#39;)!,
style: TextStyle(fontSize: 18),
),
SizedBox(height: 10),
inputForm(
title: locale.translate(&#39;RadiusGeo&#39;)!,
controller: _controller2,
suffix: locale.translate(&#39;RadiusSuffix&#39;)!,
),
SizedBox(height: 10),
Text(
locale.translate(&#39;CircumferenceConvert&#39;)!,
style: TextStyle(fontSize: 18),
),
SizedBox(height: 10),
inputForm(
title: locale.translate(&#39;CircumferenceGeo&#39;)!,
controller: _controller4,
suffix: locale.translate(&#39;CircumferenceSuffix&#39;)!,
),
],
);
} else if (selectedValue == &quot;4&quot;) {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
SizedBox(height: 10),
Text(
locale.translate(&#39;AreaConvert&#39;)!,
style: TextStyle(fontSize: 18),
),
SizedBox(height: 10),
inputForm(
title: locale.translate(&#39;AreaGeo&#39;)!,
controller: _controller1,
suffix: locale.translate(&#39;AreaSuffix&#39;)!,
),
SizedBox(height: 10),
Text(
locale.translate(&#39;RadiusConvert&#39;)!,
style: TextStyle(fontSize: 18),
),
SizedBox(height: 10),
inputForm(
title: locale.translate(&#39;RadiusGeo&#39;)!,
controller: _controller2,
suffix: locale.translate(&#39;RadiusSuffix&#39;)!,
),
SizedBox(height: 10),
Text(
locale.translate(&#39;DiameterConvert&#39;)!,
style: TextStyle(fontSize: 18),
),
SizedBox(height: 10),
inputForm(
title: locale.translate(&#39;DiameterGeo&#39;)!,
controller: _controller3,
suffix: locale.translate(&#39;DiameterSuffix&#39;)!,
),
],
);
} else {
return Text(&#39;No Selection&#39;);
}}

huangapple
  • 本文由 发表于 2023年6月9日 01:40:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/76434415.html
匿名

发表评论

匿名网友

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

确定