我正在尝试使用Colorpicker中的最近使用的颜色来构建下拉菜单。因此,我得到了一个具有DropdownMenuItems的列表:
static Color recentUsedColor1;
...(5 of them)
List<DropdownMenuItem<Color>> recentUsedColors = [
DropdownMenuItem(
child: Container(height: 20,width: 20,color: recentUsedColor1,),
value: recentUsedColor1,
),
...(5 of them) ];
和我的Build方法中的一个DropdownButton:
``
DropdownButton(
value: recentUsedColor1,
items: recentUsedColors,
onChanged: (value) {
setState(() {
brushColor = value;
});
},
),``
现在,如果选择了一种新颜色,则将旧颜色放置在最近使用的Color1变量上:
``ColorPicker(
color: Colors.blue,
onChanged: (value) {
setState(() {
recentUsedColor1 = brushColor;
brushColor = value;
});
})
``
但是以某种方式我立即收到错误消息,我在选色器中选择了一种新颜色(此刻,我尝试更改变量lastUsedColor1),但我不明白为什么。
详细错误:
I/flutter ( 3095): ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
The following assertion was thrown building Grid(dirty, dependencies: [MediaQuery, LocalizationsScope-[GlobalKey#0922e], _InheritedTheme], state: GridState#ea90e):
'package:flutter/src/material/dropdown.dart': Failed assertion: line 620 pos 15: 'items == null ||
items.isEmpty || value == null || items.where((DropdownMenuItem<T> item) => item.value ==
value).length == 1': is not true.
答案 0 :(得分:1)
错误意味着lastUsedColors不是init或为空
items: recentUsedColors,
不确定使用哪种颜色选择器。但概念是相同的
使用List<Color>
记录历史颜色并生成DropdownButton
代码段
DropdownButton<Color>(
//isDense: true,
hint: Text('Choose a goal category'),
value: dropdownValue,
icon: Icon(Icons.check_circle_outline),
iconSize: 24,
elevation: 16,
style: TextStyle(color: Colors.deepPurple),
underline: Container(
height: 2,
color: Colors.blue[300],
),
onChanged: (Color newValue) {
setState(() {
dropdownValue = newValue;
});
},
items: recentUsedColors
.map<DropdownMenuItem<Color>>((Color value) {
return DropdownMenuItem<Color>(
value: value,
child: Container(height: 20, width: 20, color: value),
);
}).toList(),
),
完整代码
import 'package:flutter/material.dart';
import 'package:flutter_colorpicker/flutter_colorpicker.dart';
import 'package:flutter_colorpicker/material_picker.dart';
import 'package:flutter_colorpicker/block_picker.dart';
import 'package:flutter_colorpicker/utils.dart';
void main() => runApp(MaterialApp(home: MyApp()));
class MyApp extends StatefulWidget {
@override
State<StatefulWidget> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
Color currentColor = Colors.amber;
List<Color> recentUsedColors = [];
Color dropdownValue;
void changeColor(Color color) {
setState(() {
currentColor = color;
recentUsedColors.add(color);
});
}
@override
Widget build(BuildContext context) {
return DefaultTabController(
length: 3,
child: Scaffold(
appBar: AppBar(
title: Text('Flutter Color Picker Example'),
bottom: TabBar(
tabs: <Widget>[
const Tab(text: 'HSV'),
const Tab(text: 'Material'),
const Tab(text: 'Block'),
],
),
),
body: TabBarView(
physics: const NeverScrollableScrollPhysics(),
children: <Widget>[
Center(
child: Column(
children: <Widget>[
DropdownButton<Color>(
//isDense: true,
hint: Text('Choose a goal category'),
value: dropdownValue,
icon: Icon(Icons.check_circle_outline),
iconSize: 24,
elevation: 16,
style: TextStyle(color: Colors.deepPurple),
underline: Container(
height: 2,
color: Colors.blue[300],
),
onChanged: (Color newValue) {
setState(() {
dropdownValue = newValue;
});
},
items: recentUsedColors
.map<DropdownMenuItem<Color>>((Color value) {
return DropdownMenuItem<Color>(
value: value,
child: Container(height: 20, width: 20, color: value),
);
}).toList(),
),
RaisedButton(
elevation: 3.0,
onPressed: () {
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
titlePadding: const EdgeInsets.all(0.0),
contentPadding: const EdgeInsets.all(0.0),
content: SingleChildScrollView(
child: ColorPicker(
pickerColor: currentColor,
onColorChanged: changeColor,
colorPickerWidth: 300.0,
pickerAreaHeightPercent: 0.7,
enableAlpha: true,
displayThumbColor: true,
enableLabel: true,
paletteType: PaletteType.hsv,
pickerAreaBorderRadius: const BorderRadius.only(
topLeft: const Radius.circular(2.0),
topRight: const Radius.circular(2.0),
),
),
),
);
},
);
},
child: const Text('Change me'),
color: currentColor,
textColor: useWhiteForeground(currentColor)
? const Color(0xffffffff)
: const Color(0xff000000),
),
],
),
),
Center(
child: RaisedButton(
elevation: 3.0,
onPressed: () {
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
titlePadding: const EdgeInsets.all(0.0),
contentPadding: const EdgeInsets.all(0.0),
content: SingleChildScrollView(
child: MaterialPicker(
pickerColor: currentColor,
onColorChanged: changeColor,
enableLabel: true,
),
),
);
},
);
},
child: const Text('Change me'),
color: currentColor,
textColor: useWhiteForeground(currentColor)
? const Color(0xffffffff)
: const Color(0xff000000),
),
),
Center(
child: RaisedButton(
elevation: 3.0,
onPressed: () {
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: Text('Select a color'),
content: SingleChildScrollView(
child: BlockPicker(
pickerColor: currentColor,
onColorChanged: changeColor,
),
),
);
},
);
},
child: const Text('Change me'),
color: currentColor,
textColor: useWhiteForeground(currentColor)
? const Color(0xffffffff)
: const Color(0xff000000),
),
),
],
),
),
);
}
}
工作演示