美文网首页
Flutter开发中遇到问题解决汇总

Flutter开发中遇到问题解决汇总

作者: liuxingzi | 来源:发表于2020-11-13 16:25 被阅读0次

    1 更改StatusBar颜色

    其实很简单,在需要改变的页面中用AnnotatedRegion<SystemUiOverlayStyle>包一下 里面指定 SystemUiOverlayStyle.dark

     return AnnotatedRegion<SystemUiOverlayStyle>(
            value: SystemUiOverlayStyle.dark,
            child: Scaffold(
              body: SafeArea(child: pageBody),
            ));
    

    2 减少Widget嵌套

    使用 Dart 中的扩展函数方式,为Widget增加扩展

    extension WidgetExt on Widget {
      Container intoContainer({
        //复制Container构造函数的所有参数(除了child字段)
        Key key,
        AlignmentGeometry alignment,
        EdgeInsetsGeometry padding,
        Color color,
        Decoration decoration,
        Decoration foregroundDecoration,
        double width,
        double height,
        BoxConstraints constraints,
        EdgeInsetsGeometry margin,
        Matrix4 transform,
      }) {
        //调用Container的构造函数,并将当前widget对象作为child参数
        return Container(
          key: key,
          alignment: alignment,
          padding: padding,
          color: color,
          decoration: decoration,
          foregroundDecoration: foregroundDecoration,
          width: width,
          height: height,
          constraints: constraints,
          margin: margin,
          transform: transform,
          child: this,
        );
      }
    
      Padding intoPadding({Key key, EdgeInsetsGeometry padding}) {
        return Padding(key: key, padding: padding, child: this);
      }
    }
    

    使用处

    Column(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [Text('2'), Text('333')])
              .intoContainer(padding: EdgeInsets.only(top: 20.0, bottom: 20))
    

    就不用在外面再包一层 Container了

    遇到问题就更新吧

    相关文章

      网友评论

          本文标题:Flutter开发中遇到问题解决汇总

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