【发布时间】:2020-03-12 03:50:07
【问题描述】:
所以我正在构建一个页面,其中有一些按钮,这些按钮将是动态选项。当您选择它突出显示的选项时,将小部件添加到列表中,然后我希望它在页面顶部显示列表。
我可以在调试模式下查看列表,所以我知道当按钮被按下时它被添加到列表中。
然而,没有发生的是页面状态没有刷新,用户无法看到。
如何让按钮同时设置其他小部件的状态以查看列表?
所以这是第一页。此页面具有将数据传递给标签的代码当按下标签并将项目添加到数字哈希列表中时,该列表位于第一个展开中
已添加评论
class Digital extends StatefulWidget {
@override
_DigitalState createState() => _DigitalState();
}
class _DigitalState extends State<Digital> {
final postRefresh = ChangeNotifier();
bool digitalSelected = false;
List digitalHash = new List();
@override
void initState() {
super.initState();
}
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Color(0xff0E0E0F),
appBar: AppBar(
iconTheme: IconThemeData(
color: Color(0xffff9900),
),
centerTitle: true,
backgroundColor: Colors.black,
title: Row(
mainAxisAlignment: MainAxisAlignment.center,
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Text(
"#", style: TextStyle(fontSize: 25, color: Color(0xffff9900), fontFamily: 'Dokyo'),
),
Text("digital", style: TextStyle(color: Colors.white, fontFamily: 'Dokyo'),)
],
),
),
body: Column(
children: <Widget>[
Expanded(
flex: 3,
child: Container(
child: Row(
children: <Widget>[
//this is where the list is being displayed
for (var digitalHashs in digitalHash) Text(digitalHashs, style: TextStyle(color: Colors.white, fontSize: 20),),
],
),
color: Colors.blue
),
),
Expanded(
flex: 6,
child: Container(
color: Colors.black,
child: ListView(
padding: EdgeInsets.fromLTRB(25, 5, 25, 5),
children: <Widget>[
Tag(
tag: "#Digital",
isSelected: digitalSelected,
list: digitalHash,
),
],
),
),
),
我希望能够重用标签...这里是标签元素
class Tag extends StatefulWidget {
final String tag;
bool isSelected;
List list;
Widget page;
Tag({Key key, @required this.tag, @required this.isSelected, this.list, this.page}) : super(key: key);
@override
_TagState createState() => _TagState();
}
class _TagState extends State<Tag> with SingleTickerProviderStateMixin{
AnimationController _animationController;
@override
void initState() {
super.initState();
_animationController =
AnimationController(vsync: this, duration: Duration(milliseconds: 300));
}
@override
void dispose() {
super.dispose();
_animationController.dispose();
}
Widget build(BuildContext context) {
return Container(
margin: EdgeInsets.fromLTRB(0, 5, 0, 5),
height: 50,
decoration: BoxDecoration(
color: widget.isSelected ? Color(0xffff9900) : Colors.black,
border: Border.all(color: Colors.white),
borderRadius: BorderRadius.circular(10)),
child: InkWell(
onTap: () {
_handleOnPressed();
},
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Container(
padding: EdgeInsets.fromLTRB(20, 0, 0, 0),
alignment: Alignment.centerLeft,
child: Text(
widget.tag,
style: TextStyle(
fontSize: 20, color: widget.isSelected ? Colors.black : Colors.white, fontFamily: 'Dokyo'),
)),
Container(
padding: EdgeInsets.fromLTRB(0, 0, 20, 0),
child: AnimatedIcon(
icon: AnimatedIcons.home_menu,
progress: _animationController,
color: widget.isSelected ? Colors.black : Colors.white,
))
],
),
),
);
}
void _handleOnPressed() {
setState(() {
widget.isSelected = !widget.isSelected;
widget.isSelected
? _animationController.forward()
: _animationController.reverse();
widget.isSelected ? widget.list.add(widget.tag) : widget.list.remove(widget.tag);
print(widget.list);
});
}
}
当我推送标签时,数字哈希列表没有更新,没有错误。但标签确实突出显示。
我只是想更新一个展开的部分,以便它显示列表。
【问题讨论】:
-
你能把代码贴到那个吗?需要更多关于你在哪里使用什么等的信息。
-
请贴出运行此程序时终端中弹出的代码和任何错误日志,以便我们提供更好的帮助。
-
OK 代码更新