- Install
- Set up an editor
- Test drive
- Write your first Flutter app, part 1
- Learn more
- Flutter for Android developers
- Flutter for iOS developers
- Flutter for React Native developers
- Flutter for web developers
- Flutter for Xamarin.Forms developers
- Introduction to declarative UI
- Cookbook
- Codelabs
- Tutorials
- User interface
- Introduction to widgets
- Layouts in Flutter
- Layout tutorial
- Dealing with box constraints
- Adding interactivity to your Flutter app
- Adding assets and images
- Navigation & routing
- Introduction to animations
- Animations overview
- Animations tutorial
- Hero Animations
- Staggered Animations
- Advanced UI
- Slivers
- Taps, drags, and other gestures
- Widget catalog
- Data & backend
- State management
- State management
- Start thinking declaratively
- Differentiate between ephemeral state and app state
- Simple app state management
- List of state management approaches
- JSON and serialization
- Firebase
- Accessibility & internationalization
- Accessibility
- Internationalizing Flutter apps
- Platform integration
- Writing custom platform-specific code
- Packages & plugins
- Using packages
- Developing packages & plugins
- Background processes
- Tools & techniques
- Android Studio / IntelliJ
- Visual Studio Code
- Upgrading Flutter
- Hot reload
- Code formatting
- Debugging Flutter apps
- Using OEM debuggers
- Flutter's build modes
- Testing Flutter apps
- Performance best practices
- Flutter performance profiling
- Creating flavors for Flutter
- Preparing an Android App for Release
- Preparing an iOS App for Release
- Continuous Delivery using fastlane with Flutter
- Bootstrap into Dart
- Inside Flutter
- Platform specific behaviors and adaptations
- Technical Overview
- Technical videos
- FAQ
- Flutter widget index
- Install
- Windows install
- MacOS install
- Linux install
- Set up an editor
- Write your first Flutter app, part 1
- Learn more
- Cupertino (iOS-style) widgets
- Layout widgets
- Animation and motion widgets
- Retrieve the value of a text field
- Basic widgets
- Material Components widgets
- Animate the properties of a Container
- Fade a Widget in and out
- Add a Drawer to a screen
- Displaying SnackBars
- Exporting fonts from a package
- Updating the UI based on orientation
- Using Themes to share colors and font styles
- Using custom fonts
- Working with Tabs
- Building a form with validation
- Create and style a text field
- Focus on a Text Field
- Handling changes to a text field
- Retrieve the value of a text field
- Adding Material Touch Ripples
- Handling Taps
- Implement Swipe to Dismiss
- Display images from the internet
- Fade in images with a placeholder
- Working with cached images
- Basic List
- Create a horizontal list
- Creating a Grid List
- Creating lists with different types of items
- Place a floating app bar above a list
- Working with long lists
- Report errors to a service
- Animating a Widget across screens
- Navigate to a new screen and back
- Navigate with named routes
- Pass arguments to a named route
- Return data from a screen
- Send data to a new screen
- Fetch data from the internet
- Making authenticated requests
- Parsing JSON in the background
- Working with WebSockets
- Persist data with SQLite
- Reading and Writing Files
- Storing key-value data on disk
- Play and pause a video
- Take a picture using the Camera
- An introduction to integration testing
- Performance profiling
- Scrolling
- An introduction to unit testing
- Mock dependencies using Mockito
- An introduction to widget testing
- Finding widgets
- Tapping, dragging and entering text
- Development
- Introduction to widgets
- Layout tutorial
- Dealing with box constraints
- Adding interactivity to your Flutter app
- Adding assets and images
- Navigation & routing
- Navigate to a new screen and back
- Send data to a new screen
- Return data from a screen
- Navigate with named routes
- Animating a Widget across screens
- AnimatedList
- Sample App Catalog
- Animations overview
- Animations tutorial
- Staggered Animations
- Slivers
- Taps, drags, and other gestures
- Accessibility widgets
- Assets, images, and icon widgets
- Async widgets
- Input widgets
- Interaction model widgets
- Painting and effect widgets
- Scrolling widgets
- Styling widgets
- Text widgets
- State management
- Start thinking declaratively
- Differentiate between ephemeral state and app state
- Simple app state management
- List of state management approaches
- JSON and serialization
- Accessibility
- Internationalizing Flutter apps
- Writing custom platform-specific code
- Using packages
- Fetch data from the internet
- Developing packages & plugins
- Background processes
- Android Studio / IntelliJ
- Set up an editor
- Flutter inspector
- Creating Useful Bug Reports
- Visual Studio Code
- Set up an editor
- Upgrading Flutter
- Hot reload
- Code formatting
Using packages
Flutter supports using shared packages contributed by other developers to the Flutter and Dart ecosystems. This allows you to quickly build your app without having to develop everything from scratch.
Existing packages enable many use cases, for example, making network requests (http
), custom navigation/route handling (fluro
), integration with device APIs (like url_launcher
& battery
), and using third-party platform SDKs (like Firebase).
If you are looking to develop a new package, please see developing packages.
If you are looking to add assets, images, or fonts, whether stored in files or packages, please see Assets & images.
Using packages
Searching for packages
Packages are published to the Pub site.
The Flutter landing page on the Pub site displays top packages that are compatible with Flutter (those that declare dependencies generally compatible with Flutter), and supports searching among all published packages.
Adding a package dependency to an app
To add a package ‘css_colors’ to an app:
- Depend on it
- Open the
pubspec.yaml
file located inside your app folder, and addcss_colors:
underdependencies
.
- Open the
- Install it
- From the terminal: Run
flutter packages get
OR - From Android Studio/IntelliJ: Click ‘Packages Get’ in the action ribbon at the top of
pubspec.yaml
- From VS Code: Click ‘Get Packages’ located in right side of the action ribbon at the top of
pubspec.yaml
- From the terminal: Run
- Import it
- Add a corresponding
import
statement in your Dart code.
- Add a corresponding
- Stop and restart the app, if necessary
- If the package brings platform-specific code (Java/Kotlin for Android, Swift/Objective-C for iOS), that code must be built into your app. Hot reload and hot restart do this only for the Dart code of the package, so you may have to do a full restart of the app to avoid errors like
MissingPluginException
when using the package.
- If the package brings platform-specific code (Java/Kotlin for Android, Swift/Objective-C for iOS), that code must be built into your app. Hot reload and hot restart do this only for the Dart code of the package, so you may have to do a full restart of the app to avoid errors like
The ‘Installing’ tab available on any package page on Pub is a handy reference for these steps.
For a complete example, see CSS Colors example below.
Developing new packages
Should a package not be available for your specific use case, you can develop new custom packages.
Managing package dependencies & versions
Package versions
All packages have a version number, specified in their pubspec.yaml
file. The current version of a package is displayed next to its name (for example, see the url_launcher package), as well as a list of all prior versions (url_launcher versions).
When a package is added to pubspec.yaml
using the shorthand form plugin1:
this is interpreted as plugin1: any
, i.e. any version of the package may be used. To ensure your app does not break when a package is updated, we recommend specifying a version range using one of the following formats:
Range constraints: Specify a minimum and maximim version. For example:
dependencies: url_launcher: '>=0.1.2 <0.2.0'
Range constraint with caret syntax is similar to regular range constraints:
dependencies: collection: '^0.1.2'
For additional details, see the Pub versioning guide.
Updating package dependencies
When you run flutter packages get
(‘Packages Get’ in IntelliJ) for the first time after adding a package, Flutter saves the concrete package version found in the pubspec.lock
lockfile. This ensures that you get the same version again if you, or another developer on your team, run flutter packages get
.
If you want to upgrade to a new version of the package, for example to use new features in that package, run flutter packages upgrade
(‘Upgrade dependencies’ in IntelliJ). This retrieves the highest available version of the package that is allowed by the version constraint you have specified in pubspec.yaml
.
Dependencies on unpublished packages
Packages can be used even when not published on Pub. For private plugins not intended for public publishing, or for packages not yet ready for publishing, additional dependency options are avaialble:
Path dependency: A Flutter app can depend on a plugin via a file system
path:
dependency. The path can be either relative, or absolute. For example, to depend on a plugin ‘plugin1’ located in a directory next to the app, use this syntax:dependencies: plugin1: path: ../plugin1/
- Git dependency: You can also depend on a package stored in a Git repository. If the package is located in the root of the repo, use this syntax:
dependencies: plugin1: git: url: git://github.com/flutter/plugin1.git
- Git dependency on a package in a folder: By default Pub assumes the package is located in the root of the Git repository. If that is not the case, you can specify the location with the
path
argument. For example:dependencies: package1: git: url: git://github.com/flutter/packages.git path: packages/package1
Finally, you can use the
ref
argument to pin the dependency to a specific git commit, branch, or tag. For more details, see Pub Dependencies.
Examples
Example: Using the CSS Colors package
The css_colors
package defines color constants for the CSS colors, allowing you to use them wherever the Flutter framework expects the Color
type.
To use this package:
Create a new project called ‘cssdemo’
- Open
pubspec.yaml
, and replace:dependencies: flutter: sdk: flutter
with:
dependencies: flutter: sdk: flutter css_colors: ^1.0.0
Run
flutter packages get
in the terminal, or click ‘Packages get’ in IntelliJ- Open
lib/main.dart
and replace its full contents with:
import 'package:css_colors/css_colors.dart';
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: DemoPage(),
);
}
}
class DemoPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(body: Container(color: CSSColors.orange));
}
}
- Run the app. When you click the ‘Show Flutter homepage’ you should see the phone’s default browser open, and the Flutter homepage appear.
Example: Using the URL Launcher package to launch the browser
The URL Launcher plugin package enables you to open the default browser on the mobile platform to display a given URL. It demonstrates how packages may also contain platform-specific code (we call these packages ‘plugins’). It is supported on both Android and iOS.
To use this plugin:
Create a new project called ‘launchdemo’
- Open
pubspec.yaml
, and replace:dependencies: flutter: sdk: flutter
with:
dependencies: flutter: sdk: flutter url_launcher: ^0.4.1
Run
flutter packages get
in the terminal, or click ‘Packages get’ in IntelliJ- Open
lib/main.dart
and replace its full contents with:
import 'package:flutter/material.dart';
import 'package:url_launcher/url_launcher.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: DemoPage(),
);
}
}
class DemoPage extends StatelessWidget {
launchURL() {
launch('https://flutter.io');
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: RaisedButton(
onPressed: launchURL,
child: Text('Show Flutter homepage'),
),
),
);
}
}
- Run the app (or stop and restart it, if you already had it running before adding the plugin). When you click the ‘Show Flutter homepage’ you should see the phone’s default browser open, and the Flutter homepage appear.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论