Flutter DropdownButton 显示了一些奇怪的行为:disabledHint
当它被禁用时它显示小部件而不是选定的值(必须通过设置onChanged
为 null 来完成)。
如何显示选定的值?
这是我的示例代码:
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'DropdownButton disable problem',
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
bool _enabled = true;
int value;
List<DropdownMenuItem<int>> items = [
DropdownMenuItem(
value: 11,
child: Text('asdf'),
),
DropdownMenuItem(
value: 27,
child: Text('qwert'),
),
DropdownMenuItem(
value: 31,
child: Text('yxcv'),
)
];
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('DropdownButton problem'),
),
body: Center(
child: Column(
children: <Widget>[
Text(
'Disabling the DropdownButton looses its selection',
),
Switch(
onChanged: (v) => setState(() {
_enabled = v;
}),
value: _enabled,
),
DropdownButton(
items: items,
onChanged: _enabled
? (v) => setState(() {
value = v;
})
: null,
value: value,
)
],
),
),
);
}
}