Skip to content

Commit b4f4e95

Browse files
johsoemarkusrubey
andauthored
Simplified App Open and init*() (#40)
* build: bumped to 0.4.2 * feat: simplified app open and init() and made it be called implicitly * build: bumped to 0.5.0 * chore: Minor updates (#41) - Showcase builder as the recommended way - Update README Co-authored-by: Markus Rubey <[email protected]>
1 parent 1859500 commit b4f4e95

File tree

7 files changed

+75
-91
lines changed

7 files changed

+75
-91
lines changed

README.md

Lines changed: 10 additions & 8 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.4.1
23+
ref: v0.5.0
2424

2525
dev_dependencies:
2626
build_runner:
@@ -69,8 +69,7 @@ Now increment the `"version"` number and save (⌘s) to trigger an update.
6969

7070
## Example
7171

72-
Import your `nstack.dart` file and plant your `NStackWidget` at the root of your application.\
73-
Use `NStackAppOpen` for submitting [AppOpen] events.
72+
Import your `nstack.dart` file and plant your `NStackWidget` in the builder of your application.
7473

7574
```dart
7675
import 'package:flutter/material.dart';
@@ -84,11 +83,14 @@ class MyApp extends StatelessWidget {
8483
@override
8584
Widget build(BuildContext context) {
8685
return MaterialApp(
87-
home: NStackAppOpen(
88-
child: Scaffold(
89-
appBar: AppBar(
90-
title: Text(context.localization.test.title),
91-
),
86+
builder: (context, child) {
87+
return NStackWidget(
88+
child: child!,
89+
);
90+
},
91+
home: Scaffold(
92+
appBar: AppBar(
93+
title: Text(context.localization.test.title),
9294
),
9395
),
9496
);

example/lib/main.dart

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,19 @@ import 'package:flutter/material.dart';
33
import 'nstack.dart';
44

55
void main() {
6-
runApp(NStackWidget(child: MyApp()));
6+
runApp(MyApp());
77
}
88

99
class MyApp extends StatelessWidget {
1010
@override
1111
Widget build(BuildContext context) {
1212
return MaterialApp(
13+
builder: (context, child) {
14+
return NStackWidget(
15+
platformOverride: AppOpenPlatform.android,
16+
child: child!,
17+
);
18+
},
1319
home: MainScreen(),
1420
);
1521
}
@@ -18,19 +24,16 @@ class MyApp extends StatelessWidget {
1824
class MainScreen extends StatelessWidget {
1925
@override
2026
Widget build(BuildContext context) {
21-
return NStackAppOpen(
22-
platformOverride: AppOpenPlatform.android,
23-
child: Scaffold(
24-
appBar: AppBar(
25-
title: Text(context.localization.test.testDollarSign),
26-
),
27-
body: Center(
28-
child: MaterialButton(
29-
onPressed: () async =>
30-
{NStackScope.of(context).changeLanguage(Locale("de-DE"))},
31-
child: Text(
32-
"Selected locale: ${NStackScope.of(context).nstack.activeLanguage.name}"),
33-
),
27+
return Scaffold(
28+
appBar: AppBar(
29+
title: Text(context.localization.test.testDollarSign),
30+
),
31+
body: Center(
32+
child: MaterialButton(
33+
onPressed: () async =>
34+
{NStackScope.of(context).changeLanguage(Locale("de-DE"))},
35+
child: Text(
36+
"Selected locale: ${NStackScope.of(context).nstack.activeLanguage.name}"),
3437
),
3538
),
3639
);

example/lib/nstack.dart

Lines changed: 18 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,10 @@ class NStackScope extends InheritedWidget {
7777

7878
class NStackWidget extends StatefulWidget {
7979
final Widget child;
80+
final AppOpenPlatform? platformOverride;
81+
final VoidCallback? onComplete;
8082

81-
const NStackWidget({Key? key, required Widget child})
83+
const NStackWidget({Key? key, required Widget child, this.platformOverride, this.onComplete})
8284
: child = child,
8385
super(key: key);
8486

@@ -88,55 +90,38 @@ class NStackWidget extends StatefulWidget {
8890

8991
class NStackState extends State<NStackWidget> {
9092
final NStack<Localization> nstack = _nstack;
93+
bool _initializedNStack = false;
94+
95+
late Future<bool> _nstackInitFuture;
9196

9297
@override
9398
void initState() {
9499
super.initState();
95-
_nstack.initClientLocale();
100+
_nstackInitFuture = _nstack.init();
96101
}
97102

98103
changeLanguage(Locale locale) async {
99104
await _nstack.changeLocalization(locale).whenComplete(() => setState(() {}));
100105
}
101106

102-
Future<void> appOpen(Locale locale, {AppOpenPlatform? platformOverride}) async {
103-
await _nstack.appOpen(locale, platformOverride: platformOverride).whenComplete(() => setState(() {}));
104-
}
105-
106-
@override
107-
Widget build(BuildContext context) {
108-
return NStackScope(child: widget.child, state: this, nstack: this.nstack, checksum: nstack.checksum,);
109-
}
110-
}
111-
112-
class NStackAppOpen extends StatefulWidget {
113-
const NStackAppOpen({
114-
Key? key,
115-
required this.child,
116-
this.onComplete,
117-
this.platformOverride
118-
}) : super(key: key);
119-
120-
final Widget child;
121-
final VoidCallback? onComplete;
122-
final AppOpenPlatform? platformOverride;
123-
124-
@override
125-
_NStackAppOpenState createState() => _NStackAppOpenState();
126-
}
127-
128-
class _NStackAppOpenState extends State<NStackAppOpen> {
129-
bool _initializedNStack = false;
130-
131107
@override
132108
Widget build(BuildContext context) {
133109
if (!_initializedNStack) {
134-
NStackScope.of(context)
110+
_nstack
135111
.appOpen(Localizations.localeOf(context), platformOverride: widget.platformOverride)
136112
.whenComplete(() => widget.onComplete?.call());
137113
_initializedNStack = true;
138114
}
139-
return widget.child;
115+
116+
return FutureBuilder(
117+
future: _nstackInitFuture,
118+
builder: (context, snapshot) {
119+
if(snapshot.connectionState == ConnectionState.done) {
120+
return NStackScope(child: widget.child, state: this, nstack: this.nstack, checksum: nstack.checksum,);
121+
} else {
122+
return SizedBox();
123+
}
124+
});
140125
}
141126
}
142127

example/pubspec.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ packages:
302302
path: ".."
303303
relative: true
304304
source: path
305-
version: "0.4.1"
305+
version: "0.5.0"
306306
package_config:
307307
dependency: transitive
308308
description:

lib/nstack.dart

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,16 @@ class NStack<T> {
176176
}
177177
}
178178

179-
Future<String?> initClientLocale() async {
179+
Future<bool> init() async {
180+
try {
181+
await _initClientLocale();
182+
return true;
183+
} catch(e) {
184+
return false;
185+
}
186+
}
187+
188+
Future<String?> _initClientLocale() async {
180189
final prefs = await SharedPreferences.getInstance();
181190
if (prefs.containsKey(_prefsSelectedLocale)) {
182191
var languageTag = prefs.getString(_prefsSelectedLocale) ?? clientLocale?.toLanguageTag() ?? '';

lib/src/nstack_builder.dart

Lines changed: 18 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -249,8 +249,10 @@ class NStackScope extends InheritedWidget {
249249
250250
class NStackWidget extends StatefulWidget {
251251
final Widget child;
252+
final AppOpenPlatform? platformOverride;
253+
final VoidCallback? onComplete;
252254
253-
const NStackWidget({Key? key, required Widget child})
255+
const NStackWidget({Key? key, required Widget child, this.platformOverride, this.onComplete})
254256
: child = child,
255257
super(key: key);
256258
@@ -260,55 +262,38 @@ class NStackWidget extends StatefulWidget {
260262
261263
class NStackState extends State<NStackWidget> {
262264
final NStack<Localization> nstack = _nstack;
265+
bool _initializedNStack = false;
266+
267+
late Future<bool> _nstackInitFuture;
263268
264269
@override
265270
void initState() {
266271
super.initState();
267-
_nstack.initClientLocale();
272+
_nstackInitFuture = _nstack.init();
268273
}
269274
270275
changeLanguage(Locale locale) async {
271276
await _nstack.changeLocalization(locale).whenComplete(() => setState(() {}));
272277
}
273278
274-
Future<void> appOpen(Locale locale, {AppOpenPlatform? platformOverride}) async {
275-
await _nstack.appOpen(locale, platformOverride: platformOverride).whenComplete(() => setState(() {}));
276-
}
277-
278-
@override
279-
Widget build(BuildContext context) {
280-
return NStackScope(child: widget.child, state: this, nstack: this.nstack, checksum: nstack.checksum,);
281-
}
282-
}
283-
284-
class NStackAppOpen extends StatefulWidget {
285-
const NStackAppOpen({
286-
Key? key,
287-
required this.child,
288-
this.onComplete,
289-
this.platformOverride
290-
}) : super(key: key);
291-
292-
final Widget child;
293-
final VoidCallback? onComplete;
294-
final AppOpenPlatform? platformOverride;
295-
296-
@override
297-
_NStackAppOpenState createState() => _NStackAppOpenState();
298-
}
299-
300-
class _NStackAppOpenState extends State<NStackAppOpen> {
301-
bool _initializedNStack = false;
302-
303279
@override
304280
Widget build(BuildContext context) {
305281
if (!_initializedNStack) {
306-
NStackScope.of(context)
282+
_nstack
307283
.appOpen(Localizations.localeOf(context), platformOverride: widget.platformOverride)
308284
.whenComplete(() => widget.onComplete?.call());
309285
_initializedNStack = true;
310286
}
311-
return widget.child;
287+
288+
return FutureBuilder(
289+
future: _nstackInitFuture,
290+
builder: (context, snapshot) {
291+
if(snapshot.connectionState == ConnectionState.done) {
292+
return NStackScope(child: widget.child, state: this, nstack: this.nstack, checksum: nstack.checksum,);
293+
} else {
294+
return SizedBox();
295+
}
296+
});
312297
}
313298
}
314299

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: nstack
22
description: Nstack plugin for Flutter.
3-
version: 0.4.1
3+
version: 0.5.0
44
homepage: https://github.com/nstack-io/flutter-sdk
55

66
environment:

0 commit comments

Comments
 (0)