text
stringlengths
1
372
<code_start>
Navigator.of(context).pushNamed('/b');
<code_end>
the navigator class handles routing in flutter and is used to get
a result back from a route that you have pushed on the stack.
this is done by awaiting on the future returned by push().
for example, to start a location route that lets the user select their
location, you might do the following:
<code_start>
object? coordinates = await Navigator.of(context).pushNamed('/location');
<code_end>
and then, inside your location route, once the user has selected their
location, pop() the stack with the result:
<code_start>
navigator.of(context).pop({'lat': 43.821757, 'long': -79.226392});
<code_end>
<topic_end>
<topic_start>
navigating to another app
in UIKit, to send the user to another application,
you use a specific URL scheme.
for the system level apps, the scheme depends on the app.
to implement this functionality in flutter,
create a native platform integration, or use an
existing plugin, such as url_launcher.
<topic_end>
<topic_start>
manually pop back
calling SystemNavigator.pop() from your dart code
invokes the following iOS code:
if that doesn’t do what you want, you can create your own
platform channel to invoke arbitrary iOS code.
<topic_end>
<topic_start>
handling localization
unlike iOS, which has the localizable.strings file,
flutter doesn’t currently have a dedicated system for handling strings.
at the moment, the best practice is to declare your copy text
in a class as static fields and access them from there. for example:
<code_start>
class strings {
static const string welcomeMessage = 'welcome to flutter';
}
<code_end>
you can access your strings as such:
<code_start>
Text(Strings.welcomeMessage);
<code_end>
by default, flutter only supports US english for its strings.
if you need to add support for other languages,
include the flutter_localizations package.
you might also need to add dart’s intl
package to use i10n machinery, such as date/time formatting.
to use the flutter_localizations package,
specify the localizationsDelegates and
supportedLocales on the app widget:
<code_start>
import 'package:flutter/material.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
class MyWidget extends StatelessWidget {
const MyWidget({super.key});
@override
widget build(BuildContext context) {
return const MaterialApp(
localizationsDelegates: <localizationsdelegate<dynamic>>[
// add app-specific localization delegate[s] here
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
],
supportedLocales: <locale>[
locale('en', 'us'), // english
locale('he', 'il'), // hebrew
// ... other locales the app supports
],
);
}
}
<code_end>
the delegates contain the actual localized values,
while the supportedLocales defines which locales the app supports.
the above example uses a MaterialApp,
so it has both a GlobalWidgetsLocalizations
for the base widgets localized values,
and a MaterialWidgetsLocalizations for the material widgets localizations.
if you use WidgetsApp for your app, you don’t need the latter.
note that these two delegates contain “default” values,
but you’ll need to provide one or more delegates
for your own app’s localizable copy,
if you want those to be localized too.
when initialized, the WidgetsApp (or MaterialApp)
creates a localizations widget for you,
with the delegates you specify.
the current locale for the device is always accessible
from the localizations widget from the current context
(in the form of a locale object), or using the window.locale.
to access localized resources, use the localizations.of() method
to access a specific localizations class that is provided by a given delegate.
use the intl_translation package to extract translatable copy
to arb files for translating, and importing them back into the app
for using them with intl.