Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Flutter AnimatedContainer不使用ListView_Flutter_Dart - Fatal编程技术网

Flutter AnimatedContainer不使用ListView

Flutter AnimatedContainer不使用ListView,flutter,dart,Flutter,Dart,AnimatedContainer类对我来说不是很清楚。根据下面的代码(有效)-在所有列表视图中创建所有子项。我只想填充某些列表视图,而不是全部。我该怎么做 您可以看到代码工作正常-没有错误,但无论我对代码做了多少更改,我似乎都无法使列表视图或卡片具有唯一的子级(或expandedContainers) 这真的让我很苦恼 当我按下向下按钮时,它会展开所有内容,而不仅仅是我需要展开的单元格 class Home extends StatelessWidget { @override Wid

AnimatedContainer类对我来说不是很清楚。根据下面的代码(有效)-在所有列表视图中创建所有子项。我只想填充某些列表视图,而不是全部。我该怎么做

您可以看到代码工作正常-没有错误,但无论我对代码做了多少更改,我似乎都无法使列表视图或卡片具有唯一的子级(或expandedContainers)

这真的让我很苦恼

当我按下向下按钮时,它会展开所有内容,而不仅仅是我需要展开的单元格

class Home extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      backgroundColor: Colors.grey,
      appBar: new AppBar(
        title: new Text("Expandable List"),
        backgroundColor: Colors.redAccent,
      ),
      body: new ListView.builder(
        itemBuilder: (BuildContext context, int index) {
          return new ExpandableListView(title: "Title $index");
        },
        itemCount: 5,
      ),
    );
  }
}

class ExpandableListView extends StatefulWidget {
  final String title;

  const ExpandableListView({Key key, this.title}) : super(key: key);

  @override
  _ExpandableListViewState createState() => new _ExpandableListViewState();
}

class _ExpandableListViewState extends State<ExpandableListView> {
  bool expandFlag = false;

  @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,
            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.orange,
                        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, color: Colors.white),
                )
              ],
            ),
          ),
          new ExpandableContainer(
              expanded: expandFlag,
              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(
                        "Cool $index",
                        style: new TextStyle(fontWeight: FontWeight.bold, color: Colors.white),
                      ),
                      leading: new Icon(
                        Icons.local_pizza,
                        color: Colors.white,
                      ),
                    ),
                  );
                },
                itemCount: 15,
              ))
        ],
      ),
    );
  }
}

class ExpandableContainer extends StatelessWidget {
  final bool expanded;
  final double collapsedHeight;
  final double expandedHeight;
  final Widget child;

  ExpandableContainer({
    @required this.child,
    this.collapsedHeight = 0.0,
    this.expandedHeight = 300.0,
    this.expanded = true,
  });

  @override
  Widget build(BuildContext context) {
    double screenWidth = MediaQuery.of(context).size.width;
    return new AnimatedContainer(
      duration: new Duration(milliseconds: 500),
      curve: Curves.easeInOut,
      width: screenWidth,
      height: expanded ? expandedHeight : collapsedHeight,
      child: new Container(
        child: child,
        decoration: new BoxDecoration(border: new Border.all(width: 1.0, color: Colors.blue)),
      ),
    );
  }
}
class Home扩展了无状态小部件{
@凌驾
小部件构建(构建上下文){
归还新脚手架(
背景颜色:颜色。灰色,
appBar:新的appBar(
标题:新文本(“可扩展列表”),
背景颜色:Colors.redAccent,
),
正文:新建ListView.builder(
itemBuilder:(构建上下文,int索引){
返回新的ExpandableListView(标题:“标题$index”);
},
物品计数:5,
),
);
}
}
类ExpandableListView扩展StatefulWidget{
最后的字符串标题;
const ExpandableListView({Key-Key,this.title}):super(Key:Key);
@凌驾
_ExpandableListViewState createState()=>new_ExpandableListViewState();
}
类_ExpandableListViewState扩展状态{
bool expandFlag=false;
@凌驾
小部件构建(构建上下文){
退回新货柜(
边距:新边集。对称(垂直:1.0),
子:新列(
儿童:[
新容器(
颜色:颜色,蓝色,
填充:新边集。对称(水平:5.0),
孩子:新的一排(
mainAxisAlignment:mainAxisAlignment.spaceBetween,
儿童:[
新图标按钮(
图标:新容器(
身高:50.0,
宽度:50.0,
装饰:新盒子装饰(
颜色:颜色。橙色,
形状:BoxShape.circle,
),
孩子:新中心(
孩子:新图标(
expandFlag?Icons.keyboard\u arrow\u up:Icons.keyboard\u arrow\u down,
颜色:颜色,白色,
尺寸:30.0,
),
),
),
已按下:(){
设置状态(){
expandFlag=!expandFlag;
});
}),
新文本(
widget.title,
样式:新文本样式(fontWeight:fontWeight.bold,颜色:Colors.white),
)
],
),
),
新型可扩展容器(
扩展:expandFlag,
子项:新建ListView.builder(
itemBuilder:(构建上下文,int索引){
退回新货柜(
装饰:
新框装饰(边框:新边框。全部(宽度:1.0,颜色:颜色。灰色),颜色:颜色。黑色),
孩子:新的ListTile(
标题:新文本(
“酷$index”,
样式:新文本样式(fontWeight:fontWeight.bold,颜色:Colors.white),
),
领先:新图标(
Icons.local_pizza,
颜色:颜色,白色,
),
),
);
},
物品计数:15,
))
],
),
);
}
}
类ExpandableContainer扩展了无状态小部件{
最终目标扩大;
最终双折高度;
最终双膨胀高度;
最后一个孩子;
可膨胀容器({
@需要这个孩子,
此值为0.0。折叠高度=0.0,
此值为0.1,扩展高度=300.0,
this.expanded=true,
});
@凌驾
小部件构建(构建上下文){
double screenWidth=MediaQuery.of(context).size.width;
返回新的动画容器(
持续时间:新的持续时间(毫秒:500),
曲线:Curves.easeInOut,
宽度:屏幕宽度,
高度:展开?展开高度:折叠高度,
子容器:新容器(
孩子:孩子,
装饰:新框装饰(边框:新边框。全部(宽度:1.0,颜色:Colors.blue)),
),
);
}
}

能否尝试将ValueKey(“$index”)添加到每个可扩展容器中?在您的ListView.builder中,如下所示:返回新的ExpandableListView(标题:“title$index”,key:ValueKey(“$index”))嗨,我对这一点很陌生,但您在这一点上失去了我,Zhanghir。你能再解释一下吗?谢谢,我还不太擅长这个。但是键基本上是相同对象的标识符,所以通过在ListView.builder的顶部应用键,就像我在前面的评论中所展示的那样,您可以告诉Flatter它们是不同的。试试看,它可能有用。老实说,我不认为这是最好的做法,但对我来说,Flitter布局的建筑总是一帆风顺。有时候你会遇到麻烦。你能试着把ValueKey(“$index”)添加到每个可扩展的容器中吗?在您的ListView.builder中,如下所示:返回新的ExpandableListView(标题:“title$index”,key:ValueKey(“$index”))嗨,我对这一点很陌生,但您在这一点上失去了我,Zhanghir。你能再解释一下吗?谢谢,我还不太擅长这个。但是键基本上是相同对象的标识符,所以通过在ListView.builder的顶部应用键,就像我在前面的评论中所展示的那样,您可以告诉Flatter它们是不同的。试试看,它可能有用。老实说,我不认为这是最好的做法,但对我来说,Flitter布局的建筑总是一帆风顺。只是有时候不得不忍受。