flutter零碎代码整理

整理一波零碎代码:状态栏隐藏/显示、强制屏幕旋转、google镜像、字符串比较、复制文本到剪切板、从剪切板读取文本、将APP设为主屏幕、数据类型判断、签名

状态栏隐藏/显示#

import 'package:flutter/material.dart';

//隐藏
SystemChrome.setEnabledSystemUIOverlays([]);
//恢复
SystemChrome.setEnabledSystemUIOverlays(SystemUiOverlay.values);

强制屏幕旋转#

使用插件:orientation 访问插件

OrientationPlugin.forceOrientation(DeviceOrientation.landscapeLeft);

安卓:

//进入页面横屏
@override
void initState() {
  super.initState();
  SystemChrome.setPreferredOrientations([
    DeviceOrientation.landscapeRight,
  ]);
}

//退出页面竖屏
@override
void dispose() {
  SystemChrome.setPreferredOrientations([
    DeviceOrientation.portraitUp,
  ]);
  super.dispose();
}

iOS:存在问题,代码无效


google镜像#

flutter 项目内配置代理:
(1)项目名\android\build.gradle

maven{ url 'http://maven.aliyun.com/nexus/content/groups/public/'}
maven{ url 'https://maven.aliyun.com/repository/google' }
maven{ url 'https://maven.aliyun.com/repository/jcenter' }

(2)SDK 名\flutter\packages\flutter_tools\gradle

buildscript {
  repositories {
  //google()
  //jcenter()
      maven{ url 'http://maven.aliyun.com/nexus/content/groups/public/'}
      maven{ url 'https://maven.aliyun.com/repository/google' }
  }
  dependencies {
      classpath 'com.android.tools.build:gradle:3.2.1'
  }
}

字符串比较#

繁冗

class CompareStr{
  // final String str1,str2;
  // CompareStr({this.str1,this.str2});

  int compare(String str1, String str2){
    final minCount = min(str1.length, str2.length);
    for (var i = 0; i < minCount; i++) {
      final l1 = str1.codeUnitAt(i);
      final l2 = str2.codeUnitAt(i);
      if(l1>l2){
        return 1;
      }else if(l1<l2){
        return -1;
      }else{
        continue;
      }
    }
    if(str1.length>str2.length){
      return 1;
    }else if(str1.length<str2.length){
      return -1;
    }else{
      return 0;
    }
  }
}

简洁

int compare(String str1, String str2){
    return Comparable.compare(str1, str2);
}

复制文本到剪切板、从剪切板读取文本#

导入包

import 'package:flutter/services.dart';

复制文本到剪切板

Clipboard.setData(ClipboardData(text: '复制到剪切板'));

从剪切板读取文本

var text = Clipboard.getData(Clipboard.kTextPlain);

将APP设为主屏幕#

安卓

项目名\android\app\src\main\AndroidManifest.xml

<activity
         android:windowSoftInputMode="stateHidden"
         android:launchMode="singleTop"
         android:name=".main"
         android:label="YOUR APP NAME"
         android:screenOrientation="portrait">
         <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
         </intent-filter>
         <intent-filter >
          <action android:name="android.intent.action.MAIN" />
          <category android:name="android.intent.category.HOME" />
          <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
</activity>

iOS

不支持


数据类型判断#

if (val is String) {
    print("The value is a String, but I needed "
        "to check with an explicit condition.");
  }
import 'dart:mirrors'; 
 
getTypeName(dynamic obj) {
  return reflect(obj).type.reflectedType.toString();
}
 
void main() {
  var val = "\"Dart is dynamically typed (with optional type annotations.)\"";
  if (val is String) {
    print("The value is a String, but I needed "
        "to check with an explicit condition.");
  }
  var typeName = getTypeName(val);
  print("\nThe mirrored type of the value is $typeName.");
}

签名#

MAC

jarsigner -verbose -keystore /Users/user/Documents/root/app/android/app/key.jks -signedjar app-signed.apk app-release_legu.apk key

Windows

jarsigner -verbose -keystore D:/nodejs/wwwroot/lift_app/android/app/key.jks -signedjar app-signed.apk app-release_legu.apk key