当从本机视图收到数据时,如何在flatter中更新小部件-Android
来自Android本机主机$count的Hello 我使用methodChannel在Flatter和Android原生视图之间进行通信 应用程序开始->Main.dart(1个按钮)->打开Android的MainActivity->打开NativeView活动,这里按钮计数器的onTap增量为1,日志以上面的消息打印,但我也想在颤振屏幕上显示此消息 我在做什么??短暂性脑缺血发作 主飞镖当从本机视图收到数据时,如何在flatter中更新小部件-Android,android,flutter,cross-platform,Android,Flutter,Cross Platform,来自Android本机主机$count的Hello 我使用methodChannel在Flatter和Android原生视图之间进行通信 应用程序开始->Main.dart(1个按钮)->打开Android的MainActivity->打开NativeView活动,这里按钮计数器的onTap增量为1,日志以上面的消息打印,但我也想在颤振屏幕上显示此消息 我在做什么??短暂性脑缺血发作 主飞镖 import 'dart:async'; import 'package:flutter/materia
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
void main() => runApp(new MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new MaterialApp(
title: 'Flutter-Native Demo',
theme: new ThemeData(
primarySwatch: Colors.blue,
),
home: new MyHomePage(title: 'Channel Demo App'),
);
}
}
class MyHomePage extends StatefulWidget {
final String title;
MyHomePage({Key key, this.title}) : super(key: key) {}
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
static const platform = const MethodChannel('flutter.channel');
var valueFromNative = 0;
@override
void initState() {
super.initState();
platform.setMethodCallHandler(_handleMethod);
}
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
title: new Text(widget.title),
),
body: new Center(
child: new Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
new RaisedButton(
child: new Text('Show native view'),
onPressed: _showNativeView,
),
new RaisedButton(
child: new Text(valueFromNative.toString()),
onPressed: () {
print("CHECK THIS VAL $valueFromNative");
}),
],
),
),
);
}
Future<Null> _showNativeView() async {
await platform.invokeMethod('showNativeView');
}
Future<void> _handleMethod(MethodCall call) async {
switch (call.method) {
case "message":
print("CHECK THIS");
//Able to get console log here
debugPrint(call.arguments);
setState(() {
new Text(call.arguments);
valueFromNative = call.arguments;
});
print("CHECK THIS VAL ${call.arguments}"); //but not able to get value here
break;
}
}
}
NativeViewActivity.kt
package flutter.channel
import android.content.Intent
import android.os.Bundle
import android.util.Log
import io.flutter.app.FlutterActivity
import io.flutter.plugin.common.MethodChannel
import io.flutter.plugins.GeneratedPluginRegistrant
class MainActivity() : FlutterActivity() {
companion object {
const val CHANNEL = "flutter.channel"
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
GeneratedPluginRegistrant.registerWith(this)
MethodChannel(flutterView, CHANNEL).setMethodCallHandler { call, result ->
if (call.method == "showNativeView") {
val intent = Intent(this, NativeViewActivity::class.java)
startActivity(intent)
result.success(true)
} else {
result.notImplemented()
}
}
}
}
package flutter.channel
import android.os.Bundle
import android.util.Log
import android.widget.Button
import android.widget.TextView
import flutter.channel.MainActivity
import flutter.channel.R
import io.flutter.app.FlutterActivity
import io.flutter.plugin.common.EventChannel
import io.flutter.plugin.common.MethodChannel
class NativeViewActivity : FlutterActivity() {
var count = 0
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val channel = MethodChannel(flutterView, MainActivity.CHANNEL)
setContentView(R.layout.layout)
val tvCount = findViewById<TextView>(R.id.tvCount)
findViewById<Button>(R.id.button).setOnClickListener {
++count
tvCount.text = count.toString()
channel.invokeMethod("message", "Hello from Android native host $count");
}
}
}
package flatter.channel
导入android.os.Bundle
导入android.util.Log
导入android.widget.Button
导入android.widget.TextView
导入flatter.channel.main活动
导入颤振.channel.R
导入io.flatter.app.flatterActivity
导入io.flatter.plugin.common.EventChannel
导入io.flatter.plugin.common.MethodChannel
类NativeViewActivity:Activity(){
变量计数=0
重写创建时的乐趣(savedInstanceState:Bundle?){
super.onCreate(savedInstanceState)
val通道=方法通道(视图,MainActivity.channel)
setContentView(R.layout.layout)
val tvCount=findViewById(R.id.tvCount)
findViewById(R.id.button).setOnClickListener{
++计数
tvCount.text=count.toString()
invokeMethod(“消息”,“来自Android本机主机$count的你好”);
}
}
}