Flutter 在按钮上刷新ListView.builder,在颤振中单击
我正在使用下面的代码,其中我有一个带有开关的ListView。 我想实现类似于当我点击Flutter 在按钮上刷新ListView.builder,在颤振中单击,flutter,flutter-listview,Flutter,Flutter Listview,我正在使用下面的代码,其中我有一个带有开关的ListView。 我想实现类似于当我点击RaisedButton-它将重新加载ListView和开关的所有值。value应更改为true或false。 用户可以从ListView中的项目或单击按钮更改开关的值。 我不知道如何更改ListView中的值或所有开关 return Column( children: <Widget>[ Container( width: MediaQuery.o
RaisedButton
-它将重新加载ListView和开关的所有值。value
应更改为true或false。
用户可以从ListView中的项目或单击按钮更改开关的值。
我不知道如何更改ListView中的值或所有开关
return Column(
children: <Widget>[
Container(
width: MediaQuery.of(context).size.width / 2,
height: 100,
padding: EdgeInsets.all(20),
child: RaisedButton(
onPressed: () {
},
child: Text(
BTN_START_TRIP,
style: new TextStyle(
fontSize: 20.0,
),
),
textColor: buttonFontColor,
color: buttonColor,
shape: new RoundedRectangleBorder(
borderRadius: new BorderRadius.circular(15.0))
),
),
Expanded(
child: ListView.builder(
padding: EdgeInsets.all(3.0),
// Let the ListView know how many items it needs to build.
itemCount: snapshot.data.results.length,
// Provide a builder function. This is where the magic happens.
// Convert each item into a widget based on the type of item it is.
itemBuilder: (context, index){
return Container(
height: 120,
child: Card(
elevation: 10,
child: InkWell(
splashColor: Colors.blue.withAlpha(30),
onTap: () {
print(snapshot.data.results[index].original_title);
},
child: Container(
height: 120,
child: Row(
children: <Widget>[
Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Container(
padding: EdgeInsets.fromLTRB(20, 0, 5, 0),
width: MediaQuery.of(context).size.width -90,
child: Align(
alignment: Alignment.topLeft,
child: Text(snapshot.data.results[index].original_title,
textAlign: TextAlign.left,
style: TextStyle(fontSize: defaultTitleFontsize, color: defaultFontColor),
maxLines: 5),
),
),
Container(
padding: EdgeInsets.fromLTRB(20, 0, 5, 0),
child: Align(
alignment: Alignment.topLeft,
child: Text(snapshot.data.results[index].original_language,textAlign: TextAlign.left,style: TextStyle(fontSize: defaultsubTitleFontsize, color: defaultFontColor)),
),
),
],
),
Row(
mainAxisAlignment: MainAxisAlignment.end,
crossAxisAlignment: CrossAxisAlignment.end,
children: <Widget>[
Container(
child: Switch(
value: false,
onChanged: (value){
setState(() {
print(value);
});
}
),
),
],
),
]
),
),
),
),
);
},
),
)
],
);
返回列(
儿童:[
容器(
宽度:MediaQuery.of(context).size.width/2,
身高:100,
填充:边缘设置。全部(20),
孩子:升起按钮(
已按下:(){
},
子:文本(
BTN_开始_行程,
样式:新文本样式(
字体大小:20.0,
),
),
textColor:buttonFontColor,
颜色:纽扣颜色,
形状:新的RoundedRectangleBorder(
边界半径:新边界半径。圆形(15.0))
),
),
扩大(
子项:ListView.builder(
填充:所有边缘设置(3.0),
//让ListView知道需要生成多少项。
itemCount:snapshot.data.results.length,
//提供一个生成器函数。这就是神奇发生的地方。
//根据项目的类型将每个项目转换为小部件。
itemBuilder:(上下文,索引){
返回容器(
身高:120,
孩子:卡片(
标高:10,
孩子:InkWell(
飞溅颜色:颜色。蓝色。带有alpha(30),
onTap:(){
打印(快照.数据.结果[索引].原始标题);
},
子:容器(
身高:120,
孩子:排(
儿童:[
纵队(
mainAxisAlignment:mainAxisAlignment.center,
crossAxisAlignment:crossAxisAlignment.start,
儿童:[
容器(
填充:来自LTRB(20,0,5,0)的边缘设置,
宽度:MediaQuery.of(context).size.width-90,
子对象:对齐(
对齐:alignment.topLeft,
子项:文本(快照.数据.结果[索引])。原始标题,
textAlign:textAlign.left,
样式:TextStyle(fontSize:defaultTitleFontsize,颜色:defaultFontColor),
最大线:5),
),
),
容器(
填充:来自LTRB(20,0,5,0)的边缘设置,
子对象:对齐(
对齐:alignment.topLeft,
子项:文本(snapshot.data.results[index]。原始语言,textAlign:textAlign.left,样式:TextStyle(fontSize:defaultsubTitleFontsize,颜色:defaultFontColor)),
),
),
],
),
划船(
mainAxisAlignment:mainAxisAlignment.end,
crossAxisAlignment:crossAxisAlignment.end,
儿童:[
容器(
孩子:开关(
值:false,
一旦更改:(值){
设置状态(){
印刷品(价值);
});
}
),
),
],
),
]
),
),
),
),
);
},
),
)
],
);
您需要一个变量来决定开关是打开还是关闭。在特定事件期间(例如,单击按钮),将变量设置为适当的值,并通过调用setState
重新触发构建(重新绘制UI)。要实现这一点,需要有状态小部件的上述逻辑部分