nativelan 0.0.1
nativelan: ^0.0.1 copied to clipboard
NativeLan SDK for Flutter applications to deliver dynamic over-the-air localization updates.
example/lib/main.dart
import 'package:flutter/material.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:nativelan/nativelan.dart';
void main() {
runApp(
const NativeLanScope(
child: MyApp(),
),
);
}
class MyApp extends StatefulWidget {
const MyApp({Key? key}) : super(key: key);
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
// Default to English (US)
Locale _currentLocale = const Locale('en', 'US');
void _changeLanguage(Locale locale) {
setState(() {
_currentLocale = locale;
});
}
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'NativeLan Flutter Demo',
locale: _currentLocale,
localizationsDelegates: [
// Initialize the NativeLan SDK Delegate
NativeLanLocalizationsDelegate(
config: NativeLanConfig(
projectId: '01KXRBR1MVERKEN5BNYN1XP6Q9',
sdkKey: 'nlk_live_q2xyr0MM2i-hUeQxMgbp_zBqIXfU_eyUVIclv1aqHWo',
baseUrl: 'https://staging-api.nativelan.com',
prerelease: false, // Set to true to test staged draft bundles
appVersion: '1.0.0',
),
),
// Fallbacks for Flutter framework widgets
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
],
supportedLocales: const [
Locale('en', 'US'),
Locale('ar', 'IQ'),
],
home: HomeScreen(
currentLocale: _currentLocale,
onLocaleChange: _changeLanguage,
),
);
}
}
class HomeScreen extends StatelessWidget {
final Locale currentLocale;
final Function(Locale) onLocaleChange;
const HomeScreen({
Key? key,
required this.currentLocale,
required this.onLocaleChange,
}) : super(key: key);
@override
Widget build(BuildContext context) {
final localizations = NativeLanLocalizations.of(context);
// Resolve string key 'continue_button' (falls back to local text if not synced OTA yet)
final testTranslation = localizations?.translate('continue_button') ?? 'Continue Button (Local Fallback)';
final languages = [
{'name': 'English (en-US)', 'locale': const Locale('en', 'US')},
{'name': 'Arabic (ar-IQ)', 'locale': const Locale('ar', 'IQ')},
];
return Scaffold(
backgroundColor: const Color(0xFFFAFAFA),
body: SafeArea(
child: Padding(
padding: const EdgeInsets.all(24.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
const SizedBox(height: 32),
const Text(
'NativeLan SDK Sandbox',
style: TextStyle(
fontSize: 18,
fontWeight: FontWeight.bold,
color: Color(0xFF212121),
),
textAlign: TextAlign.center,
),
const SizedBox(height: 8),
const Text(
'Dashboard key: continue_button',
style: TextStyle(
fontSize: 11,
fontWeight: FontWeight.bold,
color: Color(0xFF999999),
),
textAlign: TextAlign.center,
),
const SizedBox(height: 24),
// Elevated white container card
Container(
padding: const EdgeInsets.all(24.0),
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(8.0),
boxShadow: const [
BoxShadow(
color: Color(0x0C000000), // ~5% opacity black
blurRadius: 4.0,
offset: Offset(0, 2),
),
],
),
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Text(
testTranslation,
style: const TextStyle(
fontSize: 28,
fontWeight: FontWeight.bold,
color: Color(0xFF3F51B5),
),
textAlign: TextAlign.center,
),
],
),
),
const SizedBox(height: 32),
// Horizontal Divider
Container(
height: 1,
color: const Color(0xFFDDDDDD),
),
const SizedBox(height: 24),
const Text(
'OTA Target Control',
style: TextStyle(
fontSize: 12,
fontWeight: FontWeight.bold,
color: Color(0xFF999999),
),
textAlign: TextAlign.center,
),
const SizedBox(height: 16),
// Spinner dropdown
Container(
padding: const EdgeInsets.symmetric(horizontal: 12.0),
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(8.0),
border: Border.all(
color: const Color(0xFFBDBDBD),
),
),
child: DropdownButtonHideUnderline(
child: DropdownButton<Locale>(
value: currentLocale,
isExpanded: true,
style: const TextStyle(
color: Color(0xFF212121),
fontSize: 16,
),
onChanged: (Locale? value) {
if (value != null && value != currentLocale) {
onLocaleChange(value);
}
},
items: languages.map((lang) {
return DropdownMenuItem<Locale>(
value: lang['locale'] as Locale,
child: Text(lang['name'] as String),
);
}).toList(),
),
),
),
const SizedBox(height: 24),
// Active Locale Debug Status
Text(
'SDK Active Locale: ${currentLocale.toLanguageTag()}',
style: const TextStyle(
fontSize: 11,
color: Color(0xFF888888),
fontFamily: 'monospace',
),
textAlign: TextAlign.center,
),
],
),
),
),
);
}
}