New features of Flutter2

Look at the past and think about the present

The release of Flutter 2 on March 4th. It has been more than two years since the release of Flutter 1.0.
In these two years, 24,541 issues have been resolved and 17,039 PRs from 765 contributors have been merged.
Since the release of Flutter 1.22 in September, 5807 issues have been resolved and 4091 PRs from 298 contributors have been merged.
The largest voluntary contributor to the Flutter 2 release is xu-baolin with 46 PRs,
a14n contributed 32 PRs to make Flutter achieve Null Safety, and
shamdikahloun has 20 PRs and improved many Flutter plugins.
It's not just coders who contribute to the Flutter project; a large number of volunteer PR reviewers are also responsible for reviewing 1525 PRs

To find out what's new in Flutter 2 itself, read on!

What is new

Web

Flutter web releases the stable version. In this initial stable version, Flutter takes code reusability to another level with the support of the web platform. Now when you create Flutter apps steadily, Web is just another platform for apps like IOS and Android.

By taking advantage of the many advantages of the web platform, Flutter lays the foundation for building rich and interactive web applications, focusing mainly on performance and rendering fidelity improvements. In addition to the HTML renderer, a new CanvasKit-based Renderer. Web-specific features have also been added, such as the Link widget , to ensure that your app running in the browser feels like a web app. For more details click here

Sound Null Safety

Sound Null Safety is an important addition to the Dart language that further enhances the type system by distinguishing between nullable and non-nullable. This enables developers to prevent null error crashes, which are a common cause of application crashes. By incorporating null checks into the type system, these errors can be caught during development, preventing production crashes. Starting with Flutter 2, the stable version including Dart 2.12 fully supports Sound Null Safety. More details here
The pub.dev package repository has published over 1,000 null safe packages, including hundreds from the Dart, Flutter, Firebase, and Material teams. If you are a package author, please review the migration guide and consider migrating now.

Desktop

Ready to try out Desktop as a Flutter app as a deployment target, think of it as a "beta snapshot" previewing the final stable release later this year.
To bring the Flutter desktop to this level of quality, big and small, improvements have been made, starting with making sure text editing feels like a native experience on every supported platform, including basic features like text selection pivot points, and the ability to edit text in Stop propagation after handling keyboard events. On the mouse input side, you can now start dragging with a high-precision pointing device immediately, rather than waiting for the delay required to process touch input. Additionally, built-in context menus have been added to TextField and TextFormField in the Material and Cupertino design languages. Finally, grab handles have been added to the ReorderableListView.

As a developer, ReorderableListView has always been good at moving items around, but it requires the user to long press to initiate the drag. This makes a lot of sense on mobile, but few desktop users would think of long-pressing on an item with the mouse to move it, so this release includes grab handles for mouse or touch input. Another improvement to platform idioms is updated scroll bars that display correctly on desktop form factors.

The Scrollbar has been updated to provide the interactivity expected on the desktop, including the ability to drag the thumb, click the track to page up and down, and show the track when the mouse hovers over any part of the scrollbar. Additionally, since scrollbars are themed using the new ScrollbarTheme class, you can style them to match the look and feel of your application.

For additional desktop-specific functionality, this release also enables command-line argument handling for Flutter apps, so simple actions like double-clicking a data file in Windows File Explorer can open files in your app. We've also worked hard to make resizing on Windows and macOS smoother, and enable IME (input method editor) for international users.

We've also provided updated documentation on what you need to do to start preparing your desktop app for deployment to OS-specific stores.

When trying out the Flutter desktop beta, you can access it by switching to the beta channel as expected and setting the configuration flags for the target platform according to the guidance on flutter.dev. A snapshot of the beta bits available in the stable channel is also provided. If you use "flutter config" to enable either of the desktop config settings (e.g. enable-macos-desktop), you can try beta features of desktop support without going through the long process of going to the beta channel and pulling down all of the Flutter SDK's Latest betas, build tools, etc. This is great for giving it a try or using the desktop support as a simple "Flutter Emulator".

However, if you choose to stay on the stable channel to access the desktop beta, you won't get new features or bug fixes as quickly as you would if you switched to the beta or developer channel. Therefore, if you are actively targeting Windows, macOS, or Linux, we recommend switching to a channel that provides updates more quickly.

Future things to come include support for integration with native top menus, text editing that feels more like a single platform experience, accessibility support, and general bug fixes and performance enhancements.

Platform Adaptive Apps: Flutter Folio Sample

Flutter now supports three platforms (Android, iOS, and Web) for production apps, and three beta versions (Windows, macOS, and Linux), which raises a natural question: how do you write an application that fits many different form factors? Apps of different sizes (small, medium and large screens), different input modes (touch, keyboard and mouse) and different idioms (mobile, web and desktop)? To answer this question for ourselves and for Flutter developers around the world, we commissioned the Flutter Folio scrapbook app.
You can click to watch https://youtu.be/x4xZkdlADWo

Folio is just a simple example, you want it to work well on multiple platforms from one code base. Then "well," means that it looks good on small, medium, and large screens, that it takes advantage of touch, keyboard, and mouse input, and that it fits well into the idioms of that platform, (for example, by using web link on , and the menu on the desktop). We call this kind of application "platform adaptive" because it adapts well to whatever platform it's running on.

If you want to learn how to make your own application platform adaptable, you can check out Folio's source code . In the future, expect to find documentation and codelabs that explore the topic in more depth. In the meantime, check out Aloïs Deniel's excellent blog post and video on the subject .

Google Mobile Ads to Beta

Public beta of Flutter's Google Mobile Ads SDK. This is a brand new plugin that offers inline banner and native ads in addition to the existing overlay formats (overlay banner, interstitial and rewarded video). The plugin unifies support for Ad Manager and Admob, so no matter what size publisher you are, you can tailor the plugin to suit you.

The plugin is available to you today, and as part of Flutter Engage, Andrew Brogdon and Zoey Fan did a session on "App Monetization with Flutter" (available on the Flutter Engage website) where they discussed apps built with Flutter monetization strategy, and how to load ads in your Flutter app. Additionally, we've created a new Ads page on flutter.dev where you'll find all helpful resources such as plugin implementation guides, inline banner and native ad code labs, as well as overlay banners, interstitials and Rewarded Video Ads Code Lab. Make sure to check it out!

New iOS features

In fact, this release brings 178 iOS-related PR merges, including 23495, which brings state restoration to iOS, 67781, which fulfills the longstanding requirement to build an IPA directly from the command line without opening Xcode, 69809, It updates the CocoaPods version to match the latest tools. Additionally, some iOS widgets have been added to the Cupertino design language implementation.

The new CupertinoSearchTextField provides the iOS search bar UI.

The CupertinoFormSection, CupertinoFormRow, and CupertinoTextFormFieldRow widgets make it easier to generate validated form fields using iOS's visual appearance.

In addition to the features available for iOS, we will continue to work on performance improvements for iOS and Flutter, both in shaders and animations. iOS remains the primary platform for Flutter, and we continue to work hard to bring important new features and improve performance.

New widgets: Autocomplete and ScaffoldMessenger

This release of Flutter comes with two additional new widgets, AutocompleteCore and ScaffoldMessenger. AutocompleteCore represents the minimum functionality required to incorporate autocomplete functionality into a Flutter application.

Autocompletion is a frequently requested feature in Flutter, so this release starts offering it. You can use it today, but if you're curious about the fully functional design, check out the autocomplete design docs .

Likewise, ScaffoldMessenger was created to handle many issues related to SnackBars, including the ability to easily create SnackBars in response to AppBar actions, the ability to create SnackBars to persist between Scaffold transitions, and the ability to switch between Scaffolds asynchronously even if the user has navigated to a page with other Scaffolds Show SnackBars when the operation is complete.

All these advantages can be used from now on to display SnackBars with a few lines of code:

final messenger = ScaffoldMessenger.of(context);
messenger.showSnackBar(SnackBar(content: Text(‘I can fly.)));

Multiple Flutter instances with Add-to-App

We've learned from talking to many Flutter developers that many of you don't have the luxury of launching a brand new app, but you can take advantage of Flutter by adding it to your existing iOS and Android apps The advantages. This feature is called "Add-to-App" and is an excellent way to reuse Flutter code on both mobile platforms while still preserving the existing native code base. However, for some of you, as we have heard sometimes, it is not clear how to integrate the first screen into Flutter. Interweaving Flutter and native screens makes navigation state hard to maintain, and integrating multiple Flutters at the view level can be memory intensive.

In the past, additional Flutter instances cost the same storage as the first instance. In Flutter 2, we reduced the static memory cost of creating additional Flutter engines by about 99%, to about 180kB per instance.

The new API supporting this feature can be previewed on the beta channel and documented on flutter.dev, along with a series of example projects demonstrating this new mode. With this change, we no longer hesitate to recommend creating multiple instances of the Flutter engine in native applications.

Flutter Fix

Whenever any framework matures and gathers users with more and more codebase, the tendency over time is to avoid making any changes to the framework API in order to avoid breaking more and more lines of code. Flutter 2 has over 500,000 Flutter developers working on an ever-increasing number of platforms, so it quickly falls into this category. However, in order for us to continue improving Flutter over time, we want to be able to make breaking changes to the API. The question is, how do you continue to improve the Flutter API without disrupting developers?

The answer is Flutter Fix.

Flutter Fix is ​​a combination of things. First, the dart CLI tool has a new command-line option called dart fix that knows where to look for a list of deprecated APIs and how to update your code with those APIs. Second, it's the list of available fixes themselves, bundled with version 2 of the Flutter SDK. Finally, it's an updated set of Flutter extensions for VS Code, IntelliJ, and Android Studio IDEs Know how to use the little lightbulb to expose the same list of available fixes as quick fixes, which will help you make code changes with the click of a mouse.
For example, suppose your application contains the following line of code

Creating a Flutter widget with deprecated parameters

Since the arguments to this constructor are deprecated, they should be replaced by the following:

Deprecated parameters replaced by

Even if you're familiar with all of Flutter's deprecations, the larger the number of changes you make in your code, the harder it is for you to apply all the fixes, and the easier it is to make mistakes. Humans are not very good at these kinds of repetitive tasks. But the computer is; you can see all the fixes we know how to make throughout the project by executing:

dart fix --dry-run

If you want to apply them in batches, you can easily do this:

dart fix --apply

Or, if you want to apply these patches interactively in your favorite IDE, you can do that too.

Although we've marked old APIs as deprecated for many years, we now have policies about when APIs that are actually deprecated are removed, and Flutter 2 is the first time we've done so. Even though we haven't captured all deprecated APIs as data to provide Flutter Fixes, we will continue to add more from previously deprecated APIs and will continue to do so in future breaking changes. Our goal is to do our best to make Flutter's API the best it can be, while also keeping your code up to date.

Flutter DevTools

To make it clear that DevTools is a tool for debugging Flutter applications, we renamed it to Flutter DevTools when debugging Flutter applications. Also, we've done a lot of work to bring it to Flutter 2 production quality.

A new feature that can help you troubleshoot problems even before you start DevTools is the ability of Android Studio, IntelliJ or Visual Studio Code to notify you when there are common exceptions and offer to bring them into DevTools to help you debug . For example, the following shows that an overflow exception was thrown in your application, which brings up an option in Visual Studio Code to debug the problem in DevTools.

Flutter IDE extension to notify when your app throws a layout overflow exception

Pressing that button takes you to the Flutter Inspector in DevTools on the offending widget so you can fix it. Today we only do this for layout overflow exceptions, but our plan is to provide this handling for all common exceptions that DevTools can resolve. Today we only do this for layout overflow exceptions, but our plan is to provide this handling for all common exceptions that DevTools can resolve.

Once DevTools is running, new error flags on the tabs help you track down specific issues in your application.

Red dots in DevTools help you focus on the part of the application where errors occur

Another new DevTools feature is the ability to easily view images with a higher resolution than the display, helping to track down excessive app size and memory usage. To enable this feature, enable "Invert Oversized Images" in the Flutter Inspector.

Enable the "Invert oversized images" option to highlight images that are larger than the ones that need to be enlarged.

Now, when you display an image with a resolution significantly larger than its display size, the image will be displayed upside down for easy findability in your app.

Also, by popular demand, in addition to showing details about flexible layouts in the Flutter Inspector's Layout Explorer, we've added the ability to show fixed layouts, allowing you to debug various layouts.

New Layout Explorer showing layout details for fixed and flexible layouts

That's not all. Here's just a rundown of some more new features in Flutter DevTools 2:

  • Added average FPS information and improved usability in Flutter frame graph
  • Bring up failed network requests in Network Profiler with red error labels
  • The new memory view charts are faster, smaller and easier to use, including new hovercards for depicting activity at a specific time.
  • Added search and filtering to the Logging tab
  • Track logs before starting DevTools, so full log history can be viewed on startup
  • Renamed the "Performance" view to "CPU Profiler" to make it clearer what features it offers
  • Added timing grid to CPU Profiler flame graph
  • Renamed the "Timeline" view to "Performance" for a clearer view of what it offers

Android Studio/IntelliJ Extension

The Flutter plugin for the IntelliJ family of IDEs has also gained many new features for Flutter 2. First, there is a new project wizard, which matches the new wizard style in IntelliJ.

Also, if you use IntelliJ or Android Studio on Linux to program the Flutter SDK installed from the Snap Store, the Flutter snapshot path has been added to the list of known SDK paths. This makes it easier for users of Flutter snapshots to configure the Flutter SDK in Settings.

Visual Studio Code Extension

The Flutter extension for Visual Studio Code also improves on Flutter 2, starting with a number of testing enhancements, including the ability to rerun only failed tests.

After two years of development, LSP (Language Server Protocol) support for Dart is now available as the default way to integrate Dart analyzers into the Flutter extension Visual Studio Code. LSP support brings many improvements to Flutter development, including the ability to apply all fixes of a certain type in the current Dart file and cause code completion to generate a full function call, including parentheses and required arguments.

And LSP support is not limited to Dart. It also supports code completion in pubspec.yaml and analysis_options.yaml files.

DartPad updated to support Flutter 2

The list of tool updates would be incomplete then, and DartPad has been updated to support Flutter 2.

Ecosystem updates

The development experience with Flutter includes not only the framework and tools, but also other content. It also includes various packages and plugins for Flutter applications. A lot has also happened in this area since the last stable release of Flutter. For example, between the camera and video_player plugins, nearly 30 PRs were merged together, greatly improving the quality of both. If you've had trouble using either of these in the past, you should switch up the look; we think you'll find them even more powerful.

Also, if you're a Firebase user, we're happy to announce that the most popular plugins are production-quality, excluding null-safe support, along with a full set of reference documentation and common usage tutorials for Android, iOS, Web, and macOS. These plugins include:

  • Core
  • Authentication
  • Cloud Firestore
  • Cloud Functions
  • Cloud Messaging
  • Cloud Storage
  • Crashlytics

Also, if you're looking for crash reporting for your app, you might want to consider Sentry, which has released a new SDK for Flutter apps.

Using Sentry's Flutter SDK, you can be notified in real time of errors that occur on Android, iOS, or native platforms. You can see details in the Sentry documentation.

Also, if you haven't seen the Flutter Community "plus" plugin, you need to check it out. They forked many popular plugins originally developed by the Flutter team, added null safety support for other platforms and a whole new set of documentation, and started fixing corresponding issues in the flutter/plugins repository. The plugin includes the following:

  • Android Alarm+
  • Android Intent+
  • Battery+
  • Connectivity+
  • Device Info+
  • Network Info+
  • Package Info+
  • Sensors+
  • Share+

At this point, the number of Flutter-compatible packages and plugin sets exceeds 15,000, making it difficult to find the ones you should consider first. Therefore, we publish release points (static analysis score), popularity, likes, and special titles for those packages marked as Flutter Favorite at particularly high quality. In time for Flutter 2, we've added several new packages to our favorites list:

  • animated_text_kit
  • bottom_navy_bar
  • chopper
  • font_awesome_flutter
  • flutter_local_notifications
  • just_audio
    Congratulations to the authors of these packages! If you haven't checked them out, or the rest of the packages on the list, you should.

Last but not least, for package authors or package users interested in whether the latest version of a package works with the latest version of Flutter, you will want to visit Codemagic's new pub.green website.

The pub.green website tests the compatibility of the Flutter and Dart packages available on pub.dev with different Flutter versions. Think of it as Flutter's "can I use". For more details, I recommend the CodeMagic team post an announcement blog.

Breaking Changes

The following breaking changes have been made to Flutter 2, many of which can be automatically mitigated using the dart fix command or quick-fixes in your IDE of choice:

  • 61366 Continue clipBehavior breaking changes
  • 66700 The default clipBehavior of FittedBox is None
  • 68905 Remove nullOk parameter from Cupertino color resolution API
  • 69808 Remove nullOk from Scaffold.of and ScaffoldMessenger.of, created for both
  • 68910 Remove nullOk parameter from Router.of and make it return a non-null value
  • 68911 Add maybeLocaleOf to localization
  • 68736 Remove nullOK in Media.queryOf
  • 68917 Remove nullOk parameter from Focus.of, FocusTraversalOrder.of and FocusTraversalGroup.of
  • 68921 Remove nullOk parameter from Shortcuts.of, Actions.find and Actions.handler
  • 68925 Remove nullOk parameter from AnimatedList.of and SliverAnimatedList.of
  • 69620 Remove deprecated method from BuildContext
  • 70726 Remove nullOk parameter from Navigator.of and add Navigator.maybeOft
  • 72017 Remove deprecated CupertinoTextThemeData.brightness
  • 72395. Remove deprecated [PointerEnterEvent, PointerExitEvent] from HoverEvent.
  • 72532 Remove deprecated showDialog.child
  • 72890 Remove deprecated Scaffold.resizeToAvoidBottomPadding
  • 72893 Remove deprecated WidgetsBinding. [deferFirstFrameReport, allowFirstFrameReport]
  • 72901 Remove deprecated StatefulElement.inheritFromElement
  • 72903 Remove deprecated Element method
  • 73604 Remove deprecated CupertinoDialog
  • 73745 Remove deprecated actionForegroundColor from Cupertino [Sliver] NavigationBar
  • 73746 Remove deprecated ButtonTheme.bar
  • 73747 Remove span deprecation
  • 73748 Remove deprecated RenderView.scheduleInitialFrame
  • 73749 Remove deprecated Layer.findAll
  • 75657 Remove residual nullOk parameter from Localizations.localeOf
  • 74680 Remove nullOk from Actions.invoke, add Actions.maybeInvoke

Guess you like

Origin blog.csdn.net/rd_w_csdn/article/details/114399911