xccjk / x-blog

学习笔记
18 stars 2 forks source link

Flutter #104

Open xccjk opened 8 months ago

xccjk commented 8 months ago

Flutter初始化项目,android gradle下载报错

报错内容

flutter/flutter_demo/android/settings.gradle' line: 22

* What went wrong:
Plugin [id: 'com.android.application', version: '7.3.0', apply: false] was not found in any of the following sources:

- Gradle Core Plugins (plugin is not in 'org.gradle' namespace)
- Included Builds (None of the included builds contain this plugin)

通过给Android Studio设置代理来解决

代理方式: https://www.360mb.net/30521.html

xccjk commented 8 months ago

Flutter 引入三方包

pubspec.yaml文件dependencies或者dev_dependencies中添加要安装的依赖包,vscode中保存就会自动安装依赖包。

安装依赖可能会出现pub.dev访问不了的情况,修改为国内镜像

// 打开flutter安装目录下http_host_validator.dart文件
flutter\packages\flutter_tools\lib\src\http_host_validator.dart

const String kPubDev = 'https://pub.dev/';
修改为
const String kPubDev = 'https://mirrors.tuna.tsinghua.edu.cn/dart-pub';
xccjk commented 8 months ago

Flutter 安装、更新、删除package包

// 安装
flutter pub add css_colors

// 删除
flutter pub remove css_colors

// 更新
flutter pub upgrade

// 安装指定版本
flutter pub add dio:4.0.6

// 安装dev_dependencies包
flutter pub add --dev package_name
xccjk commented 8 months ago

Flutter 设置包名、打包、环境等信息

https://blog.csdn.net/wsyx768/article/details/136319899

xccjk commented 8 months ago

Flutter 静态资源加载

// 本地图片
// pubspec.yaml

flutter:
  assets:
    - assets/images/logo.png
    - assets/images/launch_screen.png
    - assets/tabs/

// 使用本地图片
Image.asset('assets/images/logo.png', width: 80.w, height: 80.w)

// 网络图片
Image.network('https://example.com/image.jpg', width: 80.w, height: 80.w)
xccjk commented 8 months ago

dart 中map和asMap

在Dart中,map方法是用于将一个集合(例如列表)中的每个元素都映射到另一个元素,返回一个新的集合。由于map方法是对原集合的每个元素都执行一次操作,它不提供对当前元素索引的访问

// map
appBottomBar.map<BottomNavigationBarItem>((itemData) {
    return BottomNavigationBarItem(
        ...
    )
})

// asMap
appBottomBar.asMap().entries.map<BottomNavigationBarItem>((entry) {
    final index = entry.key;
    final itemData = entry.value;
    return BottomNavigationBarItem(
        ...
    )
})