如何用Dart格式化日期?

您可以使用intl package来格式化日期。

对于en_US格式,这是相当简单:

import 'package:intl/intl.dart'; 

main() { 
    var now = new DateTime.now(); 
    var formatter = new DateFormat('yyyy-MM-dd'); 
    String formatted = formatter.format(now); 
    print(formatted); // something like 2013-04-20 
} 

有格式化多种选择。从文档:

ICU Name     Skeleton 
--------     -------- 
DAY       d 
ABBR_WEEKDAY     E 
WEEKDAY      EEEE 
ABBR_STANDALONE_MONTH  LLL 
STANDALONE_MONTH    LLLL 
NUM_MONTH     M 
NUM_MONTH_DAY    Md 
NUM_MONTH_WEEKDAY_DAY  MEd 
ABBR_MONTH     MMM 
ABBR_MONTH_DAY    MMMd 
ABBR_MONTH_WEEKDAY_DAY  MMMEd 
MONTH      MMMM 
MONTH_DAY     MMMMd 
MONTH_WEEKDAY_DAY   MMMMEEEEd 
ABBR_QUARTER     QQQ 
QUARTER      QQQQ 
YEAR       y 
YEAR_NUM_MONTH    yM 
YEAR_NUM_MONTH_DAY   yMd 
YEAR_NUM_MONTH_WEEKDAY_DAY yMEd 
YEAR_ABBR_MONTH    yMMM 
YEAR_ABBR_MONTH_DAY   yMMMd 
YEAR_ABBR_MONTH_WEEKDAY_DAY yMMMEd 
YEAR_MONTH     yMMMM 
YEAR_MONTH_DAY    yMMMMd 
YEAR_MONTH_WEEKDAY_DAY  yMMMMEEEEd 
YEAR_ABBR_QUARTER   yQQQ 
YEAR_QUARTER     yQQQQ 
HOUR24      H 
HOUR24_MINUTE    Hm 
HOUR24_MINUTE_SECOND   Hms 
HOUR       j 
HOUR_MINUTE     jm 
HOUR_MINUTE_SECOND   jms 
HOUR_MINUTE_GENERIC_TZ  jmv 
HOUR_MINUTE_TZ    jmz 
HOUR_GENERIC_TZ    jv 
HOUR_TZ      jz 
MINUTE      m 
MINUTE_SECOND    ms 
SECOND      s 

对于非en_US日期,您需要显式加载区域设置。有关更多信息,请参阅https://www.dartdocs.org/documentation/intl/latest/intl/DateFormat-class.html

Could not build the application for the simulator.

点击三角形按钮,突然运行错误:

Could not build the application for the simulator.

Error launching application on iPhone 11 Pro Max.

错误解析:

在项目的终端flutter clean进行一下,重启,打开运行项目;

如果还不行,说明你新添加的功能代码是有问题的,以至于产生冲突,你可以先暂时注释掉最近写的权限功能代码,再细看文档吧。

比如我的 就是应该使用高德获取位置的过程中,iOS端没有处理好!

以上是我的个人经验,可能不能100%解决。
————————————————
版权声明:本文为CSDN博主「PrintfYoli」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/cxy18137478587/article/details/114284567

flutter 页面返回时刷新页面

// —— 跳转页面时 ——-Navigator.of(context).push(
MaterialPageRoute(
builder: (BuildContext context) {
// 要跳转的页面
return DiaryItem(
id: DiaryList.fromJson(model.list[index]).id,
);
},
),).then((data) => {
// 判断是否刷新
if (data == ‘refresh’)
// 刷新操作
model.refresh()});
// —— 跳转的页面 ——-
Navigator.of(context).pop(“refresh”);

Flutter 踩坑 macos项目无法运行

error: unable to parse contents of file list '/Users/用户名/Desktop/Code/Flutter/tyty_flutter/macos/Flutter/ephemeral/FlutterInputs.xcfilelist'

error: unable to parse contents of file list '/Users/用户名/Desktop/Code/Flutter/tyty_flutter/macos/Flutter/ephemeral/FlutterOutputs.xcfilelist'

遇到这种错误不要慌,先用安卓studio或者VS Code跑一下 然后再run 你的工程就可以啦!

Navigator operation requested with a context that does not include a Navigator.
The context used to push or pop routes from the Navigator must be that of a widget that is a descendant of a Navigator widget.

遇到这种push报错,不要慌,慌也没用,提示错误是上下文类型不对,看看你的runApp函数写法和官方写法有什么不同吧(暂时总结为:要使用Navigator,根元素不能是MaterialApp)

1、Flutter Column 套 ListView 不显示

将 ListView 放到 Column 内部后无法显示 ListView 内容。遇到这种问题 不要慌 慌也没用
将 ListView 用 Expand 包裹起来。完事儿!

Flutter Android端启动白屏问题的解决

问题描述

Flutter 应用在 Android 端上启动时会有一段很明显的白屏现象,白屏的时长由设备的性能决定,设备性能越差,白屏时间越长。

问题分析

其实启动白屏的问题在Android原生应用上也是一个常见问题,大致是因为从用户点击 Launcher Icon 到应用首页显示之间,Android 系统在完成应用的初始化工作,其流程如下:

在 Flutter Android 端上,白屏的问题会更加严重,因为除了 Android 应用启动耗时外,还增加了 Flutter 初始化耗时。

直到 Flutter 渲染出第一帧内容,用户才能感知到App启动完成。

解决方案

解决方案很简单,Android原生的白屏问题可以通过为 Launcher Activity 设置 windowBackground 解决,而 Flutter 也是基于此办法,同时优化了 Flutter 初始化阶段的白屏问题(覆盖一个launchView),只用两步设置便能解决 Flutter 中白屏问题。

在项目的 android/app/src/main/res/mipmap-xhdpi/ 目录下添加闪屏图片;

打开 android/app/src/main/res/drawable/launch_background.xml 文件,这个文件就是闪屏的背景文件,具体如何设置可以查阅 Android Drawable,我在 demo 中的设置如下:

<!– Modify this file to customize your launch splash screen —

<!– You can insert your own image assets here —
<item
android:bottom=”84dp”
<bitmap
android:src=”@mipmap/launch_image” /
</item
</layer-list

效果对比如下:

白屏

白屏优化

以上就是本文的全部内容,希望对大家的学习有所帮助。

Desktop support for Flutter

Desktop support allows you to compile Flutter source code to a native Windows, macOS, or Linux desktop app. Flutter’s desktop support also extends to plugins—you can install existing plugins that support the macOS or Linux platforms, or you can create your own.

Requirements

To create a Flutter app with desktop support, you need the following software:

Additional Windows requirements

For Windows desktop development, you need the following in addition to the Flutter SDK:

  • Visual Studio 2019 (not to be confused with Visual Studio Code) with the “Desktop development with C++” workload installed, including all of its default components

Additional macOS requirements

For macOS desktop development, you need the following in addition to the Flutter SDK:

Additional Linux requirements

For Linux desktop development, you need the following in addition to the Flutter SDK:

The easiest way to install the Flutter SDK along with these dependencies is by using snapd. For more information, see Installing snapd.

Once you have snapd, you can install Flutter using the Snap Store, or at the command line:

 sudo snap install flutter --classic

If snapd is unavailable on the Linux distro you’re using, you might use the following command:

 sudo apt-get install clang cmake ninja-build pkg-config libgtk-3-dev libblkid-dev liblzma-dev

Create a new project

You can use the following steps to create a new project with desktop support.

Set up

At the command line, perform the following commands to make sure that you have the latest desktop support and that it’s enabled. If you see “flutter: command not found”, then make sure that you have installed the Flutter SDK and that it’s in your path.

 flutter channel dev
 flutter upgrade
 flutter config --enable-<platform>-desktop

Where <platform> is windows, macos, or linux:

 flutter config --enable-windows-desktop
 flutter config --enable-macos-desktop
 flutter config --enable-linux-desktop

To ensure that desktop is installed, list the devices available. You should see something like the following (you’ll see Windows, macOS, or Linux, depending on which platforms you’ve enabled):

 flutter devices
1 connected device:

Windows (desktop) • windows • windows-x64 • Microsoft Windows [Version 10.0.18362.1082]
macOS (desktop) • macos • darwin-x64 • Mac OS X 10.15.5 19F101
Linux (desktop) • linux • linux-x64 • Linux

You might also run flutter doctor to see if there are any unresolved issues. It should look something like the following on Windows:

[✓] Flutter (Channel master, 1.22.0-10.0.pre.196, on Microsoft Windows [Version 10.0.18362.1082], locale en-US)
[✓] Visual Studio - develop for Windows (Visual Studio Professional 2019 16.6.2)
[✓] VS Code (version 1.48.2)
[✓] Connected device (1 available)

On macOS, you might see something like the following:

[✓] Flutter (Channel master, 1.18.0-10.0.pre, on Mac OS X 10.15.4 19E287, locale
    en-US)
[✓] Xcode - develop for iOS and macOS (Xcode 11.2)
[✓] Chrome - develop for the web
[✓] VS Code (version 1.44.2)
[✓] Connected device (3 available)

On Linux, you might see something like the following:

 flutter doctor
[✓] Flutter (Channel master, 1.20.0-1.0.pre.132, on Linux, locale en_US.UTF-8)
[✓] Linux toolchain - develop for Linux desktop
[✓] VS Code (version 1.33.1)
[✓] Connected device (1 available)

If flutter doctor finds problems for a platform you don’t support, you can ignore those warnings. You don’t have to install Android Studio and the Android SDK, for example, if you’re writing a Linux desktop app.

After enabling desktop support, restart your IDE. You should now see windows (desktop), macOS (desktop), or linux (desktop) in the device pulldown.

Create and run

Creating a new project with desktop support is no different than creating a new Flutter project for other platforms.

Once you’ve configured your environment for desktop support, you can create and run a desktop app either in the IDE or from the command line.

IDE

After you’ve configured your environment to support desktop, make sure you restart the IDE if it was already running.

Create a new app in your IDE and it automatically creates iOS, Android, and desktop versions of your app. (And web, too, if you’ve enabled web support.) From the device pulldown, select windows (desktop), macOS (desktop), or linux (desktop) and run your app to see it launch on the desktop.

Command line

To create a new app that includes desktop support (in addition to mobile support), run the following commands, substituting myapp with the name of your project:

 flutter create myapp
 cd myapp

To launch your app from the command line, enter one of the following commands from the top of the package:

 flutter run -d windows
 flutter run -d macos
 flutter run -d linux

Build a release app

To generate a release build run one of the following commands:

 flutter build windows
 flutter build macos
 flutter build linux

Distribution

In general, we don’t recommend releasing a desktop app until desktop support is stable. There are not yet full instructions, or tooling support, for making distributable applications. However, here is some information about how to use the current build output on other machines for testing purposes.

Windows

The executable can be found in your project under build\windows\runner\<build mode>\. In addition to that executable, you need the following:

  • From the same directory:
    • all the .dll files
    • the data directory
  • The Visual C++ redistributables. You can use any of the methods shown in the deployment example walkthroughs on the Microsoft site. If you use the application-local option, you need to copy:
    • msvcp140.dll
    • vcruntime140.dll
    • vcruntime140_1.dll

Place the DLL files in a directory next to the executable and the other DLLs, and bundle them together in a zip file.

macOS

The .app is self-contained, and can be distributed as-is.

Linux

For information on publishing a Linux app to the Snap Store, see Build and release a Linux desktop app.

As the tooling solidifies, stay tuned for updates on other ways to distribute a Linux desktop app.

Add desktop support to an existing Flutter app

To add desktop support to an existing Flutter project, run the following command in a terminal from the root project directory:

 flutter create .

This adds the necessary desktop files and directories to your existing Flutter project.

macOS-specific support

The following information applies only to macOS development.

Entitlements and the App Sandbox

macOS builds are configured by default to be signed, and sandboxed with App Sandbox. This means that if you want to confer specific capabilities or services on your macOS app, such as the following:

  • Accessing the internet
  • Capturing movies and images from the built-in camera
  • Accessing files

Then you must set up specific entitlements in Xcode. The following section tells you how to do this.

Setting up entitlements

Managing sandbox settings is done in the macos/Runner/*.entitlements files. When editing these files, you shouldn’t remove the original Runner-DebugProfile.entitlements exceptions (that support incoming network connections and JIT), as they’re necessary for the debug and profile modes to function correctly.

If you’re used to managing entitlement files through the Xcode capabilities UI, be aware that the capabilities editor updates only one of the two files or, in some cases, it creates a whole new entitlements file and switches the project to use it for all configurations. Either scenario causes issues. We recommend that you edit the files directly. Unless you have a very specific reason, you should always make identical changes to both files.

If you keep the App Sandbox enabled (which is required if you plan to distribute your app in the App Store), you need to manage entitlements for your application when you add certain plugins or other native functionality. For instance, using the file_chooser plugin requires adding either the com.apple.security.files.user-selected.read-only or com.apple.security.files.user-selected.read-write entitlement. Another common entitlement is com.apple.security.network.client, which you must add if you make any network requests.

Without the com.apple.security.network.client entitlement, for example, network requests will fail with a message such as:

flutter: SocketException: Connection failed
(OS Error: Operation not permitted, errno = 1),
address = example.com, port = 443

For more information on these topics, see App Sandbox and Entitlements on the Apple Developer site.

Hardened runtime

If you choose to distribute your application outside of the App Store, you need to notarize your application for compatibility with macOS 10.15+. This requires enabling the Hardened Runtime option. Once you have enabled it, you need a valid signing certificate in order to build.

By default, the entitlements file allows JIT for debug builds but, as with App Sandbox, you may need to manage other entitlements. If you have both App Sandbox and Hardened Runtime enabled, you may need to add multiple entitlements for the same resource. For instance, microphone access would require both com.apple.security.device.audio-input (for Hardened Runtime) and com.apple.security.device.microphone (for App Sandbox).

For more information on this topic, see Hardened Runtime on the Apple Developer site.

Plugin support

Flutter on the desktop supports using and creating plugins.

Using a plugin

To use a plugin that supports desktop, follow the steps for plugins in using packages. Flutter automatically adds the necessary native code to your project, as with iOS or Android.

We recommend the following plugins, which have been updated to work for desktop apps:

Use the following links to find all packages on pub.dev that support desktop apps. These links lists all packages, not just plugin packages. (Remember that plugin packages, or plugins, provide an interface to platform-specific services.)

Writing a plugin

When you start building your own plugins, you’ll want to keep federation in mind. Federation is the ability to define several different packages, each targeted at a different set of platforms, brought together into a single plugin for ease of use by developers. For example, the Windows implementation of the url_launcher is really url_launcher_windows, but a Flutter developer can simply add the url_launcher package to their pubspec.yaml as a dependency and the build process pulls in the correct implementation based on the target platform. Federation is handy because different teams with different expertise can build plugin implementations for different platforms. You can add a new platform implementation to any endorsed federated plugin on pub.dev, so long as you coordinate this effort with the original plugin author.

For more information, including information about endorsed plugins, see the following resources:

Samples and codelabs

Write a Flutter desktop application
A codelab that walks you through building a desktop app that integrates the GitHub GraphQL API with your Flutter app.

You can run the following samples as desktop apps, as well as download and inspect the source code to learn more about Flutter desktop support.

Flutter Gallery running web app, repo
A samples project hosted on GitHub to help developers evaluate and use Flutter. The Gallery consists of a collection of Material design widgets, behaviors, and vignettes implemented with Flutter. You can clone the project and run Gallery as a desktop app by following the instructions provided in the README.
Photo Search app
A sample app built as a desktop application that uses the following desktop-supported plugins:

Flutter 自动调整文本大小 auto_size_text

作用:可自动调整文本大小以使其完全适合其边界
在这里插入图片描述

1.在 pubspec.yaml 中添加 auto_size_text 包扩展
2.用法:

AutoSizeText(
  'The text to display',
  style: TextStyle(fontSize: 20),
  maxLines: 2,
)
  • 1
  • 2
  • 3
  • 4
  • 5

AutoSizeText 行为完全像 Text。唯一的区别是它将调整文本的大小以适合其范围,且需要限制约束来调整文本大小

参数 描述
key* 控制一个小部件如何替换树中的另一个小部件
textKey 设置结果Text小部件的键
style* 如果不为空,则用于此文本的样式
minFontSize 自动调整文本大小时使用的最小文本大小约束。如果presetFontSizes被设置则被忽略
maxFontSize 自动调整文本大小时使用的最大文本大小限制。如果presetFontSizes被设置则被忽略
stepGranularity 调整字体大小以适应约束的步长
presetFontSizes 预定义所有可能的字体大小。重要提示: presetFontSizes必须按降序排列
group 同步多个AutoSizeTexts 的大小
textAlign* 文本应如何水平对齐
textDirection* 文本的方向性。这决定了textAlign像TextAlign.start和值TextAlign.end的解释方式
locale* 当可以根据区域设置以不同方式呈现相同的Unicode字符时,用于选择字体
softWrap* 文本是否应在换行符处中断
wrapWords 是否应该换行不适合的单词。默认为true表现得像Text
overflow* 视觉溢出应如何处理
overflowReplacement 如果文本溢出且不适合其边界,则会显示此小部件
textScaleFactor* 每个逻辑像素的字体像素数。也影响minFontSize,maxFontSize和presetFontSizes
maxLines 文本可以跨越的可选最大行数
semanticsLabel* 此文本的替代语义标签

详情可参照顶部文档,写的很详细

flutter 枚举&类型

枚举

  1. 枚举是一种有穷序列集的数据类型
  2. 使用关键字enum定义一个枚举
  3. 枚举常用于代替常量,控制语句等
void testDart(){
  var currentSeason = Season.summer;
  switch (currentSeason) {
    case Season.spring:
      print('1-3月');
      break;
    case Season.summer:
      print('4-6月');
      break;
    case Season.autumn:
      print('7-9月');
      break;
    case Season.winter:
      print('10-12月');
      break;
  }
}
enum Season {
  spring,
  summer,
  autumn,
  winter
}

定义常量需要四个,而用枚举一个就行(Javascript中对象常量的属性是可以修改的)

Dart枚举特性

  1. index从0开始,一次累加
  2. 不能指定原始值
  3. 不能添加方法
var currentSeason = Season.summer;
  print(currentSeason.index);
错误
enum Season = {
    spring = 10; //错误
}

泛型

  1. Dart中的类型是可选的(dynamic),可使用泛型限定类型
  2. 使用泛型能够有效的减少代码重复
void testDart(){
  var list = new List();
  list.add(1);
  list.add('1');

  var list1 = new List<String>();
  list1.add('1');
}

泛型的使用

  1. 类的泛型
  2. 方法的泛型
    类上使用泛型
void testDart(){
  var utils = new Utils<String>();
  utils.put('xxxx');
  var utils1 = new Utils<int>();
  utils1.put(11);

}
class Utils<T>{
  T element;
  String elementStr;
  void put(T element){
    this.element = element;
  }
}

方法使用泛型

void testDart(){
  var utils = new Utils();
  utils.put<String>('11');
}
class Utils{
  String elementStr;
  void put<T>(T element){
    print(element);
  }
}