我的活动是滞后的,它是因为代码?

时间:2018-03-01 14:44:30

标签: dart flutter

class _DaftarMuridState extends State<DaftarMurid> {
  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
        title: new Text(widget.title),
      ),
      body: new Column(
         children: <Widget>[
           new Flexible(
             child: new FirebaseAnimatedList(//new
               query: db.reference().child("Murid"),
               sort: (a, b) => a.key.compareTo(b.key),                
               padding: new EdgeInsets.all(8.0),                      
               itemBuilder: (_, DataSnapshot dataSnapshot, Animation<double> animations,x){
                 return new DaftarMuridView(
                   snapshot: dataSnapshot,
                   animation: animations,
                 );//new
               }
             ),
           ),
         ],
      ),
    );
  }
}

class DaftarMuridViewState extends State<DaftarMuridView>{
  DaftarMuridViewState({this.snapshot, this.animation});
  final DataSnapshot snapshot;
  final Animation animation;

  @override
  Widget build(BuildContext context) {
    var size = MediaQuery.of(context).size;
    String fotoUrl = snapshot.value['Foto'];
    String ig = snapshot.value['Instagram'];
    hash.putIfAbsent(snapshot.value['Nama'], () => false);
    bool expanded = hash[snapshot.value['Nama']];
    var expansionPanel = new ExpansionPanelList(
      expansionCallback: (int index, bool isExpanded) {
          setState(() {
            hash.remove(snapshot.value['Nama']);
            hash.putIfAbsent(snapshot.value['Nama'], () => !isExpanded);
            expanded = !expanded;
          });
      },
      children: [new ExpansionPanel(headerBuilder: (BuildContext context, bool isExpanded) {
      return new ListTile(
          leading: const Icon(Icons.school),
          title: new Text(
            snapshot.value['Nama'],
            textAlign: TextAlign.left,
            style: new TextStyle(
              fontSize: 20.0,
              fontWeight: FontWeight.w400,
            ),
          ));
      },
        body: new ListView(
            physics:  const BouncingScrollPhysics(),
            shrinkWrap: true,
            padding: const EdgeInsets.all(8.0),
            children: <Widget>[
              new CachedNetworkImage(
                imageUrl: fotoUrl == null?"https://drive.google.com/uc?export=download&id=1tkqO59S9jiWpkzHQNJRKLuCGYIn5kK_v":fotoUrl,
                placeholder: new CircularProgressIndicator(),
                errorWidget: new CachedNetworkImage(imageUrl: "https://drive.google.com/uc?export=download&id=1tkqO59S9jiWpkzHQNJRKLuCGYIn5kK_v"),
                fadeOutDuration: new Duration(seconds: 1),
                fadeInDuration: new Duration(seconds: 1),
                height: size.height / 2.0,
                width: size.width / 2.0,
                alignment: Alignment.center,
              ),
              new ListTile(
                leading: const Icon(Icons.today),
                title: const Text('Tanggal Lahir'),
                subtitle: new Text(snapshot.value['Tanggal Lahir']),
              ),
              new Row(
                children: <Widget>[
                  ig != null ?
                    new FlatButton(
                    onPressed: () => _instagram(ig),
                    child: new CachedNetworkImage(imageUrl: "http://diylogodesigns.com/blog/wp-content/uploads/2016/05/Instagram-logo-png-icon.png", width: size.width / 4.0, height: size.height / 4.0, ),
                    )
                  : new Container(),
                ],
              ),
            ],
        ),
        isExpanded: expanded)],
    );
    return new SizeTransition(
        sizeFactor: new CurvedAnimation(
            parent: animation, curve: Curves.easeOut),
      axisAlignment: 0.0,
      child: expansionPanel,
    );
  }
}

我的代码效率不高?该过程是从Firebase获取数据 - &gt;将其存储到列表视图

打开活动时有点滞后,可能是因为获取数据。但有没有解决方案让它不滞后?

我剪掉了一些不重要的代码。

1 个答案:

答案 0 :(得分:0)

使用Futures执行耗时的操作,因此不会冻结UI。 https://www.dartlang.org/tutorials/language/futures