gpt4 book ai didi

dart - 如何在一行中使用 DropDownButton?

转载 作者:IT王子 更新时间:2023-10-29 07:08:18 25 4
gpt4 key购买 nike

我想在一行中添加一个下拉列表。

当我这样做的时候

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',
)),
],
)
],
),
),
);
}
}

最佳答案

DropdownButton 的文档说 enter image description here

value: 'Rupees'

不是

value: 'Rupee'

因为

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

你必须添加

onChanged: (str){},

关于dart - 如何在一行中使用 DropDownButton?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55904185/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com