如何从下拉菜单中获取价值并在屏幕上显示为文本?

时间:2018-10-16 12:55:57

标签: android dart flutter flutter-layout

如何使“时尚”下拉列表从该下拉列表中获取数据并将其显示为文本。我该如何完成这项任务?这是我编写的flutter应用程序中的代码,该应用程序是一瓶旋转游戏。我在其中进行了下拉,但是如何获取其值并在屏幕上以文本形式打印:

import 'package:flutter/material.dart';
import 'dart:math';

List<DropdownMenuItem<String>> listDrop = [];
loadData() {
  listDrop = [];
  listDrop.add(new DropdownMenuItem(
    child: new Text('Item 1'),
    value: "1",
  ));
  listDrop.add(new DropdownMenuItem(
    child: new Text('Item 2'),
    value: "2",
  ));
  listDrop.add(new DropdownMenuItem(
    child: new Text('Item 3'),
    value: "3",
  ));
}
class ImageRotate extends StatefulWidget {
  @override
  _ImageRotateState createState() => new _ImageRotateState();
}
class _ImageRotateState extends State<ImageRotate>
  with SingleTickerProviderStateMixin {
  AnimationController animationController;

  static var rng = new Random();
  double random_number = 0.0;
  );
}                  }
new Container(
  alignment: Alignment.centerRight,
  padding: new EdgeInsets.only(top: 200.0, right: 100.0),
  child: new DropdownButton(
    style: new TextStyle(
      color: Colors.redAccent,
      fontWeight: FontWeight.bold, 
    ),
    items: listDrop,
    hint: new Text(
      "Select"
    ),
    onChanged: loadData(),
  ),
),

1 个答案:

答案 0 :(得分:0)

您的代码不明确,因此我为您添加了完整的示例。

import 'package:flutter/material.dart';

  void main() => runApp(Home());

  class Home extends StatelessWidget {
    @override
    Widget build(BuildContext context) {
      return MaterialApp(
        home:MultipleDropDownPage()
      );
    }
  }


  class MultipleDropDownPage extends StatefulWidget {
    MultipleDropDownPage({Key key}) : super(key: key);

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

  class _MultipleDropDownPageState extends State<MultipleDropDownPage> {
    String selectedValues;

    @override
    void initState() {
      // TODO: implement initState
      super.initState();
      selectedValues = "1";
    }

    @override
    Widget build(BuildContext context) {
      return new Scaffold(
        appBar: new AppBar(
          title: Text('Multi Drop'),
        ),
        body:  Column(
          children: <Widget>[
            new Text(selectedValues.toString()),
            new DropdownButton<String>(
                  onChanged: (String value) {
                    setState(() {
                      selectedValues = value;
                    });
                  },
                  hint: new Text('Course Unit'),
                  value: selectedValues,
                  items: <String>["1", "2", "3", "4", "5"].map((String value) {
                    return new DropdownMenuItem<String>(
                      value: value,
                      child: new Text(value),
                    );
                  }).toList(),
                ),
          ],
        ),
        floatingActionButton: FloatingActionButton(
          onPressed: () {
            setState(() {
            });
          },
        ),
      );
    }
  }