如何在flutter中获取选定的下拉项目值?

时间:2020-04-23 06:40:21

标签: android flutter flutter-layout

伙计们,我是不熟悉Flutter的人,我创建了一个表单,并在其中添加了一个下拉菜单。我要打印所选项目的数据。该应用正在运行,但是正在打印的是“ Degree”的实例

我真正想做的是通过API将这些数据发送到数据库。

这是我的代码。

class Degree {
const Degree(this.name);

final String name;
}

class _RegisterStudentsState extends State<RegisterStudents> {
Degree selectedDegree;
List<String> selectedValues;
List<Degree> degrees = <Degree>[
const Degree('BSc in Software Engineering (PLY)'),
const Degree('BSc in Computer Security (PLY)'),
const Degree('BSc in Computer Networks (PLY)')
];


@override
Widget build(BuildContext context) {
SystemChrome.setSystemUIOverlayStyle(
    SystemUiOverlayStyle(statusBarColor: Colors.transparent));

return Scaffold(
  resizeToAvoidBottomPadding: false,
  body: Stack(
    children: <Widget>[
      Image.asset(
        'assets/background2.jpg',
        fit: BoxFit.fill,
        height: double.infinity,
        width: double.infinity,
      ),
      Container(
        height: double.infinity,
        width: double.infinity,
        decoration: BoxDecoration(
            gradient: LinearGradient(
                begin: Alignment.bottomCenter,
                end: Alignment.topCenter,
                colors: [
              Colors.black.withOpacity(.9),
              Colors.black.withOpacity(.1),
            ])),
      ),
      Padding(
        padding: EdgeInsets.only(bottom: 60),
        child: Column(
          mainAxisAlignment: MainAxisAlignment.start,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: <Widget>[

            Container(
              decoration: BoxDecoration(
                  color: Colors.white,
                  borderRadius: BorderRadius.circular(10)),
              padding:
                  EdgeInsets.only(left: 30, top: 2, bottom: 2, right: 30),
              child: DropdownButton<Degree>(
                hint: new Text(
                  "Select a degree",
                  style: TextStyle(color: Colors.black),
                ),
                value: selectedDegree,
                onChanged: (Degree newValue) {
                  setState(() {
                    selectedDegree = newValue;
                    print(newValue);
                  });
                },
                items: degrees.map((Degree degree) {
                  return new DropdownMenuItem<Degree>(
                    value: degree,
                    child: new Text(
                      degree.name,
                      style: new TextStyle(color: Colors.black),
                    ),
                  );
                }).toList(),
              ),
            ),
          ],
        ),
      )
    ],
  ),
 );
}

如何打印学位名称而不是打印“学位”实例?

1 个答案:

答案 0 :(得分:1)

尝试>>打印(newValue.name)>> 您要打印整个类,则应打印该类的属性。