美文网首页
Flutter小知识点拾遗《一》

Flutter小知识点拾遗《一》

作者: 玉思盈蝶 | 来源:发表于2020-07-16 13:52 被阅读0次

1、加载文本assets

import 'dart:async' show Future;
import 'package:flutter/services.dart' show rootBundle;
 
Future<String> loadAsset() async {
  return await rootBundle.loadString('assets/config.json');
}

2、Visibility

Offstage是控制组件隐藏/可见的组件,如果感觉有些单调功能不全,我们可以使用Visibility,Visibility也是控制子组件隐藏/可见的组件。不同是的Visibility有隐藏状态是否留有空间、隐藏状态下是否可调用等功能。

Visibility(
  visible: !isDetail,
  child: Column(
    children: <Widget>[
      new Container(
        margin: EdgeInsets.only(
            left: 15,
            right: 15,
            bottom: 10,
            top: weiboItem.containZf ? 0 : 12),
        height: 1,
        color: Color(0xffDBDBDB),
      ), //下划线
      _RePraCom(context, weiboItem),
      new Container(
        margin: EdgeInsets.only(top: 10),
        height: 12,
        color: Color(0xffEFEFEF),
      ),
    ],
  ),
) //下划线

3、判断参数空

return Container(
    child: Container(
        margin: EdgeInsets.only(left: 15, right: 15),
        child: (vedioUrl.isEmpty || "null" == vedioUrl)
            ? new Container()
            : Container(
                constraints: BoxConstraints(
                    maxHeight: 250,
                    maxWidth: MediaQuery.of(context).size.width,
                    //    maxWidth: 200,
                    minHeight: 150,
                    minWidth: 150),
                child: VideoWidget(
                  vedioUrl,
                ))),
  );

4、Flexible

Flexible,Expanded,Spacer这三个小控件用于Row, Column, or Flex这三个容器;

Spacer

顾名思义只是一个间距控件,可以用于调节小部件之间的间距,它有一个flex可以进行设置;

Expanded

Expanded会尽可能的充满分布在Row, Column, or Flex的主轴方向上;

Flexible

Flexible也是为小部件提供空间的,但是不会要求子空间填满可用空间;

5、SafeArea控件

在不同的平台上,有很多特殊的位置,比如 Android 系统的状态栏,或者 iPhone X 的“齐刘海”,我们应该避免在这些位置放置元素。

解决方案就是使用 SafeArea 部件.

6、Align

Align的布局行为分为两种情况:

当widthFactor和heightFactor为null的时候,当其有限制条件的时候,Align会根据限制条件尽量的扩展自己的尺寸,当没有限制条件的时候,会调整到child的尺寸;
当widthFactor或者heightFactor不为null的时候,Aligin会根据factor属性,扩展自己的尺寸,例如设置widthFactor为2.0的时候,那么,Align的宽度将会是child的两倍。

/// The top left corner.
static const Alignment topLeft = const Alignment(-1.0, -1.0);

Alignment实际上是包含了两个属性的,其中第一个参数,-1.0是左边对齐,1.0是右边对齐,第二个参数,-1.0是顶部对齐,1.0是底部对齐。根据这个规则,我们也可以自定义我们需要的对齐方式。

7、SliverToBoxAdapter

用作在slivers中包裹需要放在滚动列表中的元素。

SliverToBoxAdapter(
    child: Container(
      height: 100,
      child: Text("Grid列表"),
    ),
  )

8、函数回调返回值

Future<bool> onLikeButtonTapped(bool isLiked, WeiBoModel weiboItem) async {
    final Completer<bool> completer = new Completer<bool>();
    completer.complete(false);
    return completer.future;
}

9、SizedBox

能强制子控件具有特定宽度、高度或两者都有,使子控件设置的宽高失效。

body:SizedBox(
    width: 100.0,
    height: 100.0,
    child:  Container(
      width: 200.0,
      height: 200.0,
      color: Color(0xffff0000),
    ),
)

10、PreferredSize

使用脚手架Scaffold可以设置AppBar,想要设置高度,在AppBar外包一层PreferredSize,设置preferredSize的属性为想要的高度即可。

PreferredSize(
  child: AppBar(
    backgroundColor: Color(0xffffffff),
    leading: IconButton(
        iconSize: 30,
        icon: Image.asset(
          Constant.ASSETS_IMG + 'icon_back.png',
          width: 23.0,
          height: 23.0,
        ),
        onPressed: () {
          Navigator.pop(context);
        }),
    title: Text(
      '联系人',
      style: TextStyle(fontSize: 16, color: Colors.black),
    ),
    elevation: 0,
    centerTitle: true,
  ),
  preferredSize: Size.fromHeight(50)
)

11、WillPopScope

iOS 下 WillPopScope 可以用于禁止测滑返回;
Android 返回按钮点击两次退出应用。

Widget build(BuildContext context) {
    return SafeArea(
      child: WillPopScope(
        child: Scaffold(
          resizeToAvoidBottomInset: false,
          backgroundColor: Colors.white,
          body: Column(
            mainAxisSize: MainAxisSize.max,
            children: <Widget>[
              _retweettitle(),
              _retweettosay(),
              _retweetcontent(),
              new Expanded(
                  child: Container(
                alignment: Alignment.bottomCenter,
                child: buildBottom(),
              ))
            ],
          ),
        ),
        onWillPop: () {
          print("点击返回键");
          if (mBottomLayoutShow) {
            setState(() {
              mBottomLayoutShow = false;
              mEmojiLayoutShow = false;
            });
          } else {
            Navigator.pop(context);
          }
        },
      ),
    );
  }

又是一堆需求来了,不能好好学习了呀~~~

相关文章

网友评论

      本文标题:Flutter小知识点拾遗《一》

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