Created
April 5, 2021 09:16
-
-
Save Dev-Owl/77c3dcc7d6dcfd5c26d2885e533503d6 to your computer and use it in GitHub Desktop.
Boot your app
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:caladrius/main.dart'; | |
import 'package:flutter/material.dart'; | |
import 'package:shared_preferences/shared_preferences.dart'; | |
typedef BootCompleted = Widget Function(); | |
class BootStrap extends StatefulWidget { | |
final List<BootstrapStep> steps; | |
final int currentIndex; | |
final BootCompleted bootCompleted; | |
const BootStrap( | |
this.steps, | |
this.bootCompleted, { | |
Key? key, | |
this.currentIndex = 0, | |
}) : super(key: key); | |
@override | |
_BootStrapState createState() => _BootStrapState(); | |
} | |
class _BootStrapState extends State<BootStrap> implements BootstrapController { | |
late int currentIndex; | |
bool bootRunning = true; | |
@override | |
void initState() { | |
super.initState(); | |
currentIndex = widget.currentIndex; | |
} | |
@override | |
Widget build(BuildContext context) { | |
return StreamBuilder<Widget>( | |
stream: work(), | |
builder: (c, snap) { | |
if (snap.hasData) { | |
return snap.data!; | |
} | |
return Scaffold( | |
body: Center( | |
child: CircularProgressIndicator(), | |
), | |
); | |
}); | |
} | |
Stream<Widget> work() async* { | |
while (bootRunning && | |
!(await widget.steps[currentIndex].stepRequired(preferences))) { | |
if (currentIndex + 1 < widget.steps.length) { | |
currentIndex++; | |
} else { | |
bootRunning = false; | |
} | |
} | |
if (bootRunning) { | |
yield widget.steps[currentIndex].buildStep(this); | |
} else { | |
yield widget.bootCompleted(); | |
} | |
} | |
@override | |
void procced() { | |
if (currentIndex + 1 < widget.steps.length) { | |
setState(() { | |
currentIndex++; | |
}); | |
} else { | |
setState(() { | |
bootRunning = false; | |
}); | |
} | |
} | |
@override | |
void stepback() { | |
if (currentIndex > 0) { | |
setState(() { | |
currentIndex--; | |
}); | |
} else { | |
setState(() { | |
bootRunning = false; | |
}); | |
} | |
} | |
} | |
abstract class BootstrapStep { | |
const BootstrapStep(); | |
Future<bool> stepRequired(SharedPreferences prefs); | |
Widget buildStep(BootstrapController controller); | |
} | |
abstract class BootstrapController { | |
void procced(); | |
void stepback(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment