我已经设法在 Flutter 上实现了原生 Android Activity。代码如下所示:
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
class Communication extends StatefulWidget {
@override
State<StatefulWidget> createState() {
return MyCommunication();
}
}
class MyCommunication extends State<Communication> {
static const platform = const MethodChannel("test_activity");
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: FutureBuilder<Widget>(
future: getNewActivity(),
builder: (BuildContext context, AsyncSnapshot<Widget> snapshot) {
if (snapshot.hasData)
return snapshot.data;
else
return Container(child: CircularProgressIndicator());
}),
));
}
Future<Widget> getNewActivity() async {
try {
return await platform.invokeMethod('startNewActivity');
} on PlatformException catch (e) {
print(e.message);
}
}
}
问题是,当我从本机活动中点击后退按钮以转到显示它的屏幕时,应用程序会显示容器,并在 snapshot.hasData 为 false 时返回圆形进度指示器。
我必须再次点击 Back 以关闭此屏幕并进入预期的呈现视图。显然,我在代码中做的不对。我该如何解决这个问题?