如何在行内使用DropDownButton?

时间:2019-04-29 13:14:13

标签: dart flutter

我试图在行内添加一个下拉列表。

当我这样做

Row(
              children: <Widget>[
               Expanded(child:TextField(
                  keyboardType: TextInputType.number,
                  decoration: InputDecoration(
                      labelText: 'Terms',
                      hintText: '%'
                  ),
                )),

               Expanded(child:DropdownButton<String>(
                  items:_currencies.map((String value){
                    return DropdownMenuItem<String>(
                      value: value,
                      child: Text(value),
                    );
                  }).toList(),
               value : 'Rupee',
                )),
              ],
            )

出现错误

erforming hot reload...
Syncing files to device iPhone XR...
flutter: ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
flutter: The following assertion was thrown building SIForm(dirty, state: _SIFormState#da46e):
flutter: 'package:flutter/src/material/dropdown.dart': Failed assertion: line 560 pos 15: 'items == null ||
flutter: items.isEmpty || value == null || items.where((DropdownMenuItem<T> item) => item.value ==
flutter: value).length == 1': is not true.
flutter:
flutter: Either the assertion indicates an error in the framework itself, or we should provide substantially
flutter: more information in this error message to help you determine and fix the underlying cause.
flutter: In either case, please report this assertion by filing a bug on GitHub:
flutter:   https://github.com/flutter/flutter/issues/new?template=BUG.md
flutter:
flutter: When the exception was thrown, this was the stack:
flutter: #2      new DropdownButton (package:flutter/src/material/dropdown.dart:560:15)
flutter: #3      _SIFormState.build (package:simple_cal/main.dart:65:31)
flutter: #4      StatefulElement.build (package:flutter/src/widgets/framework.dart:3825:27)
flutter: #5      ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3736:15)
flutter: #6      Element.rebuild (package:flutter/src/widgets/framework.dart:3559:5)
flutter: #7      BuildOwner.buildScope (package:flutter/src/widgets/framework.dart:2273:33)
flutter: #8      _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding&PaintingBinding&SemanticsBinding&RendererBinding&WidgetsBinding.drawFrame (package:flutter/src/widgets/binding.dart:700:20)
flutter: #9      _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding&PaintingBinding&SemanticsBinding&RendererBinding._handlePersistentFrameCallback (package:flutter/src/rendering/binding.dart:268:5)
flutter: #10     _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding._invokeFrameCallback (package:flutter/src/scheduler/binding.dart:988:15)
flutter: #11     _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding.handleDrawFrame (package:flutter/src/scheduler/binding.dart:928:9)
flutter: #12     _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding.scheduleWarmUpFrame.<anonymous closure> (package:flutter/src/scheduler/binding.dart:749:7)
flutter: #14     _Timer._runTimers (dart:isolate/runtime/libtimer_impl.dart:382:19)
flutter: #15     _Timer._handleMessage (dart:isolate/runtime/libtimer_impl.dart:416:5)
flutter: #16     _RawReceivePortImpl._handleMessage (dart:isolate/runtime/libisolate_patch.dart:171:12)
flutter: (elided 3 frames from class _AssertionError and package dart:async)
flutter: ════════════════════════════════════════════════════════════════════════════════════════════════════
Reloaded 1 of 432 libraries in 736ms.

这是我完整的main.dart文件

import 'package:flutter/material.dart';

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

Widget MyApp() {
  return MaterialApp(
    title: 'Simple Intrest app',
    home: SIForm (),
  );
}

class SIForm extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
      return _SIFormState ();
  }
}

class _SIFormState extends State<SIForm>{
  var _currencies = ["Rupees","USD","CAD"];

  @override
  Widget build(BuildContext context) {

    return Scaffold(

      appBar: AppBar(
        title:Text("Simple Interest Calc"),
      ),

      body: Container(
        child: Column(
          children: <Widget>[
            Padding(
                padding: EdgeInsets.only(top : 5.0,bottom : 5.0),
                child: TextField(
                  keyboardType: TextInputType.number,
                  decoration: InputDecoration(
                    labelText: 'principle',
                    hintText: 'Enter the principle amout'
                  ),
                )),

      Padding(
          padding: EdgeInsets.only(top : 5.00*2,bottom : 5.00*2),
          child :  TextField(
                  keyboardType: TextInputType.number,
                  decoration: InputDecoration(
                      labelText: 'Rate of Interest',
                      hintText: '%'
                  ),
                )),


            Row(
              children: <Widget>[
               Expanded(child:TextField(
                  keyboardType: TextInputType.number,
                  decoration: InputDecoration(
                      labelText: 'Terms',
                      hintText: '%'
                  ),
                )),

               Expanded(child:DropdownButton<String>(
                  items:_currencies.map((String value){
                    return DropdownMenuItem<String>(
                      value: value,
                      child: Text(value),
                    );
                  }).toList(),
               value : 'Rupee',
                )),
              ],
            )
          ],
        ),
      ),
    );
  }
}

2 个答案:

答案 0 :(得分:3)

DropdownButton的文档说 enter image description here

value: 'Rupees'

不是

value: 'Rupee'

因为

var _currencies = ["Rupees","USD","CAD"];

,您必须添加

onChanged: (str){},

答案 1 :(得分:0)

  

从值更改为“卢比”并添加onChanged函数

DropdownButton<String>(
     items:_currencies.map((String value){
       return DropdownMenuItem<String>(
          value: value,
          child: Text(value),
       );
   }).toList(),
 value : 'Rupees',
 onChanged: (val){},),