Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import 'package:flutter/material.dart';
- import 'package:after_layout/after_layout.dart';
- import 'dart:async';
- import 'questions.dart';
- import 'life_history.dart';
- import 'alert.dart';
- // Textstyle of the heading used in Age
- TextStyle heading = TextStyle(
- fontWeight: FontWeight.bold,
- fontSize: 16,
- );
- // Textstyle of the heading used in Month
- TextStyle subhead = TextStyle(
- fontStyle: FontStyle.italic,
- );
- class LifeTab extends StatefulWidget {
- LifeTab({Key key}) : super(key: key);
- @override
- _LifeTabState createState() => _LifeTabState();
- }
- class _LifeTabState extends State<LifeTab> with AfterLayoutMixin<LifeTab>{
- // "historyList" => Past events retrieved from db
- // "time" => 0-12 in year 0, 13-24 in year 1 ...
- // "processedTime" => only for headings
- // "processedAge" => only for headings
- List<Widget> historyList = [];
- int time = -1;
- int processedTime = -1;
- int processedAge = -1;
- ScrollController _scrollController = new ScrollController();
- // Reads previous history from database and formats them
- @override
- void initState(){
- super.initState();
- // Read history
- getAllHistory().then((history){
- setState((){
- // Go through every entry in our history db
- for (int i = 0; i<history.length; i++){
- var entry = history[i];
- var outcome;
- if (time < entry.time){
- outcome = RichText(
- text: TextSpan(
- style: DefaultTextStyle.of(context).style,
- children: <TextSpan>[
- TextSpan(text: 'Month '+(entry.time%12).toString() + " — ",
- style: subhead,),
- TextSpan(text: entry.outcome,),
- ],
- ),
- );
- } else {
- outcome = Text(entry.outcome);
- }
- // If time of entry % 12 is 0, a.k.a a year,
- if (entry.time % 12 == 0){
- int age = i~/12;
- // Obtain the year and check if we have processed that age heading
- if (age>processedAge){
- historyList.add(Text(
- '\nAge: '+age.toString()+' years old',
- style: heading,)
- );
- // After we have processed the heading, add to addedAge to show we processed that age
- processedAge += 1;
- }
- }
- // Make sure we update the current time to be the latest entry time
- time = entry.time;
- historyList.add(outcome);
- }
- });
- _scrollController.jumpTo(_scrollController.position.maxScrollExtent);
- });
- }
- @override
- void afterFirstLayout(BuildContext context){
- //Timer(Duration(milliseconds: 100), () => _scrollController.animateTo(_scrollController.position.maxScrollExtent, duration: const Duration(milliseconds: 100), curve: Curves.easeOut));
- _scrollController.jumpTo(_scrollController.position.maxScrollExtent);
- scheduleMicrotask(() => _scrollController.jumpTo(_scrollController.position.maxScrollExtent));
- }
- Widget monthHeading(text){
- if (processedTime < time){
- processedTime += 1;
- return RichText(
- text: TextSpan(
- style: DefaultTextStyle.of(context).style,
- children: <TextSpan>[
- TextSpan(
- text: 'Month '+(time%12).toString() + " — ",
- style: subhead,),
- TextSpan(text: text,),
- ],
- ),
- );
- } else {
- return Text(text);
- }
- }
- void questionRecursive({id, text}) async {
- if (text != null){
- var split = text.split('|');
- simpleAlert(context, split[0], split[1]);
- return;
- }
- Question data;
- if (id == null){
- data = await getRandomQuestion(time);
- } else {
- data = await getQuestionById(id);
- }
- if (data == null){
- insertHistory(History(
- outcome: "Nothing interesting happened.\n",
- time: time,
- questionId: -1,
- ));
- setState((){historyList.add(monthHeading("Nothing interesting happened.\n"));});
- return;
- }
- var choices = data.choices.split(';');
- var consequences = data.consequences.split(';');
- List<Function> consequenceList = [];
- for (int i = 0; i<choices.length;i++){
- var split = consequences[i].split(">");
- String textbox = split[2];
- String historyText = split[0];
- var formatted = historyText;
- Function onPress;
- if (textbox.substring(0,6) == "GO TO "){
- onPress = (){
- Navigator.pop(context);
- insertHistory(History(
- outcome: formatted,
- time: time,
- questionId: data.id,
- ));
- setState((){historyList.add(monthHeading(formatted));});
- questionRecursive(id: textbox.substring(6));
- };
- } else {
- onPress = (){
- Navigator.pop(context);
- formatted = historyText + "\n";
- insertHistory(History(
- outcome: formatted,
- time: time,
- questionId: data.id,
- ));
- setState((){historyList.add(monthHeading(formatted));});
- questionRecursive(text: textbox);
- };
- }
- consequenceList.add(onPress);
- }
- var question = data.question.split('|');
- multiChoiceAlert(context, question[0], question[1], choices, consequenceList);
- }
- void onPlusTap() async{
- time += 1;
- setState(() {
- if (time % 12 == 0 && (time~/12) > processedAge){
- historyList.add(Text(
- "\nAge "+(time~/12).toString()+" years old",
- style: heading,
- ));
- processedAge += 1;
- }
- questionRecursive();
- });
- _scrollController.animateTo(_scrollController.position.maxScrollExtent, duration: const Duration(milliseconds: 500), curve: Curves.easeOut);
- }
- Widget _buildEvent(BuildContext context, int index){
- return historyList[index];
- }
- @override
- Widget build(BuildContext context) {
- // This method is rerun every time setState is called, for instance as done
- // by the _incrementCounter method above.
- //
- // The Flutter framework has been optimized to make rerunning build methods
- // fast, so that you can just rebuild anything that needs updating rather
- // than having to individually change instances of widgets.
- return Scaffold(
- body: ListView.builder(
- controller: _scrollController,
- padding: EdgeInsets.only(
- top: 40,
- left: 20,
- right: 20,
- bottom: 400,
- ),
- shrinkWrap: true,
- itemCount: historyList.length,
- itemBuilder: _buildEvent,
- ),
- floatingActionButton: FloatingActionButton(
- onPressed: onPlusTap,
- tooltip: 'Age up',
- child: Icon(Icons.add),
- ), // This trailing comma makes auto-formatting nicer for build methods.
- );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement