我有一个名为 X 的较大应用程序,还有另一个名为 Y 的较小应用程序。它们现在彼此分开,并且运行良好。我想将应用程序 Y 集成到 X 中。我想将 Y 的代码放在 X 项目中,但它们应该有不同的 Main,以便我可以设置不同的主题和路线。有办法做到这一点吗?我使用 GetX 中的 GetMaterialApp 而不是 MaterialApp。我希望如果我单击应用程序 X 上的按钮,然后我会导航到 Y 应用程序部分作为屏幕或页面。我已经完成了代码合并,例如控制器、模型、数据等。现在我有来自两个应用程序的 main.dart 文件。请帮忙!!!
我尝试将应用程序 Y 制作为一个包,但意识到这是行不通的。我尝试点击直接导航到应用程序 Y 的 MyApp(),但这也不起作用,因为无法初始化。然后我尝试导航到 Y,但在初始化之后,但我没有收到任何错误,也没有发生任何事情。画面没有变化到Y部分。
要将较小的应用程序 Y 集成到较大的应用程序 X 中,同时保持单独的主题和路线,您可以按照以下步骤操作:
你可以做类似的事情
在应用程序 X 的
main.dart
中:
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:app_x/my_app_x.dart'; // Import the main widget of app X
void main() {
runApp(MyAppX());
}
在
my_app_x.dart
:
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:app_x/home_screen.dart'; // Import the home screen of app X
class MyAppX extends StatelessWidget {
@override
Widget build(BuildContext context) {
return GetMaterialApp(
title: 'App X',
theme: ThemeData(...), // Define theme for app X
home: HomeScreenX(), // Entry point for app X
);
}
}
在
home_screen_x.dart
中(假设这是您要触发导航到应用程序 Y 的位置):
import 'package:flutter/material.dart';
import 'package:get/get.dart';
class HomeScreenX extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Home Screen X'),
),
body: Center(
child: ElevatedButton(
onPressed: () {
// Navigate to app Y
Get.toNamed('/app_y');
},
child: Text('Go to App Y'),
),
),
);
}
}
在应用程序 Y 的
main.dart
中:
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:app_y/my_app_y.dart'; // Import the main widget of app Y
void main() {
// Initialize necessary components for app Y
// This could include initializing controllers, services, etc.
runApp(MyAppY());
}
在
my_app_y.dart
:
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:app_y/home_screen_y.dart'; // Import the home screen of app Y
class MyAppY extends StatelessWidget {
@override
Widget build(BuildContext context) {
return GetMaterialApp(
title: 'App Y',
theme: ThemeData(...), // Define theme for app Y
initialRoute: '/home', // Define initial route for app Y
getPages: [
GetPage(name: '/home', page: () => HomeScreenY()), // Define routes for app Y
// Define more routes if needed
],
);
}
}
此设置应允许您从应用程序 X 导航到应用程序 Y,并为两个应用程序维护单独的主题和路线