【问题标题】:How to navigate to a specific flutter route from an Android activity?如何从 Android 活动导航到特定的颤振路线?
【发布时间】:2019-01-12 07:59:36
【问题描述】:

我有一个现有的 android 应用程序,我已经在我的项目中集成了颤振我想调用一个颤振特定的路线,我在我的主要方法中定义这样的路线

class FlutterView extends StatelessWidget {
 @override
  Widget build(BuildContext context) {
  return new MaterialApp(
  title: 'Platform View',
  initialRoute: '/',
  routes: {
    '/': (context) => HomeScreen(),
    '/secound': (context) => MyCustomForm(),
    '/dashboard': (context) => DashBoardScreen(),
    '/login': (context) => LoginScreen(),
  },
  theme: new ThemeData(
    primarySwatch: Colors.red,
    textSelectionColor: Colors.red,
    textSelectionHandleColor: Colors.red,
    ),
   );
  }
}

从我的 android 活动中,我正在调用这样的颤动活动

startActivity(new Intent(this,FlutterActivity.class));

它确实打开了我的颤动活动,但使用 initialRoute: '/' 这很好,但有时我想打开例如('/dashboard')路线,当我打开颤动活动时我该怎么做??

【问题讨论】:

    标签: android dart flutter flutter-layout


    【解决方案1】:

    只需创建一个方法通道并从 Android 调用一个颤振函数。在该功能中,将您的应用程序从颤动导航到您想要的任何位置。

    有关如何使用方法通道在颤振和本机代码之间进行通信的更多信息,反之亦然。请看一下

    https://flutter.dev/docs/development/platform-integration/platform-channels

    【讨论】:

    【解决方案2】:

    来自 Android,如 here 所述:

    Intent intent = new Intent(context, MainActivity.class);
    intent.setAction(Intent.ACTION_RUN);
    intent.putExtra("route", "/routeName");
    context.startActivity(intent);
    

    来自 Flutter,使用 android_intent

    AndroidIntent intent = AndroidIntent(
      action: 'android.intent.action.RUN',
    
      // Replace this by your package name.
      package: 'app.example', 
    
      // Replace this by your package name followed by the activity you want to open.
      // The default activity provided by Flutter is MainActivity, but you can check
      // this in AndroidManifest.xml.
      componentName: 'app.example.MainActivity', 
    
      // Replace "routeName" by the route you want to open. Don't forget the "/".
      arguments: {'route': '/routeName'},
    );
    
    await intent.launch();
    

    请注意,应用程序只有在终止时才会在此路由中打开,也就是说,如果应用程序处于前台或后台,则不会在指定的路由中打开。

    【讨论】:

    • 非常详细的答案,谢谢
    • 效果很好,非常感谢
    猜你喜欢
    • 1970-01-01
    • 2019-06-30
    • 1970-01-01
    • 1970-01-01
    • 2019-12-16
    • 1970-01-01
    • 1970-01-01
    • 2018-09-15
    • 2020-07-14
    相关资源
    最近更新 更多