Flutter 24: 易忽略的【小而巧】的技術(shù)點(diǎn)匯總 (三)

??????小菜繼續(xù)整理 Flutter 中日常用到的小知識點(diǎn)。

1. LinearGradient 漸變色

??????小菜為了給添加漸變色背景,需要用到 LinearGradient,小菜嘗試時(shí)發(fā)現(xiàn)如下注意。

  1. LinearGradient 中設(shè)置漸變色需要兩種顏色以上才會有效果;
  2. 設(shè)置多種顏色,若沒有其他設(shè)置,每種顏色所占比例均分為 1:1:...:1;
  3. 小菜嘗試如果設(shè)置所占比例,可以多設(shè)置幾個(gè)相同色值,只是這種方式不太合理;
  4. 可以通過設(shè)置 beginend 來調(diào)整漸變色的位置,小菜建議多多嘗試;
  5. TileMode 中包括三個(gè)狀態(tài):clamp 為默認(rèn)方式,自動延伸;repeated 重復(fù)效果,建議與 beginend 共同使用效果為佳;mirror 鏡面效果,即對稱效果。
  6. BoxDecoration 在使用過程時(shí)不可與背景色 color 同用。
Widget _decorationWid() {
  return ListView(
    children: <Widget>[
      _childChangeWid(
          '兩種顏色 均分',
          BoxDecoration(
              gradient: const LinearGradient(
                  colors: [Color(0xFFFFC125), Color(0xFFFF7F24)]))),
      _childChangeWid(
          '多種顏色 均分',
          BoxDecoration(
              gradient: const LinearGradient(colors: [
            Color(0xFFFFC125), Color(0xFFFF7F24), Color(0xFFFF4040)
          ]))),
      _childChangeWid(
          '兩種顏色 1:3',
          BoxDecoration(
              gradient: const LinearGradient(colors: [
            Color(0xFFFFC125), Color(0xFFFF7F24), Color(0xFFFF7F24), Color(0xFFFF7F24)
          ]))),
      _childChangeWid(
          '兩種顏色 垂直均分',
          BoxDecoration(
              gradient: const LinearGradient(
                  begin: Alignment.topRight,
                  colors: [Color(0xFFFFC125), Color(0xFFFF7F24)]))),
      _childChangeWid(
          '兩種顏色 前半部均分 延伸',
          BoxDecoration(
              gradient: const LinearGradient(
                  begin: Alignment(-1.0, 0.0), end: Alignment(0.0, 0.0), tileMode: TileMode.clamp,
                  colors: [Color(0xFFFFC125), Color(0xFFFF7F24)]))),
      _childChangeWid(
          '兩種顏色 均分 重復(fù)',
          BoxDecoration(
              gradient: const LinearGradient(
                  begin: Alignment(-1.0, 0.0), end: Alignment(0.0, 0.0), tileMode: TileMode.repeated,
                  colors: [Color(0xFFFFC125), Color(0xFFFF7F24)]))),
      _childChangeWid(
          '兩種顏色 均分 鏡面反射',
          BoxDecoration(
              gradient: const LinearGradient(
                  begin: Alignment(-1.0, 0.0), end: Alignment(0.0, 0.0), tileMode: TileMode.mirror,
                  colors: [Color(0xFFFFC125), Color(0xFFFF7F24)]))),
      _childChangeWid(
          '兩種顏色 設(shè)置起始位置與終止位置',
          BoxDecoration(
              gradient: const LinearGradient(
                  begin: Alignment.topLeft, end: Alignment(0.5, 0.0), tileMode: TileMode.repeated,
                  colors: [Color(0xFFFFC125), Color(0xFFFF7F24)])))]
  );
}

Widget _childChangeWid(var des, Decoration childDec) {
  return Container(
      height: 60.0, alignment: Alignment.center,
      child: Text(des, style: TextStyle(color: Colors.white, fontSize: 16.0)),
      margin: const EdgeInsets.fromLTRB(8.0, 3.0, 8.0, 3.0),
      decoration: childDec);
}

2. PreferdSize 尺寸 AppBar

??????Flutter 中默認(rèn)提供了 AppBar 給我們帶來了很多便利,但是有需要調(diào)整 AppBar 的高度,方式很簡單。在 AppBar 外嵌套一層 PreferdSize 即可設(shè)置 AppBar 的高度。

appBar: AppBar(
    title: Row(children: <Widget>[
  Text('標(biāo)題欄高度測試-默認(rèn)'),
  Expanded(flex: 1, child: Icon(Icons.add))
])),
appBar: PreferredSize(
    child: AppBar(
        title: Row(children: <Widget>[
      Text('標(biāo)題欄高度測試-80.0'),
      Expanded(flex: 1, child: Icon(Icons.add))
    ])),
    preferredSize: Size.fromHeight(80.0)),

3. SafeArea 是否沉浸式狀態(tài)欄

??????小菜以前處理過沉浸式狀態(tài)欄,效果很不錯(cuò),但是有時(shí)也需要保護(hù)狀態(tài)欄,此時(shí)需要 SafeArea 配合一下。通過調(diào)整 SafeAreatopbool 屬性來判斷是否保護(hù)狀態(tài)欄,bottom 屬性可以在有虛擬返回狀態(tài)欄的測試機(jī)中嘗試。

Widget _childTopImgWid() {
  return new SafeArea(
      top: false,
      child: Scaffold(
          body: Container(
              color: Colors.blue, child: Image.asset('images/icon_top_bg.png'))));
}
Widget _childTopImgWid() {
  return new SafeArea(
      top: true,
      child: Scaffold(
          body: Container(
              color: Colors.blue, child: Image.asset('images/icon_top_bg.png'))));
}

4. CheckBox 選擇框

??????小菜因業(yè)務(wù)方面需要使用 Checkbox 選擇框,簡單學(xué)習(xí)整理一下。

  1. 默認(rèn) Checkbox 只有【選中 true】和【未選中 false】兩種狀態(tài),選中顏色為主題色;
  2. 借助 activeColor 可以調(diào)整 Checkbox 選中顏色
  3. 借助 tristatetrue 可以有【選中 true】【未選中 false】【value null】三種狀態(tài);tristatefalse 只能有【選中 true】【未選中 false】兩種狀態(tài);
  4. materialTapTargetSize 為目標(biāo)與布局大小,默認(rèn)有 paddedshrinkWrap 兩種狀態(tài);小菜理解 padded 為谷歌設(shè)計(jì)建議尺寸 48px * 48px,shrinkWrap 為目標(biāo)尺寸縮小到谷歌設(shè)計(jì)提供的最小值,但在實(shí)際效果中差別不大。

??????Tips: CheckboxAndroid 略有不同,只有狀態(tài)框沒有文字內(nèi)容。

Checkbox(
    value: select,
    tristate: true,
    activeColor: Colors.pink,
    materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
    onChanged: (bool cb) {
      setState(() {
        select = cb;
      });
    })

5. BlendMode 圖片混合模式

??????小菜在學(xué)習(xí)圖片時(shí)發(fā)現(xiàn)一個(gè)有意思的屬性 colorBlendMode,在 Image 使用時(shí)配合 color 共同使用,可以營造很多特殊效果,包括色度色調(diào)等,屬性眾多,建議多多嘗試一下。

Widget _mixImgWid() {
  return Row(children: <Widget>[
    Image.asset('images/icon_launcher.png',
        color: Colors.redAccent,
        width: 80.0,
        colorBlendMode: BlendMode.modulate),
    Image.asset('images/icon_launcher.png',
        color: Colors.redAccent,
        width: 80.0,
        colorBlendMode: BlendMode.colorBurn),
    Image.asset('images/icon_launcher.png',
        color: Colors.redAccent,
        width: 80.0,
        colorBlendMode: BlendMode.colorDodge),
    Image.asset('images/icon_launcher.png',
        color: Colors.redAccent,
        width: 80.0,
        colorBlendMode: BlendMode.difference)
  ]);
}

??????如果有不對的地方還希望多多指出。

來源:阿策小和尚

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容