- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
我想在按下后更改每个图标的颜色。但是 Expandable Container
中的所有图标在按下其中一个后都会发生变化。
class _ExpandableListViewState extends State<ExpandableListView> {
bool expandFlag = false;
Color _iconColor = Colors.white;
@override
Widget build(BuildContext context) {
return new Container(
margin: new EdgeInsets.symmetric(vertical: 1.0),
child: new Column(
children: <Widget>[
new Container(
.
.
.
),
new ExpandableContainer(
expanded: expandFlag,
expandedHeight: ...
child: new ListView.builder(
itemBuilder: (BuildContext context, int index) {
return new Container(
decoration:
new BoxDecoration(border: new Border.all(width: 1.0, color: Colors.grey), color: Colors.black),
child: new ListTile(
title: ...
leading: new IconButton(
icon: Icon(Icons.star, color: _iconColor),
onPressed: () {
setState(() {
_iconColor = _iconColor == Colors.white ? Colors.yellow : Colors.white;
});
},
),
subtitle: ...
),
);
},
itemCount: ...,
))
],
),
);
}
}
class ExpandableContainer extends StatelessWidget {
final bool expanded;
final double expandedHeight;
final Widget child;
ExpandableContainer({
@required this.child,
this.expandedHeight,
this.expanded = true,
});
@override
Widget build(BuildContext context) {
.
.
.
}
完整代码:
import 'package:flutter/material.dart';
import 'data.dart';
void main() {
runApp(new MaterialApp(home: new Home()));
}
class Home extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new Scaffold(
backgroundColor: Colors.grey,
appBar: new AppBar(
title: new Text("Expandable List", style: TextStyle(color: Colors.black)),
backgroundColor: Colors.lightGreen,
),
body: new ListView.builder(
itemBuilder: (BuildContext context, int index) {
return new ExpandableListView(title: broadcast[index].title, ind: index);
},
itemCount: broadcast.length,
),
);
}
}
class ExpandableListView extends StatefulWidget {
final String title;
final int ind;
const ExpandableListView({this.title, this.ind});
@override
_ExpandableListViewState createState() => new _ExpandableListViewState();
}
class _ExpandableListViewState extends State<ExpandableListView> {
bool expandFlag = false;
Color _iconColor = Colors.white;
@override
Widget build(BuildContext context) {
return new Container(
margin: new EdgeInsets.symmetric(vertical: 1.0),
child: new Column(
children: <Widget>[
new Container(
color: Colors.blue[300],
padding: new EdgeInsets.symmetric(horizontal: 5.0),
child: new Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
new IconButton(
icon: new Container(
height: 50.0,
width: 50.0,
decoration: new BoxDecoration(
color: Colors.deepOrange,
shape: BoxShape.circle,
),
child: new Center(
child: new Icon(
expandFlag ? Icons.keyboard_arrow_up : Icons.keyboard_arrow_down,
color: Colors.white,
size: 30.0,
),
),
),
onPressed: () {
setState(() {
expandFlag = !expandFlag;
});
}),
new Text(
widget.title,
style: new TextStyle(fontWeight: FontWeight.bold,fontSize: 20.0, color: Colors.black87),
)
],
),
),
new ExpandableContainer(
expanded: expandFlag,
expandedHeight: 90.0 * (broadcast[widget.ind].contents.length < 4 ? broadcast[widget.ind].contents.length : 4), // + (0.0 ?: 29.0),
child: new ListView.builder(
itemBuilder: (BuildContext context, int index) {
return new Container(
decoration:
new BoxDecoration(border: new Border.all(width: 1.0, color: Colors.grey), color: Colors.black),
child: new ListTile(
title: new Text(
broadcast[widget.ind].contents[index],
style: new TextStyle(fontWeight: FontWeight.bold, color: Colors.lightGreen),
textAlign: TextAlign.right,
),
leading: new IconButton(
icon: Icon(Icons.star, color: _iconColor),
onPressed: () {
setState(() {
_iconColor = _iconColor == Colors.white ? Colors.yellow : Colors.white;
});
},
),
subtitle: new Text ('${broadcast[widget.ind].team[index]}\n${broadcast[widget.ind].time[index]} ${broadcast[widget.ind].channel[index]}',
textAlign: TextAlign.right, style:TextStyle(color: Colors.white)),
isThreeLine: true,
),
);
},
itemCount: broadcast[widget.ind].contents.length,
))
],
),
);
}
}
class ExpandableContainer extends StatelessWidget {
final bool expanded;
final double expandedHeight;
final Widget child;
//final Color iconColor;
ExpandableContainer({
@required this.child,
this.expandedHeight,
this.expanded = true,
//this.iconColor,
});
@override
Widget build(BuildContext context) {
double screenWidth = MediaQuery.of(context).size.width;
return new AnimatedContainer(
duration: new Duration(milliseconds: 100),
curve: Curves.easeInOut,
width: screenWidth,
height: expanded ? expandedHeight : 0.0,
child: new Container(
child: child,
decoration: new BoxDecoration(border: new Border.all(width: 1.0, color: Colors.blue)),
),
);
}
}
最佳答案
您需要将列表项设为状态 _iconColor
状态列表磁贴
class StatefulListTile extends StatefulWidget {
const StatefulListTile({this.subtitle, this.title});
final String subtitle, title;
@override
_StatefulListTileState createState() => _StatefulListTileState();
}
class _StatefulListTileState extends State<StatefulListTile> {
Color _iconColor = Colors.white;
@override
Widget build(BuildContext context) {
return new Container(
decoration: new BoxDecoration(
border: new Border.all(width: 1.0, color: Colors.grey),
color: Colors.black),
child: new ListTile(
title: new Text(
widget?.title ?? "",
style: new TextStyle(
fontWeight: FontWeight.bold, color: Colors.lightGreen),
textAlign: TextAlign.right,
),
leading: new IconButton(
icon: Icon(Icons.star, color: _iconColor),
onPressed: () {
setState(() {
_iconColor =
_iconColor == Colors.white ? Colors.yellow : Colors.white;
});
},
),
subtitle: new Text(widget?.subtitle ?? "",
textAlign: TextAlign.right, style: TextStyle(color: Colors.white)),
isThreeLine: true,
),
);
}
}
用法
class ExpandableListView extends StatefulWidget {
final String title;
final int ind;
const ExpandableListView({this.title, this.ind});
@override
_ExpandableListViewState createState() => new _ExpandableListViewState();
}
class _ExpandableListViewState extends State<ExpandableListView> {
bool expandFlag = false;
Color _iconColor = Colors.white;
@override
Widget build(BuildContext context) {
return new Container(
margin: new EdgeInsets.symmetric(vertical: 1.0),
child: new Column(
children: <Widget>[
new Container(
color: Colors.blue[300],
padding: new EdgeInsets.symmetric(horizontal: 5.0),
child: new Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
new IconButton(
icon: new Container(
height: 50.0,
width: 50.0,
decoration: new BoxDecoration(
color: Colors.deepOrange,
shape: BoxShape.circle,
),
child: new Center(
child: new Icon(
expandFlag
? Icons.keyboard_arrow_up
: Icons.keyboard_arrow_down,
color: Colors.white,
size: 30.0,
),
),
),
onPressed: () {
setState(() {
expandFlag = !expandFlag;
});
}),
new Text(
widget.title,
style: new TextStyle(
fontWeight: FontWeight.bold,
fontSize: 20.0,
color: Colors.black87),
)
],
),
),
new ExpandableContainer(
expanded: expandFlag,
expandedHeight: 90.0 *
(broadcast[widget.ind].contents.length < 4
? broadcast[widget.ind].contents.length
: 4), // + (0.0 ?: 29.0),
child: new ListView.builder(
itemBuilder: (BuildContext context, int index) {
return StatefulListTile(
title: broadcast[widget.ind].contents[index],
subtitle:
'${broadcast[widget.ind].team[index]}\n${broadcast[widget.ind].time[index]} ${broadcast[widget.ind].channel[index]}',
);
},
itemCount: broadcast[widget.ind].contents.length,
))
],
),
);
}
}
关于dart - Flutter: `onPressed` 作用于 ListView 中的所有 `IconButton`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55104585/
我有以下 flatlist 渲染方法,在点击列表项时,它将调用 this._onPress 方法: render() { return (
我正在使用很棒的组件来自Native Base 。 import React, { Component } from 'react' import {View, Picker, Text, Butto
我正在做一个更大的项目,但不知道为什么这个小部分让我花了将近一整天的时间。 import 'package:flutter/material.dart'; void main() { runApp
我刚刚将我的 react-native 项目移植到 typescript 并且有一个关于函数作为 Prop 的问题 我通过: this.props.navigation.navigate("Card
我已经实现了教程中的 React Navigation 示例 https://reactnavigation.org/docs/intro/而且效果很好。 navigate('Chat')} t
我从RaisedButton创建了一个扩展类。传递onPressed参数时,该类拒绝说驱动器为空。 类 class VecRaisedButton extends RaisedButton { V
我正在制作简单的自定义文本按钮 SizedBox( height: 40, child: Material( color: Colors.transparent
onPressed 错误尝试传递 onPressed 函数但出现错误。 错误 - 无法将参数类型“Function”分配给参数类型“void Function()?”。 class CircleBut
onPressed 错误尝试传递 onPressed 函数但出现错误。 错误 - 无法将参数类型“Function”分配给参数类型“void Function()?”。 class CircleBut
RESET 我在上面看到的代码中使用了可触摸的不透明度。当我更改状态 working_pressure 但不按下按钮时,控制台会记录状态。问题是按下按钮时,根本没有记录任何内容。 我的最
我的应用程序中有这个聊天屏幕,我在旁边的 button 及其 onPress() 的帮助下发送消息, 代码如下: import React, { Component } from "react"; i
正在学习native React并学习更多关于javascript的知识,所以我仍然不明白关于它的行为的很多事情。我使用 TouchableOpacity 及其 onPress 属性创建了一个按钮组件
我正在尝试为我使用 Flutter 创建的游戏实现 onPressed 函数。如果我使用 RaisedButton 然后 onPressed 工作,但因为我想要一个图像然后我必须使用 Material
我需要在下面的图标上插入 onpressed() 函数(导航器推送到其他页面): Column buildButtonColumn(IconData icon, String label) {
我有这个容器: new Container( width: 500.0, padding: new EdgeInsets.fromLTRB(20.0, 40.0, 20.0, 40
我正在尝试使用 onPressed 切换到 Flutter 项目中的不同屏幕,但它没有生成任何结果,不确定是什么原因。 这是主屏幕页面:
我的平面列表中有一些按钮,如下所示 const renderItem = ({ item }) => ; const Item = ({ name, slug }) => { return
我在 React Native 上处理 onPress 事件。第一次,我写了那个代码: =this.props.minimum) ? this.props.onDecrement
我如何实现这种效果?单击日期后,我想扩展我的 AppBar。 最佳答案 最简单的方法是设置一个变量,然后改变 bottom 的高度。小部件。 Scaffold( appBar: AppBa
我已经使用 Flutter 大约 8 周了。 太棒了,毫无疑问。 然而,自从我开始以来,一直有一些问题困扰着我,那就是为什么有些小部件有 onTap方法而其他人有 onPressed ?谷歌无法给我任
我是一名优秀的程序员,十分优秀!