Skip to content

Instantly share code, notes, and snippets.

@SongFuZhen
Last active October 26, 2020 09:51
Show Gist options
  • Save SongFuZhen/40b4a1041cb2948e37f320431697561b to your computer and use it in GitHub Desktop.
Save SongFuZhen/40b4a1041cb2948e37f320431697561b to your computer and use it in GitHub Desktop.
Google Flutter Leaning Flutter的一些配置

Execution failed for task ':app:transformDexArchiveWithExternalLibsDexMergerForDebug'.

Android 运行的时候会报错

解决方案

androidapp层的build.gradle里面添加如下内容:

android {
  defaultConfig {
    multiDexEnabled true
    ...
  }
}
dependencies {
  ...
  implementation 'com.android.support:multidex:1.0.3'
}

Execution failed for task ':app:transformClassesAndResourcesWithProguardForRelease

Android 在开发的时候没有报错,但是在打包的时候,报错如上

解决方案

proguard-rules.pro文件中添加代码

 -ignorewarnings 

添加完毕之后,再次运行即可正常打包

async & await 相关信息

Flutter的一些配置

1. Resolving dependencies... 很慢

解决方案

打开Flutter SDK:xx/flutter/packages/flutter_tools/gradle/flutter.gradle

修改内容如下

  buildscript {
    repositories {
        // google()
        // jcenter()

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

2.precache 很慢

Downloading android-arm64-profile/windows-x64 tools... 下载不下来

解决方案

修改环境变量如下

Tips: 注意linux系统和windows的不同配置方式;
Windows尽量配置在系统变量中, 然后将之添加到Path中,最好重启电脑

PUB_HOSTED_URL=https://pub.flutter-io.cn
FLUTTER_STORAGE_BASE_URL=https://storage.flutter-io.cn

3.android-licenses 长时间没反应

Downloading android-arm64-profile/windows-x64 tools... 下载不下来

解决方案

执行命令如下:

flutter doctor --android-licenses

在Container上添加事件

Container上包裹一层GestureDetector

GestureDetector(
  onTap:(){
    print('click');
  },
  child: Container(
    child: Text("文本")
  )
)

flutter 相关packages, 里面有很多优秀和常用的库

/// List of Awesome Flutter Packages
///
/// A curated list 📄 of awesome 🌟 Flutter packages. These are some of the most valuable gems 💎 of the Flutter community.
/// Is your favourite 😍 package missing? Let me know or create a pull request...
/// Please read the contribution guidelines before opening a pull request 👍

https://github.com/leisim/awesome-flutter-packages

一个UI框架

/// Flutter Screens
///
/// A collection of Login Screens, Buttons, Loaders and Widgets with attractive UIs, built with Flutter, ready to be used in your applications.

https://github.com/samarthagarwal/FlutterScreens
一个UI框架

UI设计, 一些通用界面的UI设计,可以参考参考

/// flutter-ui-nice

https://github.com/nb312/flutter-ui-nice

一些Flutter Demo的集合

/// Flutter-Notebook
///
/// lutetr_note_book有许多flutter相关功能demo的集合,它能够帮助您快速学习一些零碎的知识,本项目将会不定期更新

https://github.com/OpenFlutter/Flutter-Notebook

flutter Waiting for another flutter command to release the startup lock

Flutter 报错如上

解决方案

# windows 
taskkill /F /IM dart.exe

在mac os 下配置 flutter 环境变量

下载 flutter 到指定目录

# 从 github 下载
git clone https://github.com/flutter/flutter.git
pwd

设置到全局 profile 里面

使用管理员权限打开文件

sudo vim /etc/profile

添加以下内容到文件末尾

export PATH_TO_FLUTTER_GIT_DIRECTORY=/Volumes/Extreme\ SSD/Mac_Softwares/flutter
export PUB_HOSTED_URL=https://pub.flutter-io.cn #国内用户需要设置
export FLUTTER_STORAGE_BASE_URL=https://storage.flutter-io.cn #国内用户需要设置
export PATH=PATH_TO_FLUTTER_GIT_DIRECTORY/flutter/bin:$PATH

Tips: 内容里面如果有空格,需要加\转义;只能用 # 注释;=两边不能有空格;

使配置生效

source /etc/profile

Widget 父子传值

父组件传值给子组件

父组件调用子组件

  BarCode(fromWhere: "cc");

子组件接收父组件传递过来的值

class Barcode extends StatefulWidget {
  final String fromWhere;
  Barcode({Key key, this.fromWhere}) : super(key: key);

  @override
  _BarcodeState createState() => _BarcodeState();
}

子组件使用父组件传递过来的值

  String fromWhere = widget.fromWhere

TextField 怎么Remove Focus, 关闭软键盘

FocusScope.of(context).requestFocus(FocusNode());

Widget 之间传值

使用Event Bus进行消息的传递

定义EventBus

新建../common/EventBus.dart文件

import 'package:event_bus/event_bus.dart';

EventBus eventBus = new EventBus();

class BarcodeInEvent {
  List<String> barcodeLists = List<String>();
  BarcodeInEvent(this.barcodeLists);
}

发送消息

  import '../common/EventBus.dart'; // 要使用这种方式引入,不能用package
  ...
  
  eventBus.fire(new BarcodeInEvent(barCodeLists)); // 此处的eventBus是上面文件定义的全局变量
  

接收消息

  import '../common/EventBus.dart';
  ...
  
  // 在 initState 添加监听事件,监听特定的事件
  @override
  void initState() {
    super.initState();

    eventBus
        .on<BarcodeInEvent>()
        .listen((BarcodeInEvent data) => _setBarCode(data.barcodeLists));
  }
  
  void _setBarCode(list) {
    setState(() {
      barCodeLists = list;
    });
  }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment