【发布时间】:2023-02-14 19:48:32
【问题描述】:
我正在尝试将 Flutter 模块添加到本机 iOS 应用程序。但是,我遇到了一个问题,即在多次呈现 FlutterViewController 时维护 Flutter 的导航堆栈(即显示详细信息屏幕而不是登录页面)。
使用 Flutter 引擎时如何重置导航堆栈?
这是我的演示代码。
class ExampleApp extends StatelessWidget {
const ExampleApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
initialRoute: 'example',
routes: {
'example': (context) => const LandingPage(),
},
);
}
}
class LandingPage extends StatelessWidget {
const LandingPage({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Landing screen')),
body: Center(
child: TextButton(
child: const Text('Go to details'),
onPressed: () => _navigateToDetails(context),
),
),
);
}
void _navigateToDetails(BuildContext context) {
Navigator.of(context).push(
MaterialPageRoute(builder: (context) => const DetailsPage()),
);
}
}
class DetailsPage extends StatelessWidget {
const DetailsPage({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Details screen')),
body: const Center(child: Text('Details')),
);
}
}
这是我的原生 Swift 代码。
@main
class AppDelegate: FlutterAppDelegate {
lazy var sharedEngine = FlutterEngine(name: "shared")
override func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
sharedEngine.run();
GeneratedPluginRegistrant.register(with: sharedEngine);
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}
}
class ViewController: UIViewController {
@IBAction private func onButtonTapped(_ sender: UIButton) {
let page = FlutterViewController(
engine: AppDelegate.current.sharedEngine,
nibName: nil,
bundle: nil
)
present(page, animated: true)
}
}
【问题讨论】:
标签: swift flutter flutter-add-to-app