简体   繁体   English

Flutter DropDown 按钮不显示所选值

[英]Flutter DropDownbutton not showing selected values

The dropdown doesn't show it as the chosen one, it just continues as though nothing was selected.下拉列表不会将其显示为已选择的,它只是继续,好像没有选择任何内容。 Please help me to solve the problem.请帮我解决问题。

I created this custom dropdown widget for multiple usages...我为多种用途创建了这个自定义下拉小部件......

class _AddItemWidgetState extends State<AddItemWidget> {
  static const categoryTypes = [ 
    "SL",
    "KA",
  ];
  static const subCategoryType = [
    "10KG",
    "20KG",
    "5KG",
  ];
  static const Type = [
    "Big Tray",
    "Small Tray",
  ];

  String categorySelectedValue;
  String subCategorySelectedValue;
  String itemType;

  Widget categoryFieldWidget(
      {String name, List<String> nameList, String selectedValue}) {
    return Container(
      height: 49,
      child: FormField<String>(
        builder: (FormFieldState<String> state) {
          return InputDecorator(
            decoration: InputDecoration(
                contentPadding: EdgeInsets.only(left: 10, right: 10),
                border: OutlineInputBorder(
                    borderRadius: BorderRadius.circular(5.0))),
            child: DropdownButtonHideUnderline(
              child: DropdownButton<String>(
                icon: Icon(Icons.keyboard_arrow_down),
                hint: Text(
                  name,
                ),
                onChanged: (String newValue) {
                  setState(() {
                    selectedValue = newValue;
                  });
                  print(selectedValue);
                },
                value: selectedValue,
                isDense: true,
                items: nameList.map((String value) {
                  return DropdownMenuItem<String>(
                    value: value,
                    child: Text(value),
                  );
                }).toList(),
              ),
            ),
          );
        },
      ),
    );
  }

Usage:-用法:-

But when I use this custom dropdown widget in other widgets, the value is not showing on the Ui.但是当我在其他小部件中使用此自定义下拉小部件时,该值未显示在 Ui 上。 "categorySelectedValue"'s value changes...but it's not showing on the Ui... “categorySelectedValue”的值发生了变化……但它没有显示在 Ui 上……

Expanded(
   child: categoryFieldWidget(
            name: "Category", 
            nameList: categoryTypes,
            selectedValue: categorySelectedValue)),
                              

Just check out this example看看这个例子

import 'package:flutter/material.dart';

void main() => runApp(new MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      title: 'Flutter Demo',
      theme: new ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: new MyHomePage(title: 'Users'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => new _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  static const categoryTypes = [
    "SL",
    "KA",
  ];
  static const subCategoryType = [
    "10KG",
    "20KG",
    "5KG",
  ];
  static const itemtype = [
    "Big Tray",
    "Small Tray",
  ];

  String categorySelectedValue;
  String subCategorySelectedValue;
  String itemType;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SafeArea(
        child: Column(
          children: <Widget>[
            Container(
              height: 100,
              child: CustomDropDown(
                callback: (value) {
                  print('This is the category callbackValue : $value');
                },
                name: 'Category',
                list: categoryTypes,
                selectedValue: categorySelectedValue,
              ),
            ),
            Container(
              height: 100,
              child: CustomDropDown(
                callback: (value) {
                  print('This is the subcategory callbackValue : $value');
                },
                name: 'SubCategory',
                list: subCategoryType,
                selectedValue: subCategorySelectedValue,
              ),
            ),
            Container(
              height: 100,
              child: CustomDropDown(
                callback: (value) {
                  print('This is the type callbackValue : $value');
                },
                name: 'Type',
                list: itemtype,
                selectedValue: itemType,
              ),
            ),
          ],
        ),
      ),
    );
  }
}

typedef void StringCallback(String val);

class CustomDropDown extends StatefulWidget {
  final StringCallback callback;
  final List<String> list;
  final String name;
  final String selectedValue;

  const CustomDropDown(
      {Key key, this.list, this.name, this.selectedValue, this.callback})
      : super(key: key);

  @override
  _CustomDropDownState createState() => _CustomDropDownState();
}

class _CustomDropDownState extends State<CustomDropDown> {
  List<String> currentList = List();
  String name;
  String currentSelectedValue;

  @override
  void initState() {
    super.initState();
    currentList = widget.list;
    name = widget.name;
    currentSelectedValue = widget.selectedValue;
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      height: 49,
      child: FormField<String>(
        builder: (FormFieldState<String> state) {
          return InputDecorator(
            decoration: InputDecoration(
                contentPadding: EdgeInsets.only(left: 10, right: 10),
                border: OutlineInputBorder(
                    borderRadius: BorderRadius.circular(5.0))),
            child: DropdownButtonHideUnderline(
              child: DropdownButton<String>(
                icon: Icon(Icons.keyboard_arrow_down),
                hint: Text(
                  widget.name,
                ),
                onChanged: (String newValue) {
                  print('This is the value on select $newValue');
                  setState(() {
                    currentSelectedValue = newValue;
                  });
                  widget.callback(currentSelectedValue);
                },
                value: currentSelectedValue,
                isDense: true,
                items: currentList.map((String value) {
                  return DropdownMenuItem<String>(
                    value: value,
                    child: Text(value),
                  );
                }).toList(),
              ),
            ),
          );
        },
      ),
    );
  }
}


Let me know if it works.让我知道它是否有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM