现有代码显示了不同兴趣的按钮列表。用户可以点击选择他们喜欢的兴趣。
但是,如果用户已经事先选择了他们的兴趣并返回到这个页面,那么让用户重新选择一个新的状态是不合逻辑的。
我希望重新填充用户先前选择的内容,并在屏幕上作为选择(其中= widget.viewInterest.isChosen)进行反射。容器的颜色将是Color(0xff0B84FE) &文本的颜色是Colors.yellow,如下面的代码所示。
假设用户选择了这个列表UserInterests = "☕Coffee",“剧院”,;
问:如何使包含这些字符串的容器成为真(即widget.viewInterest.isChosen),类似于用户已经点击了相应的按钮?
附加的是截断代码:
final List<String> artsInterests = [
" Photography",
" Theaters",
"️ Exhibitions",
" Architecture",
" Cooking",
"☕ Coffee",
"️ Design",
" Fashion",
" Reading",
" Dance",
" Pottery",
" Drawing",
" Beauty",
" Journalling",
];
StatelessWidget shortened
final List<String> artsInterests;
shortened
child: ListView.builder(
shrinkWrap: true,
scrollDirection: Axis.horizontal,
padding: const EdgeInsets.all(1),
itemCount: artsInterests.length
itemBuilder: (context, int index) {
return Interests2(AvailableInterestChosen(
artsInterests[index],
isChosen: false,
));
brackets...
child: ListView.builder(
shrinkWrap: true,
scrollDirection: Axis.horizontal,
padding: const EdgeInsets.all(1),
itemCount: artsInterests.length - 7,
itemBuilder: (context, int index) {
return Interests2(AvailableInterestChosen(
artsInterests[7 + index],
isChosen: userChosenInterests
.contains(artsInterests[7 + index]),
));
closing brackets...
List<String> chosenArtsInterests = [];
List<String> UserInterests = [
"☕ Coffee",
" Theaters",
];
class Interests2 extends StatefulWidget {
final AvailableInterestChosen viewInterest;
Interests2(this.viewInterest);
String id = 'Interests2';
@override
Interests2State createState() => Interests2State();
}
class Interests2State extends State<Interests2> {
@override
Widget build(BuildContext context) {
final height = MediaQuery.of(context).size.height;
final width = MediaQuery.of(context).size.width;
Container container = Container(
decoration shortened
decoration: BoxDecoration(
color: widget.viewInterest.isChosen && chosenInterests.length < 9
? Color(0xff0B84FE)
: Colors.white.withOpacity(0.87),
boxShadow: [
BoxShadow(
color: Colors.grey.withOpacity(0.69),
spreadRadius: 1,
blurRadius: 3,
offset: Offset(0, 1), // changes position of shadow
),
],
borderRadius: BorderRadius.circular(9),
),
child: Text(
'${widget.viewInterest.title}',
style: TextStyle(
fontSize: 15,
fontWeight: FontWeight.w600,
color: widget.viewInterest.isChosen && chosenInterests.length < 9
? Colors.white
: Colors.black),
));
if (widget.viewInterest.isChosen && chosenInterests.length < 9) {
chosenArtsInterests.add('${widget.viewInterest.title}');
print(chosenArtsInterests);
} else {
chosenArtsInterests.remove('${widget.viewInterest.title}');
print(chosenArtsInterests);
}
return GestureDetector(
onTap: () {
setState(() {
widget.viewInterest.isChosen = !widget.viewInterest.isChosen;
});
},
child: container,
);
}
}
class AvailableInterestChosen {
bool isChosen;
String title;
AvailableInterestChosen(this.title, {this.isChosen = false});
}对于显示它们被选中的显示UI的按钮,我的猜测如下
for (string i in UserInterests) setState ((){widget.viewInterest.isChosen})但是关于把它放在哪里或者确切的代码,我迷路了。如果有人有一些经验或类似的资源可以分享,我可以阅读它,这将是伟大的!
发布于 2021-05-27 04:52:30
检查元素是否在UserInterests列表中如何?
像这样的事情可能会奏效,
AvailableInterestChosen(
artsInterests[index],
isChosen: UserInterests.contains(artsInterests[index]),
)https://stackoverflow.com/questions/67705273
复制相似问题