下拉按钮不会改变 [英] Dropdown Button wont change

查看:81
本文介绍了下拉按钮不会改变的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在下拉按钮上编写浮动代码时陷入困境,在用户从列表中选择后,提示不会更改为用户选择的内容.有人可以帮忙吗?

Hi i got stuck while write flutter code on dropdown button, where after user choosed from the list the hint wont changed to what the user choose. Can anyone help ?

这是我的代码:

DropdownButton(items: [
            DropdownMenuItem(value: "1", child: Text('+')),
            DropdownMenuItem(value: "2", child: Text('-')),
            DropdownMenuItem(value: "3", child: Text('X')),
            DropdownMenuItem(value: "4", child: Text('/'))
            ].toList(), onChanged: (value){
              setState(() {
                _value = value;
              });
            },hint: Text('Operation'),)

推荐答案

我刚刚在下面创建了一个示例,只需检查一下,让我知道它是否有效:

I have just created an example below just check it and let me know if it works :

import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      title: 'Flutter Demo',
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  String selectedOperator;
  var listOfOperators = [
    Operators(type: "+ Addition", value: 1),
    Operators(type: "- Substraction", value: 2),
    Operators(type: "* Multiplication", value: 3),
    Operators(type: "/ Division", value: 4),
  ];

  @override
  void initState() {
    super.initState();
    print(listOfOperators.length);
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      home: Scaffold(
        body: Center(
                  child: Container(

            child: Padding(
              padding: const EdgeInsets.all(30.0),
              child: Container(
                height: 50,
                decoration: BoxDecoration(
                  borderRadius: BorderRadius.circular(5.0),
                  border: Border.all(
                      color: Colors.red, style: BorderStyle.solid, width: 0.80),
                ),
                child: DropdownButton(
                    value: selectedOperator,
                    isExpanded: true,
                    icon: Padding(
                      padding: const EdgeInsets.only(left: 15.0),
                      child: Icon(Icons.arrow_drop_down),
                    ),
                    iconSize: 25,
                    underline: SizedBox(),
                    onChanged: (newValue) {
                      setState(() {
                        print(newValue);
                        selectedOperator = newValue;
                      });
                      print(selectedOperator);
                    },
                    hint: Padding(
                      padding: const EdgeInsets.all(8.0),
                      child: Text('Select'),
                    ),
                    items: listOfOperators.map((data) {
                      return DropdownMenuItem(
                        value: data.value.toString(),
                        child: Padding(
                          padding: const EdgeInsets.only(left: 10.0),
                          child: Text(
                            data.type,
                            style: TextStyle(
                              fontSize: 18,
                              color: Colors.black,
                            ),
                          ),
                        ),
                      );
                    }).toList()),
              ),
            ),
          ),
        ),
      ),
    );
  }
}

class Operators {
  String type;
  int value;
  Operators({this.type, this.value});
}

这篇关于下拉按钮不会改变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆