How to navigate from native iOS Widget to specific screen in Flutter app

116 views Asked by At

So Im building a flutter app, where I want iOS Widgets.

For that I built a simple Widget with WidgetKit in Swift

import WidgetKit
import SwiftUI

struct MediumWorkout: View {
    
    var entry: TrainingEntry
    
    var body: some View {
                
        // I tried replacing the interval_timer with intervaltimer - the interval timer thing comes from my pubspec.yaml, its the name of my app
        Link(destination: URL(string: "interval_timer://profile")!) {
             Text(intervalString)
       }
    }
}

But Im not able to navigate from the Widget, to the specific destination in my flutter app. The widget just routes to my initial route "workouts"

  initialRoute: '/workouts',
  routes: {
    '/workouts': (context) => const Home(screenIndex: 0),
    '/jumpin': (context) => const Home(screenIndex: 1),
    '/profile': (context) => const Home(screenIndex: 2),
  },

even though I want it to navigate to the profile section.

I guessed that its maybe because my routing in flutter isnt compatible with the swift one. Has anyone done this before & how can I solve the problem

1

There are 1 answers

0
Luís Otávio On

You could force an action to a specific route before initializing MaterialApp, using the Fluro (or GetX) plugin as follows:

void main() async {
   WidgetsFlutterBinding.ensureInitialized();
  
   final router = FluroRouter();
   defineRoutes(router); // call the route definition function
   runApp(MyApp());
}

void defineRoutes(FluroRouter router) {
   router.define('/workouts', handler: Handler(handlerFunc: (_, __) => const Home(screenIndex: 0)));
   router.define('/jumpin', handler: Handler(handlerFunc: (_, __) => const Home(screenIndex: 1)));
   router.define('/profile', handler: Handler(handlerFunc: (_, __) => const Home(screenIndex: 2)));
}