如何在flutter的下拉菜单中添加不同的图像

时间:2018-12-09 08:18:55

标签: flutter dropdown

我试图做一个像这样的下拉菜单:

enter image description here

我在stackoverflow中发现了这个示例,可以在下拉列表中添加图像,但是目前它是一个列表,我不知道如何添加不同的图像

import 'package:flutter/material.dart';

class Test extends StatefulWidget {
  @override
  _TestState createState() => new _TestState();
}

class _TestState extends State<Test> {
  var _img = new 

Image.network(“ https://upload.wikimedia.org/wikipedia/commons/thumb/8/89/TUCPamplona10.svg/500px-TUCPamplona10.svg.png”);

    @override
      Widget build(BuildContext context) {
        return new Scaffold(
      appBar: new AppBar(title: new Text("Test Drop"),),
      body: new Center(
     child: new Container (
        height: 50.0,
        child:new DropdownButton(
        items: new List.generate(10, (int index){
      return new DropdownMenuItem(child: new Container(
        padding: const EdgeInsets.only(bottom: 5.0),
        height: 100.0,
          child: new Row(
            mainAxisAlignment: MainAxisAlignment.spaceEvenly,
          children: <Widget>[
            _img,
            new Text("Under 10")
          ],
        ),
      ));
    }) 
    , onChanged: null),),
   ),
  );
 }
 }

1 个答案:

答案 0 :(得分:0)

我是用JSON完成的。您可以在下拉菜单中调用多个值。

import "package:flutter/material.dart";
import 'dart:async';
import 'dart:convert';
import 'package:http/http.dart' as http;


class TestApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<TestApp> {
  String _mySelection;

  final String url = "http://webmyls.com/php/getdata.php";

  List data = List(); //edited line

  Future<String> getSWData() async {
    var res = await http
        .get(Uri.encodeFull(url), headers: {"Accept": "application/json"});
    var resBody = json.decode(res.body);

    setState(() {
      data = resBody;
    });

    print(resBody);

    return "Sucess";
  }

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

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: AppBar(
        title: Text("Hospital Management"),
      ),
      body: new Center(
        child: new DropdownButton(
          items: data.map((item) {
            return new DropdownMenuItem(
              child: Row(
                children: <Widget>[
                   Text(item['item_name']),
                ],
              ),
              value: item['id'].toString(),
            );
          }).toList(),
          onChanged: (newVal) {
            setState(() {
              _mySelection = newVal;
            });
          },
          value: _mySelection,
        ),
      ),
    );
  }
}

修改此内容

 children: <Widget>[
 Text(item['item_name']),
 Text(item['item_name']),
 Text(item['item_name']),
 ],