【问题标题】:My activity is lag, it it because of the code?我的活动滞后,是因为代码吗?
【发布时间】:2018-03-01 14:44:29
【问题描述】:
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 获取数据 -> 将其存储到列表视图

打开活动有点滞后,可能是因为获取数据。但是有没有办法让它不滞后?

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

【问题讨论】:

  • 为什么要从网页网址加载像普通黑脸和 instagram 徽标这样的图像?您可以将它们作为资产添加到项目中并更快地加载它们。此外,从网络加载“错误”图像没有意义,因为用户可能无法连接到该服务器,因此会出现错误。
  • 谢谢,我会从资产中加载它

标签: dart flutter


【解决方案1】:

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

【讨论】:

  • 我忘记改标题了,我认为它不需要未来。几乎所有代码都在构建小部件
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-06
  • 2017-10-04
  • 2021-11-11
  • 1970-01-01
  • 2013-07-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多