Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in
Toggle navigation
F
Front-End
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
abdullh.alsoleman
Front-End
Commits
af008749
Commit
af008749
authored
Mar 24, 2016
by
Jason Simmons
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
An example showing how to send messages between the host app and Flutter
parent
de8262f4
Changes
6
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
238 additions
and
42 deletions
+238
-42
main.dart
examples/hello_android/app/src/flutter/lib/main.dart
+66
-1
AndroidManifest.xml
examples/hello_android/app/src/main/AndroidManifest.xml
+4
-3
ExampleActivity.java
...pp/src/main/java/com/example/flutter/ExampleActivity.java
+146
-0
FlutterActivity.java
...pp/src/main/java/com/example/flutter/FlutterActivity.java
+0
-38
flutter_layout.xml
.../hello_android/app/src/main/res/layout/flutter_layout.xml
+21
-0
strings.xml
examples/hello_android/app/src/main/res/values/strings.xml
+1
-0
No files found.
examples/hello_android/app/src/flutter/lib/main.dart
View file @
af008749
...
...
@@ -2,6 +2,71 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import
'dart:async'
;
import
'dart:convert'
;
import
'dart:math'
;
import
'package:flutter/material.dart'
;
import
'package:flutter/widgets.dart'
;
import
'package:flutter/services.dart'
;
final
Random
random
=
new
Random
();
Future
<
String
>
handleGetRandom
(
String
json
)
async
{
Map
message
=
JSON
.
decode
(
json
);
double
min
=
message
[
'min'
].
toDouble
();
double
max
=
message
[
'max'
].
toDouble
();
double
value
=
(
random
.
nextDouble
()
*
(
max
-
min
))
+
min
;
Map
reply
=
{
'value'
:
value
};
return
JSON
.
encode
(
reply
);
}
class
HelloAndroid
extends
StatefulWidget
{
@override
_HelloAndroidState
createState
()
=>
new
_HelloAndroidState
();
}
class
_HelloAndroidState
extends
State
<
HelloAndroid
>
{
double
_latitude
;
double
_longitude
;
@override
Widget
build
(
BuildContext
context
)
{
return
new
Material
(
child:
new
Center
(
child:
new
Column
(
children:
<
Widget
>[
new
Text
(
'Hello from Flutter!'
),
new
RaisedButton
(
child:
new
Text
(
'Get Location'
),
onPressed:
_getLocation
),
new
Text
(
'Latitude:
${_latitude}
, Longitude:
${_longitude}
'
),
]
)
)
);
}
void
_getLocation
()
{
Map
message
=
{
'provider'
:
'network'
};
HostMessages
.
sendToHost
(
'getLocation'
,
JSON
.
encode
(
message
))
.
then
(
_onReceivedLocation
);
}
void
_onReceivedLocation
(
String
json
)
{
Map
reply
=
JSON
.
decode
(
json
);
setState
(()
{
_latitude
=
reply
[
'latitude'
];
_longitude
=
reply
[
'longitude'
];
});
}
}
void
main
(
)
{
runApp
(
new
HelloAndroid
());
void
main
(
)
=>
runApp
(
new
Center
(
child:
new
Text
(
'Hello from Flutter!'
)));
HostMessages
.
addMessageHandler
(
'getRandom'
,
handleGetRandom
);
}
examples/hello_android/app/src/main/AndroidManifest.xml
View file @
af008749
...
...
@@ -6,14 +6,15 @@
<uses-sdk
android:minSdkVersion=
"16"
android:targetSdkVersion=
"21"
/>
<uses-permission
android:name=
"android.permission.INTERNET"
/>
<uses-permission
android:name=
"android.permission.ACCESS_FINE_LOCATION"
/>
<application
android:name=
"org.domokit.sky.shell.SkyApplication"
android:label=
"@string/app_name"
>
<activity
android:name=
".FlutterActivity"
android:label=
"@string/app_name"
android:configChanges=
"orientation|keyboardHidden|keyboard|screenSize|locale|layoutDirection"
android:name=
".ExampleActivity"
android:launchMode=
"singleTask"
android:theme=
"@android:style/Theme.Black.NoTitleBar"
android:configChanges=
"orientation|keyboardHidden|keyboard|screenSize|locale|layoutDirection"
android:hardwareAccelerated=
"true"
android:windowSoftInputMode=
"adjustResize"
>
<intent-filter>
<action
android:name=
"android.intent.action.MAIN"
/>
...
...
examples/hello_android/app/src/main/java/com/example/flutter/ExampleActivity.java
0 → 100644
View file @
af008749
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package
com
.
example
.
flutter
;
import
android.app.Activity
;
import
android.content.Context
;
import
android.location.Location
;
import
android.location.LocationManager
;
import
android.os.Bundle
;
import
android.util.Log
;
import
android.view.View
;
import
android.widget.Button
;
import
android.widget.TextView
;
import
org.chromium.base.PathUtils
;
import
org.domokit.activity.ActivityImpl
;
import
org.domokit.sky.shell.SkyMain
;
import
org.domokit.sky.shell.PlatformViewAndroid
;
import
java.io.File
;
import
org.json.JSONException
;
import
org.json.JSONObject
;
public
class
ExampleActivity
extends
Activity
{
private
static
final
String
TAG
=
"ExampleActivity"
;
private
PlatformViewAndroid
flutterView
;
@Override
public
void
onCreate
(
Bundle
savedInstanceState
)
{
super
.
onCreate
(
savedInstanceState
);
SkyMain
.
ensureInitialized
(
getApplicationContext
(),
null
);
setContentView
(
R
.
layout
.
flutter_layout
);
flutterView
=
(
PlatformViewAndroid
)
findViewById
(
R
.
id
.
flutter_view
);
File
appBundle
=
new
File
(
PathUtils
.
getDataDirectory
(
this
),
SkyMain
.
APP_BUNDLE
);
flutterView
.
runFromBundle
(
appBundle
.
getPath
(),
null
);
flutterView
.
addOnMessageListener
(
"getLocation"
,
new
PlatformViewAndroid
.
OnMessageListener
()
{
@Override
public
String
onMessage
(
String
message
)
{
return
onGetLocation
(
message
);
}
});
Button
getRandom
=
(
Button
)
findViewById
(
R
.
id
.
get_random
);
getRandom
.
setOnClickListener
(
new
View
.
OnClickListener
()
{
@Override
public
void
onClick
(
View
v
)
{
sendGetRandom
();
}
});
}
@Override
protected
void
onDestroy
()
{
if
(
flutterView
!=
null
)
{
flutterView
.
destroy
();
}
super
.
onDestroy
();
}
@Override
protected
void
onPause
()
{
super
.
onPause
();
flutterView
.
onPause
();
}
@Override
protected
void
onResume
()
{
super
.
onResume
();
flutterView
.
onResume
();
}
private
void
sendGetRandom
()
{
JSONObject
message
=
new
JSONObject
();
try
{
message
.
put
(
"min"
,
1
);
message
.
put
(
"max"
,
1000
);
}
catch
(
JSONException
e
)
{
Log
.
e
(
TAG
,
"JSON exception"
,
e
);
return
;
}
flutterView
.
sendToFlutter
(
"getRandom"
,
message
.
toString
(),
new
PlatformViewAndroid
.
MessageReplyCallback
()
{
@Override
public
void
onReply
(
String
json
)
{
onRandomReply
(
json
);
}
});
}
private
void
onRandomReply
(
String
json
)
{
double
value
;
try
{
JSONObject
reply
=
new
JSONObject
(
json
);
value
=
reply
.
getDouble
(
"value"
);
}
catch
(
JSONException
e
)
{
Log
.
e
(
TAG
,
"JSON exception"
,
e
);
return
;
}
TextView
randomValue
=
(
TextView
)
findViewById
(
R
.
id
.
random_value
);
randomValue
.
setText
(
Double
.
toString
(
value
));
}
private
String
onGetLocation
(
String
json
)
{
String
provider
;
try
{
JSONObject
message
=
new
JSONObject
(
json
);
provider
=
message
.
getString
(
"provider"
);
}
catch
(
JSONException
e
)
{
Log
.
e
(
TAG
,
"JSON exception"
,
e
);
return
null
;
}
String
locationProvider
;
if
(
provider
.
equals
(
"network"
))
{
locationProvider
=
LocationManager
.
NETWORK_PROVIDER
;
}
else
if
(
provider
.
equals
(
"gps"
))
{
locationProvider
=
LocationManager
.
GPS_PROVIDER
;
}
else
{
return
null
;
}
LocationManager
locationManager
=
(
LocationManager
)
getSystemService
(
Context
.
LOCATION_SERVICE
);
Location
location
=
locationManager
.
getLastKnownLocation
(
locationProvider
);
JSONObject
reply
=
new
JSONObject
();
try
{
reply
.
put
(
"latitude"
,
location
.
getLatitude
());
reply
.
put
(
"longitude"
,
location
.
getLongitude
());
}
catch
(
JSONException
e
)
{
Log
.
e
(
TAG
,
"JSON exception"
,
e
);
return
null
;
}
return
reply
.
toString
();
}
}
examples/hello_android/app/src/main/java/com/example/flutter/FlutterActivity.java
deleted
100644 → 0
View file @
de8262f4
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package
com
.
example
.
flutter
;
import
android.app.Activity
;
import
android.os.Bundle
;
import
org.chromium.base.PathUtils
;
import
org.domokit.sky.shell.SkyMain
;
import
org.domokit.sky.shell.PlatformViewAndroid
;
import
java.io.File
;
public
class
FlutterActivity
extends
Activity
{
private
PlatformViewAndroid
flutterView
;
@Override
public
void
onCreate
(
Bundle
savedInstanceState
)
{
super
.
onCreate
(
savedInstanceState
);
SkyMain
.
ensureInitialized
(
getApplicationContext
(),
null
);
setContentView
(
R
.
layout
.
flutter_layout
);
flutterView
=
(
PlatformViewAndroid
)
findViewById
(
R
.
id
.
flutter_view
);
File
appBundle
=
new
File
(
PathUtils
.
getDataDirectory
(
this
),
SkyMain
.
APP_BUNDLE
);
flutterView
.
runFromBundle
(
appBundle
.
getPath
(),
null
);
}
@Override
protected
void
onDestroy
()
{
if
(
flutterView
!=
null
)
{
flutterView
.
destroy
();
}
super
.
onDestroy
();
}
}
examples/hello_android/app/src/main/res/layout/flutter_layout.xml
View file @
af008749
...
...
@@ -4,15 +4,36 @@
android:layout_width=
"fill_parent"
android:layout_height=
"fill_parent"
>
<TextView
android:id=
"@+id/text_view"
android:layout_width=
"fill_parent"
android:layout_height=
"wrap_content"
android:text=
"@string/title"
/>
<LinearLayout
android:orientation=
"horizontal"
android:layout_width=
"fill_parent"
android:layout_height=
"wrap_content"
>
<Button
android:id=
"@+id/get_random"
android:layout_width=
"wrap_content"
android:layout_height=
"wrap_content"
android:text=
"@string/get_random"
/>
<TextView
android:id=
"@+id/random_value"
android:layout_width=
"wrap_content"
android:layout_height=
"wrap_content"
/>
</LinearLayout>
<org.domokit.sky.shell.PlatformViewAndroid
android:id=
"@+id/flutter_view"
android:layout_width=
"fill_parent"
android:layout_height=
"fill_parent"
/>
</LinearLayout>
examples/hello_android/app/src/main/res/values/strings.xml
View file @
af008749
...
...
@@ -2,4 +2,5 @@
<resources>
<string
name=
"app_name"
>
Flutter App
</string>
<string
name=
"title"
>
Flutter Application
</string>
<string
name=
"get_random"
>
Get Random Number
</string>
</resources>
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment