Flutter Beginner
Flutter Packages
Execute the command below to create a new flutter package:
1 | flutter create --template=package package_name |
The standard flutter package files structure:
-
package_name
- lib
- src
- feature1.dart
- feature2.dart
package_name
.dart
- src
- lib
-
src
: Private code directory -
package.dart
: // Main file, the file name must equals to the package name -
Don’t write any useful code in the
package_name.dart
, this file is just used to import the files you want to expose insrc
.
The standard content of the file package_name.dart
:
1 | library package_name; |
Flutter Plugin
Execute the command below to create a new flutter plugin:
1 | flutter create --org com.example --template=plugin --platforms=android,ios -i swift plugin_name |
--org
: Your organization host.--platforms
: The platforms this plugin supported.--i
Specify ios development language oc/swift)
How to edit the plugin in Xcode?
- Open the plugin directory. You can find a
Runner.xcworkspace
file, open it. - The plugin code is in the path
Pods/Development Pods/plugin_name/.../ios/Classes
- If you want to test the plugin code, you have two ways. First, you can directly write test code in the
Runner
target. Another better way is creating aUnit Test
target. - There are some problems you will get. You cannot
import plugin_name
to test its code. - I have a solution is appending pods dependence for the unit test target:
1 | target 'pluginName_exampleTests' do |
- Test code examples:
1 | import XCTest |
Communicating between Flutter and Native
- Method Channel
- You can send an action from flutter to native or reversing, and then you can asynchronously get a result from the other side.
- Event Channel
- Can only send a message from native to flutter
- Like an
event stream,
you can register the event channel in flutter to handle events. The native side will get asink
used to send an event to the flutter side. Unlikemethod channel
that can only receive one event, the receiver usingevent channel
can receive multiple events until canceling the subscription.