无法在 Flutter 中显示下拉选择的值
Can't show the dropdown selected value in Flutter
我是 Flutter 开发的新手,我试图显示下拉菜单的 selected 值,但我无法让它工作。
你可以 select 一个 child 并且效果很好,但下拉菜单不会将其显示为已选择的,它就像什么都没有 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();
}
}
非常感谢任何帮助。
检查 DropdownButton class ,有一个名为 value
的 属性,在那个地方使用你的变量 _feedCategory,在你的 DropdownMenuItem
地图上而不是 _feedCategory 使用类别:
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 开发的新手,我试图显示下拉菜单的 selected 值,但我无法让它工作。
你可以 select 一个 child 并且效果很好,但下拉菜单不会将其显示为已选择的,它就像什么都没有 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();
}
}
非常感谢任何帮助。
检查 DropdownButton class ,有一个名为 value
的 属性,在那个地方使用你的变量 _feedCategory,在你的 DropdownMenuItem
地图上而不是 _feedCategory 使用类别:
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})");
});
},
),