import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'dart:async';
void main() => runApp(new MyApp());
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return new MaterialApp(
title: 'Flutter Demo',
theme: new ThemeData(
primarySwatch: Colors.blue,
),
home: new MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
final String title;
@override
_MyHomePageState createState() => new _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
static const platformMethodChannel = const MethodChannel("com.xyjy.flutterchannel/test");
num nativeMessage = 0;
void _incrementCounter() {
setState(() {
_counter++;
});
}
Future<Null> getData() async{
num _message;
try{
final num result = await platformMethodChannel.invokeMethod("getTime");
_message = result;
}
on PlatformException catch(e){
print(e.message);
}
setState(() {
nativeMessage = _message;
});
}
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
// Here we take the value from the MyHomePage object that was created by
// the App.build method, and use it to set our appbar title.
title: new Text(widget.title),
),
body: new Center(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
new Text(
"$nativeMessage",
),
new Text(
'$nativeMessage',
style: Theme.of(context).textTheme.display1,
),
],
),
),
floatingActionButton: new FloatingActionButton(
onPressed: ()=>getData(),
tooltip: 'Increment',
child: new Icon(Icons.add),
), // This trailing comma makes auto-formatting nicer for build methods.
);
}
}
复制代码
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
// Here we take the value from the MyHomePage object that was created by
// the App.build method, and use it to set our appbar title.
title: new Text(widget.title),
),
body: new Center(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
new Text(
"$nativeMessage",
),
new Text(
'$nativeMessage',
style: Theme.of(context).textTheme.display1,
),
],
),
),
floatingActionButton: new FloatingActionButton(
onPressed: ()=>getData(),
tooltip: 'Increment',
child: new Icon(Icons.add),
), // This trailing comma makes auto-formatting nicer for build methods.
);
}
复制代码
接下来本地方法如何编写,打开MainActivity
public class MainActivity extends FlutterActivity {
private static final String CHANNAL = "com.xyjy.flutterchannel/test";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
GeneratedPluginRegistrant.registerWith(this);
new MethodChannel(getFlutterView() , CHANNAL).setMethodCallHandler(new MethodChannel.MethodCallHandler()
{
@Override
public void onMethodCall(MethodCall methodCall, MethodChannel.Result result)
{
if (methodCall.method.equals("getTime"))
{
long time = getTime();
result.success(time);
}
else
{
result.notImplemented();
}
}
});
}
private long getTime()
{
return System.currentTimeMillis();
}
}
复制代码