Skip to content

Commit f3f7bf1

Browse files
authored
Merge pull request #34 from nstack-io/feature/languageswitch
Language switch and proper InheritedWidget
2 parents 1a2f4ea + 9406791 commit f3f7bf1

File tree

13 files changed

+349
-113
lines changed

13 files changed

+349
-113
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ dependencies:
2020
nstack:
2121
git:
2222
url: git://github.com/nstack-io/flutter-sdk.git
23-
ref: v0.1.4
23+
ref: v0.4.0
2424

2525
dev_dependencies:
2626
build_runner:
@@ -70,7 +70,7 @@ Now increment the `"version"` number and save (⌘s) to trigger an update.
7070
## Example
7171

7272
Import your `nstack.dart` file and plant your `NStackWidget` at the root of your application.\
73-
Use `NStackInitWidget` for submitting [AppOpen] events.
73+
Use `NStackAppOpen` for submitting [AppOpen] events.
7474

7575
```dart
7676
import 'package:flutter/material.dart';
@@ -84,7 +84,7 @@ class MyApp extends StatelessWidget {
8484
@override
8585
Widget build(BuildContext context) {
8686
return MaterialApp(
87-
home: NStackInitWidget(
87+
home: NStackAppOpen(
8888
child: Scaffold(
8989
appBar: AppBar(
9090
title: Text(context.localization.test.title),

example/ios/Flutter/AppFrameworkInfo.plist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,6 @@
2121
<key>CFBundleVersion</key>
2222
<string>1.0</string>
2323
<key>MinimumOSVersion</key>
24-
<string>8.0</string>
24+
<string>9.0</string>
2525
</dict>
2626
</plist>

example/ios/Podfile.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ EXTERNAL SOURCES:
2424
:path: ".symlinks/plugins/shared_preferences/ios"
2525

2626
SPEC CHECKSUMS:
27-
Flutter: 434fef37c0980e73bb6479ef766c45957d4b510c
27+
Flutter: 50d75fe2f02b26cc09d224853bb45737f8b3214a
2828
nstack: a7ebf31d8c387ec76d4bfe5eb494d3ac81756d92
2929
package_info: 873975fc26034f0b863a300ad47e7f1ac6c7ec62
3030
shared_preferences: af6bfa751691cdc24be3045c43ec037377ada40d

example/ios/Runner.xcodeproj/project.pbxproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@
348348
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
349349
GCC_WARN_UNUSED_FUNCTION = YES;
350350
GCC_WARN_UNUSED_VARIABLE = YES;
351-
IPHONEOS_DEPLOYMENT_TARGET = 8.0;
351+
IPHONEOS_DEPLOYMENT_TARGET = 9.0;
352352
MTL_ENABLE_DEBUG_INFO = NO;
353353
SDKROOT = iphoneos;
354354
SUPPORTED_PLATFORMS = iphoneos;
@@ -430,7 +430,7 @@
430430
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
431431
GCC_WARN_UNUSED_FUNCTION = YES;
432432
GCC_WARN_UNUSED_VARIABLE = YES;
433-
IPHONEOS_DEPLOYMENT_TARGET = 8.0;
433+
IPHONEOS_DEPLOYMENT_TARGET = 9.0;
434434
MTL_ENABLE_DEBUG_INFO = YES;
435435
ONLY_ACTIVE_ARCH = YES;
436436
SDKROOT = iphoneos;
@@ -479,7 +479,7 @@
479479
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
480480
GCC_WARN_UNUSED_FUNCTION = YES;
481481
GCC_WARN_UNUSED_VARIABLE = YES;
482-
IPHONEOS_DEPLOYMENT_TARGET = 8.0;
482+
IPHONEOS_DEPLOYMENT_TARGET = 9.0;
483483
MTL_ENABLE_DEBUG_INFO = NO;
484484
SDKROOT = iphoneos;
485485
SUPPORTED_PLATFORMS = iphoneos;

example/lib/main.dart

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,27 @@ class MyApp extends StatelessWidget {
99
@override
1010
Widget build(BuildContext context) {
1111
return MaterialApp(
12-
home: NStackInitWidget(child: Scaffold(
12+
home: MainScreen(),
13+
);
14+
}
15+
}
16+
17+
class MainScreen extends StatelessWidget {
18+
@override
19+
Widget build(BuildContext context) {
20+
return NStackAppOpen(
21+
child: Scaffold(
1322
appBar: AppBar(
14-
title: Text(context.localization.defaultSection.title),
23+
title: Text(context.localization.test.testDollarSign),
1524
),
1625
body: Center(
17-
child: Text(context.localization.test.testMultipleLines),
26+
child: MaterialButton(onPressed: () async => {
27+
NStackScope.of(context).changeLanguage(Locale("de-DE"))
28+
},
29+
child: Text("Selected locale: ${NStackScope.of(context).nstack.activeLanguage.name}")
30+
,),
1831
),
1932
),
20-
),
2133
);
2234
}
2335
}

example/lib/nstack.dart

Lines changed: 57 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
/// Generated by NStack, do not modify this file.
22
3+
import 'package:flutter/foundation.dart';
34
import 'package:flutter/widgets.dart';
45
import 'package:nstack/models/language.dart';
6+
import 'package:nstack/models/localize_index.dart';
57
import 'package:nstack/models/nstack_config.dart';
68
import 'package:nstack/nstack.dart';
79
import 'package:nstack/partial/section_key_delegate.dart';
@@ -27,21 +29,21 @@ class _DefaultSection extends SectionKeyDelegate {
2729
class _Test extends SectionKeyDelegate {
2830
const _Test(): super('test');
2931

30-
String get testDollarSign => get('testDollarSign', "\$testing");
32+
String get testDollarSign => get('testDollarSign', "\$testing change back");
3133
String get testSingleQuotationMark => get('testSingleQuotationMark', "\'testing\'");
3234
String get testDoubleQuotationMark => get('testDoubleQuotationMark', "\"testing\"");
3335
String get testMultipleLines => get('testMultipleLines', "testing\nmultiple\nlines");
3436
}
3537

3638
const _config = NStackConfig(projectId: 'h6wJremI2TGFM88gbLkdyljWQuwf2hxhxvCH', apiKey: 'zp2S18H32b67eYAbRQh94tVw76ZzaKKXlHjd');
3739

38-
const _languages = [
39-
Language(id: 56, name: 'English', locale: 'en-EN', direction: 'LRM', isDefault: true, isBestFit: true),
40-
Language(id: 7, name: 'German (Austria)', locale: 'de-AT', direction: 'LRM', isDefault: false, isBestFit: false),
40+
final _languages = [
41+
LocalizeIndex(id: 1216, url: null, lastUpdatedAt: null, shouldUpdate: false, language: Language(id: 56, name: 'English', locale: 'en-EN', direction: 'LRM', isDefault: true, isBestFit: true)),
42+
LocalizeIndex(id: 1270, url: null, lastUpdatedAt: null, shouldUpdate: false, language: Language(id: 7, name: 'German (Austria)', locale: 'de-AT', direction: 'LRM', isDefault: false, isBestFit: false)),
4143
];
4244

4345
const _bundledTranslations = {
44-
'en-EN': r'''{"data":{"default":{"title":"NStack SDK Demo","test":"test"},"test":{"testDollarSign":"$testing","testSingleQuotationMark":"'testing'","testDoubleQuotationMark":"\"testing\"","testMultipleLines":"testing\nmultiple\nlines"}},"meta":{"language":{"id":56,"name":"English","locale":"en-EN","direction":"LRM","is_default":false,"is_best_fit":false},"platform":{"id":515,"slug":"mobile"}}}''',
46+
'en-EN': r'''{"data":{"default":{"title":"NStack SDK Demo","test":"test"},"test":{"testDollarSign":"$testing change back","testSingleQuotationMark":"'testing'","testDoubleQuotationMark":"\"testing\"","testMultipleLines":"testing\nmultiple\nlines"}},"meta":{"language":{"id":56,"name":"English","locale":"en-EN","direction":"LRM","is_default":false,"is_best_fit":false},"platform":{"id":515,"slug":"mobile"}}}''',
4547
'de-AT': r'''{"data":{"default":{"title":"NStack SDK Demo","test":"test"},"test":{"testDollarSign":"__testDollarSign","testSingleQuotationMark":"__testSingleQuotationMark","testDoubleQuotationMark":"__testDoubleQuotationMark","testMultipleLines":"__testMultipleLines"}},"meta":{"language":{"id":7,"name":"German (Austria)","locale":"de-AT","direction":"LRM","is_default":false,"is_best_fit":false},"platform":{"id":515,"slug":"mobile"}}}''',
4648
};
4749

@@ -51,55 +53,85 @@ final _nstack = NStack<Localization>(
5153
availableLanguages: _languages,
5254
bundledTranslations: _bundledTranslations,
5355
pickedLanguageLocale: '',
56+
debug: kDebugMode
5457
);
5558

56-
class NStackWidget extends InheritedWidget {
57-
final NStack<Localization> nstack = _nstack;
59+
class NStackScope extends InheritedWidget {
60+
final NStack<Localization> nstack;
61+
final NStackState state;
62+
final String checksum;
5863

59-
NStackWidget({Key? key, required Widget child})
64+
NStackScope({Key? key, required Widget child, required this.state, required this.nstack, required this.checksum})
6065
: super(key: key, child: child);
6166

62-
static NStack of(BuildContext context) =>
63-
context.dependOnInheritedWidgetOfExactType<NStackWidget>()!.nstack;
67+
static NStackState of(BuildContext context) =>
68+
context.dependOnInheritedWidgetOfExactType<NStackScope>()!.state;
6469

6570
@override
66-
bool updateShouldNotify(NStackWidget oldWidget) =>
67-
nstack != oldWidget.nstack;
71+
bool updateShouldNotify(NStackScope oldWidget) =>
72+
checksum != oldWidget.checksum;
6873
}
6974

70-
class NStackInitWidget extends StatefulWidget {
75+
class NStackWidget extends StatefulWidget {
7176
final Widget child;
7277

73-
const NStackInitWidget({Key? key, required Widget child})
78+
const NStackWidget({Key? key, required Widget child})
7479
: child = child,
7580
super(key: key);
7681

7782
@override
78-
_NStackInitState createState() => _NStackInitState();
83+
NStackState createState() => NStackState();
7984
}
8085

81-
class _NStackInitState extends State<NStackInitWidget> {
82-
static bool _initialized = false;
86+
class NStackState extends State<NStackWidget> {
87+
final NStack<Localization> nstack = _nstack;
8388

84-
void setupNStack(BuildContext context) {
85-
final locale = Localizations.localeOf(context);
86-
final nstack = NStackWidget.of(context);
87-
nstack.appOpen(locale);
89+
changeLanguage(Locale locale) async {
90+
await _nstack.changeLocalization(locale).whenComplete(() => setState(() {}));
91+
}
92+
93+
Future<void> appOpen(Locale locale) async {
94+
await _nstack.appOpen(locale).whenComplete(() => setState(() {}));
8895
}
8996

9097
@override
9198
Widget build(BuildContext context) {
92-
if (!_initialized) {
93-
setupNStack(context);
94-
_initialized = true;
99+
return NStackScope(child: widget.child, state: this, nstack: this.nstack, checksum: nstack.checksum,);
100+
}
101+
}
102+
103+
class NStackAppOpen extends StatefulWidget {
104+
const NStackAppOpen({
105+
Key? key,
106+
required this.child,
107+
this.onComplete,
108+
}) : super(key: key);
109+
110+
final Widget child;
111+
final VoidCallback? onComplete;
112+
113+
@override
114+
_NStackAppOpenState createState() => _NStackAppOpenState();
115+
}
116+
117+
class _NStackAppOpenState extends State<NStackAppOpen> {
118+
bool _initializedNStack = false;
119+
120+
@override
121+
Widget build(BuildContext context) {
122+
if (!_initializedNStack) {
123+
NStackScope.of(context)
124+
.appOpen(Localizations.localeOf(context))
125+
.whenComplete(() => widget.onComplete?.call());
126+
_initializedNStack = true;
95127
}
96128
return widget.child;
97129
}
98130
}
99131

100132
/// Allows to access the Nstack Localization using the BuildContext
101133
extension NStackWidgetExtension on BuildContext {
102-
Localization get localization => NStackWidget.of(this).localization;
134+
Localization get localization => NStackScope.of(this).nstack.localization;
103135
}
104136

105137
/// Allows to access the Nstack Localization from StatefulWidget's State

example/pubspec.lock

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ packages:
267267
name: json_annotation
268268
url: "https://pub.dartlang.org"
269269
source: hosted
270-
version: "4.1.0"
270+
version: "4.0.1"
271271
logging:
272272
dependency: transitive
273273
description:
@@ -302,7 +302,7 @@ packages:
302302
path: ".."
303303
relative: true
304304
source: path
305-
version: "0.2.5"
305+
version: "0.4.0"
306306
package_config:
307307
dependency: transitive
308308
description:
@@ -503,7 +503,7 @@ packages:
503503
name: test_api
504504
url: "https://pub.dartlang.org"
505505
source: hosted
506-
version: "0.3.0"
506+
version: "0.4.2"
507507
timing:
508508
dependency: transitive
509509
description:

0 commit comments

Comments
 (0)