无法在Flutter中显示下拉选择的值

无法在Flutter中显示下拉选择的值

本文介绍了无法在Flutter中显示下拉选择的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Flutter开发的新手,我试图显示下拉菜单的选定值,但无法正常使用。

I am new in Flutter development and I'm trying to show the selected value of a dropdown but I can't get it to work.

您可以选择一个孩子,并且效果很好,但是下拉菜单中没有显示该孩子,就好像没有选择任何东西一样。

You can select one child and it works well but the dropdown doesn't show it as the chosen one, it just keeps like if nothing was selected.

这是我的代码:

import 'package:app2date/repository/repository.dart';
import 'package:app2date/model/FeedSource.dart';
import 'package:app2date/model/FeedCategory.dart';
import 'package:app2date/util/ui.dart';
import 'package:flutter/material.dart';

class ManageFeedSource extends StatefulWidget {
  ManageFeedSource({Key key, this.feedSource}) : super(key: key);

  final FeedSource feedSource;

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

class _ManageFeedSource extends State<ManageFeedSource> {
  final tfNameController = new TextEditingController();
  final tfUrlController = new TextEditingController();
  var editMode = false;
  FeedCategory _feedCategory;

  @override
  Widget build(BuildContext context) {
    FeedSource feedSource = widget.feedSource;
    if (feedSource != null) {
      tfNameController.text = feedSource.name;
      tfUrlController.text = feedSource.url;
      editMode = true;
    }
    return Scaffold(
      appBar: AppBar(
        title: Text('New Feed'),
      ),
      body: new FutureBuilder(
        future: Repository.get().getFeedCategories(),
        builder: (BuildContext context, AsyncSnapshot snapshot) {
          List<FeedCategory> categoriesList = snapshot.data;
          if (categoriesList != null) {
            if (categoriesList.isNotEmpty) {
                _feedCategory = categoriesList[0];      // The dropdown doesn’t draw the element (the list has elements)
            }
            print("${_feedCategory}");
            return new Padding(
              padding: EdgeInsets.all(12.0),
              child: new Column(
                mainAxisAlignment: MainAxisAlignment.start,
                children: <Widget>[
                  new DropdownButton<FeedCategory>(
                    hint: Text("Select Category"),
                    items: categoriesList.map((FeedCategory category) {
                      return new DropdownMenuItem<FeedCategory>(
                        value: _feedCategory,
                        child: Text(_feedCategory.name),
                      );
                    }).toList(),
                    onChanged: (FeedCategory category) {
                      setState(() {
                        _feedCategory = category;      // Problem here too, the element doesn’t show in the dropdown as selected
                        print("Selected: ${_feedCategory.name} (${_feedCategory.id})");
                      });
                    },
                  ),
                ],
              ),
            ),
          ],
          ),
          );
          } else {
          return Container(
          decoration: new BoxDecoration(color: Colors.white),
          );
          }
        },
      ),
    );
  }

  @override
  void initState() {
    super.initState();
  }
}

我将非常感谢您的帮助。

I would really appreciate any help.

推荐答案

检查类,有一个名为 value 的属性,在该位置使用变量_feedCategory,并在 DropdownMenuItem 映射而不是_feedCategory使用类别:

Check the DropdownButton class , there is a property named value, use your variable _feedCategory in that place, and on your DropdownMenuItem map instead of _feedCategory use category:

   new DropdownButton<FeedCategory>(
                      value: _feedCategory,
                      hint: Text("Select Category"),
                      items: categoriesList.map((FeedCategory category) {
                        return new DropdownMenuItem<FeedCategory>(
                          value: category,
                          child: Text(category.name),
                        );
                      }).toList(),
                      onChanged: (FeedCategory category) {
                        setState(() {
                          _feedCategory = category;      // Problem here too, the element doesn’t show in the dropdown as selected
                          print("Selected: ${_feedCategory.name} (${_feedCategory.id})");
                        });
                      },
                    ),

这篇关于无法在Flutter中显示下拉选择的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 03:06