美文网首页
Flutter构造方法两种写法

Flutter构造方法两种写法

作者: YannChee | 来源:发表于2019-06-17 16:55 被阅读0次

    第一种写法(常见写法)

    class NavigationIconView {
      NavigationIconView ( Widget icon, Widget title, TickerProvider vsync) {
        this.item = new BottomNavigationBarItem(
          icon: icon,
          title: title,
        );
    
        this.controller = new AnimationController(
          duration: kThemeAnimationDuration,
          vsync: vsync
        );
      }
    }
    

    第二种写法

    class NavigationIconView {
      NavigationIconView ({
        Widget icon,
        Widget title,
        TickerProvider vsync,
      }) :
      item = new BottomNavigationBarItem(
          icon: icon,
        title: title,
      ),
      controller = new AnimationController(
          duration: kThemeAnimationDuration,
          vsync: vsync
      );
    }
    

    在调用上的区别:
    第一种调用

      _navigationViews = <NavigationIconView> [
          new NavigationIconView(new Icon(Icons.assessment), new Text('首页'), this),
          new NavigationIconView(new Icon(Icons.all_inclusive), new Text("想法"), this),
          new NavigationIconView(new Icon(Icons.add_shopping_cart), new Text("市场"), this),
          new NavigationIconView(new Icon(Icons.add_alert), new Text("通知"), this),
          new NavigationIconView(new Icon(Icons.perm_identity), new Text("我的"), this),
        ];
    

    第二种调用

     _navigationViews = <NavigationIconView> [
          new NavigationIconView(
             icon: new Icon(Icons.assessment),
            title: new Text('首页'),
            vsync: this,
        ),
          new NavigationIconView(
            icon: new Icon(Icons.all_inclusive),
            title: new Text("想法"),
            vsync: this,
          ),
          new NavigationIconView(
            icon: new Icon(Icons.add_shopping_cart),
            title: new Text("市场"),
            vsync: this,
          ),
          new NavigationIconView(
            icon: new Icon(Icons.add_alert),
            title: new Text("通知"),
            vsync: this,
          ),
          new NavigationIconView(
            icon: new Icon(Icons.perm_identity),
            title: new Text("我的"),
            vsync: this,
          ),
    ];
    

    对比可以看出,第二种写法在调用时,代码可读性更好,推荐第二种写法.

    相关文章

      网友评论

          本文标题:Flutter构造方法两种写法

          本文链接:https://www.haomeiwen.com/subject/foqzfctx.html