【发布时间】:2019-11-25 22:30:46
【问题描述】:
我已阅读此https://github.com/flutter/flutter/wiki/Add-Flutter-to-existing-apps。
我的问题是将数据从现有的原生 android 应用程序传递到颤振模块(例如:令牌、用户名......等)。所以,我想问一下有没有办法在现有的原生应用程序的Native代码和flutter模块的代码之间传递数据?
比如有两个页面,A和B,A是用Java代码写的,B是嵌入flutter view的,我没有找到任何办法将A中的数据传给B中的flutter view。
public class TwoActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.two_activity);
//this params passed from HomeActivity
String params = getIntent().getStringExtra("params");
FrameLayout rootView = findViewById(R.id.container);
View flutterView = Flutter.createView(this, getLifecycle(), "service");
FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
rootView.addView(flutterView, layoutParams);
}
}
这是main.dart
void main() => runApp(chooseWidget(window.defaultRouteName));
Widget chooseWidget(String route) {
switch(route) {
case 'service':
return MyFlutterView();
}
}
class MyFlutterView extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: HomePage(),
);
}
}
class HomePage extends StatelessWidget {
static const platform = const MethodChannel('samples.flutter.dev/start');
@override
Widget build(BuildContext context) {
return Container(
color: Colors.white,
child: Column(
children: <Widget>[
Text(
'this is a flutter page',
style: TextStyle(
fontSize: 14,
color: Colors.blue
),
),
FlatButton(
onPressed: () {
platform.invokeMethod('startActivity');
},
child: Text('go native page'),
color: Colors.purple,
highlightColor: Colors.deepPurple,
)
],
),
);
}
}
-------------->2019.7.18编辑
Thank you for your help. I found the answer.
1、BasicMessageChannel:use this to pass string or other object.
2、MethodChannel:use this to method invocation
3、EventChannel: use this to event streams
【问题讨论】:
-
使用系统频道
-
有没有文章介绍类似问题的解决方案?@MuratAslan
-
例如见
BasicMessageChannel