【发布时间】:2021-06-29 18:21:24
【问题描述】:
我正在尝试制作显示SvgPicture.assets() 的简单应用程序,用户可以使用颜色选择器更改SvgPicture() 的颜色。
我有 2 个小部件:
- 用于显示
SvgPicture()将其称为 Svg.dart -
main.dart 包含
BottomNavigationView,其中一个选项卡打开颜色选择器,Svg.dart
我在使用setState(() {}) 时遇到了一些错误,但我设法以某种方式修复了错误,但它并没有改变颜色,当我尝试更改main.dart 的背景颜色时,它工作得很好。
这是我的代码:
-
BottomNavigationBar的onItemTap()方法void _onItemTap(int index) { setState(() { if (index == 0) { // First Tab } if (index == 1) { // SecondTab } if (index == 2) { // Third Tab } if (index == 3) { showDialog( context: context, builder: (BuildContext context){ return AlertDialog( content: SingleChildScrollView( child: new ColorPicker( pickerColor: Colors.red, onColorChanged: (Color colorChanged) { color = colorChanged; // (color) is assigned default value of Colors.red // here I'm trying to assign new value to (color) }, ),//ColorPicker ),//SingleChildScrollView );//AlertDialog }); } }); } -
然后在
Svg.dart中我创建了另一个变量Color picked = Colors.red并将红色指定为默认值。这就是Svg.dart小部件代码的样子:Widget build(BuildContext context) { setState(() { picked = main().createState().color; }); return CustomMultiChildLayout( delegate: TempDelegate( position: Offset.zero ), children: [ buildLayoutId(ids.shirtId, MyConstants.shirt, picked) ], ); } LayoutId buildLayoutId(Object id, String item, Color color) { return LayoutId( id: id, child: SvgPicture.asset( item, color: color, ), ); }
我尝试寻找颤振文档,但我真的不知道问题是如何/在哪里,也没有找到教程,请帮助
编辑
这是main.dart
class Main extends StatefulWidget{
@override
_MainState createState() => _MainState();
}
class _Main extends State<Main> {
int _slectedIndex = 0;
Color color = MyConstants.darkWhite;
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.black,
body: Center(
child: Svg(),
),
bottomNavigationBar: BottomNavigationBar(
items: const<BottomNavigationBarItem>[
BottomNavigationBarItem(
label: "",
icon: Icon(Icons.home),
),
BottomNavigationBarItem(
label: "",
icon: Icon(Icons.home),
),
BottomNavigationBarItem(
label: "",
icon: Icon(Icons.home),
),
BottomNavigationBarItem(
label: "",
icon: Icon(Icons.home),
),
],
onTap: _onItemTap,
),
);
}
void _onItemTap(int index) {
setState(() {
if (index == 0) {
// do something
}
if (index == 1) {
// do something
}
if (index == 2) {
// do something
}
if (index == 3) {
showDialog(
context: context,
builder: (BuildContext context){
return AlertDialog(
content:
SingleChildScrollView(
child: new ColorPicker(
pickerColor: Colors.red,
onColorChanged: (Color colorChanged) {
setState(() {
color = colorChanged;
});
},
),
),
);
});
}
});
}
}
和Svg.dart
class Svg extends StatefulWidget{
//Color picked;
@override
SvgState createState() => Svg();
}
class SvgState extends State<Svg> {
@override
Widget build(BuildContext context) {
return CustomMultiChildLayout(
delegate: SvgDelegate(
position: Offset.zero
),
children: [
buildLayoutId(ids.shirtId, MyConstants.shirt, CreateKit().createState().color)
],
);
}
LayoutId buildLayoutId(Object id, String item, Color color) {
return LayoutId(
id: id,
child: SvgPicture.asset(
item,
color: color,
),
);
}
}
在Svg.dart 中为CustomMultichildLayout 扩展MultiChildLyoutDelegate 的类
class SvgDelegate extends MultiChildLayoutDelegate{
final Offset position;
SvgDelegate({
this.position
});
@override
void performLayout(Size size) {
Size leadSize = Size.zero;
itemLayout(leadSize, size, ids.shirtId);
}
void itemLayout(Size leadSize, Size size, Object id) {
if(hasChild(id)){
leadSize = layoutChild(
id,
BoxConstraints.loose(size),
);
}
}
@override
bool shouldRelayout(TempDelegate oldDelegate) {
return oldDelegate.position != position;
}
}
【问题讨论】:
-
如果
svg和BottomNavigationBar在main中,color = colorChanged;应该被 setState 包围。我认为您不需要在svg中使用picked,全局变量color就可以了。你能分享main的代码吗? -
你能分享你完整的 StatefulWidget 代码吗?顺便说一句,在构建期间不要使用
setState((){}) -
@EdwardLi 我应用了更改并分享了编辑后的代码,请查看。
-
@Gilang 我不确定是否理解你,我是 noop 哈哈