Advertisement
Guest User

Untitled

a guest
Jul 18th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 12.97 KB | None | 0 0
  1. import 'dart:math';
  2. import 'package:maui/db/entity/concept.dart';
  3. import 'package:maui/games/single_game.dart';
  4. import 'package:meta/meta.dart';
  5. import 'package:flutter/material.dart';
  6. import 'package:tuple/tuple.dart';
  7. //import 'expansionTile.dart';
  8. import 'package:maui/db/entity/user.dart';
  9. import 'package:maui/state/app_state_container.dart';
  10. import 'package:maui/games/head_to_head_game.dart';
  11. import 'package:maui/loca.dart';
  12. import 'package:maui/components/controlled_expansion_tile.dart';
  13. // import 'package:maui/repos/concept_repo.dart';
  14. // import 'package:maui/screens/game_category_list_screen.dart';
  15.  
  16. class GameCategoryList extends StatefulWidget {
  17.   GameCategoryList(
  18.       {Key key,
  19.         @required this.gameCategories,
  20.         @required this.game,
  21.         @required this.gameMode,
  22.         @required this.concepts,
  23.         @required this.gameDisplay,
  24.         this.otherUser})
  25.       : super(key: key);
  26.   State<StatefulWidget> createState() => new _GameCategoryList();
  27.   final List<Tuple3<int, int, String>> gameCategories;
  28.   final String game;
  29.   GameMode gameMode;
  30.   GameDisplay gameDisplay;
  31.   User otherUser;
  32.   Map<int, Concept> concepts;
  33. }
  34.  
  35. class GameCategoryData {
  36.   int id;
  37.   int conceptId;
  38.   String name;
  39.   GameCategoryData(this.id, this.conceptId, this.name);
  40.   @override
  41.   String toString() {
  42.     return '{id: $id, conceptId: $conceptId, name: $name}';
  43.   }
  44.  
  45.   @override
  46.   bool operator ==(Object other) =>
  47.       identical(this, other) ||
  48.           other is GameCategoryData &&
  49.               runtimeType == other.runtimeType &&
  50.               id == other.id &&
  51.               conceptId == other.conceptId &&
  52.               name == other.name;
  53.  
  54.   @override
  55.   int get hashCode => id.hashCode ^ conceptId.hashCode ^ name.hashCode;
  56. }
  57.  
  58. class _GameCategoryList extends State<GameCategoryList> {
  59.   static final List<Color> colorsCodes = [
  60.     Color(0XFF48AECC),
  61.     Color(0XFFE66796),
  62.     Color(0XFFFF7676),
  63.     Color(0XFFEDC23B),
  64.     Color(0XFFAD85F9),
  65.     Color(0XFF77DB65),
  66.     Color(0XFF66488C),
  67.     Color(0XFFDD6154),
  68.     Color(0XFFFFCE73),
  69.     Color(0XFFD64C60),
  70.     Color(0XFFDD4785),
  71.     Color(0XFF52C5CE),
  72.     Color(0XFFF97658),
  73.     Color(0XFFA46DBA),
  74.     Color(0XFFA292FF),
  75.     Color(0XFFFF8481),
  76.     Color(0XFF35C9C1),
  77.     Color(0XFFEDC23B),
  78.     Color(0XFF42AD56),
  79.     Color(0XFFF47C5D),
  80.     Color(0XFF77DB65),
  81.     Color(0XFF57DBFF),
  82.     Color(0XFFEB706F),
  83.     Color(0XFF48AECC),
  84.     Color(0XFFFFC729),
  85.     Color(0XFF30C9E2),
  86.     Color(0XFFA1EF6F),
  87.   ];
  88.   static final List<Color> tileColors = [];
  89.   int count = 0;
  90.   bool isLoading = false;
  91.   List<GameCategoryData> gameCategoryData;
  92.   Map<int, List<GameCategoryData>> conceptIdMap;
  93.   //hold globalKey for current expandedTile
  94.   GlobalKey<ControlledExpansionTileState> currentExpandedTile;
  95.  
  96.   @override
  97.   void initState() {
  98.     super.initState();
  99.     gameCategoryData = widget.gameCategories.map((tuple3) {
  100.       return new GameCategoryData(tuple3.item1, tuple3.item2, tuple3.item3);
  101.     }).toList();
  102.     conceptIdMap = {};
  103.     gameCategoryData.forEach((data) {
  104.       conceptIdMap
  105.           .putIfAbsent(data.conceptId, () => new List<GameCategoryData>())
  106.           .add(data);
  107.     });
  108.     conceptIdMap.forEach((key, value) {
  109.       print("$key - ${value.length}");
  110.       print('value is $value');
  111.     });
  112.     int categoriesLength = widget.gameCategories.length;
  113.     for (int i = 0; i < categoriesLength + 1; i++) {
  114.       if (count == 26) count = 0;
  115.       tileColors.add(colorsCodes[count]);
  116.       count++;
  117.     }
  118.   }
  119.  
  120.   @override
  121.   Widget build(BuildContext context) {
  122.     if (isLoading) {
  123.       return new Center(
  124.           child: new SizedBox(
  125.             width: 20.0,
  126.             height: 20.0,
  127.             child: new CircularProgressIndicator(),
  128.           ));
  129.     }
  130.     Orientation orientation = MediaQuery.of(context).orientation;
  131.     Size media = MediaQuery.of(context).size;
  132.     int j = 0;
  133.     return new CustomScrollView(
  134.       primary: true,
  135.       shrinkWrap: false,
  136.       slivers: <Widget>[
  137.         new SliverAppBar(
  138.             backgroundColor:
  139.             SingleGame.gameColors[widget.game][0] ?? Colors.amber,
  140.             pinned: true,
  141.             expandedHeight: orientation == Orientation.portrait
  142.                 ? media.height * .25
  143.                 : media.height * .5,
  144.             title: new Text(Loca.of(context).intl(widget.game)),
  145.             flexibleSpace: new FlexibleSpaceBar(
  146.               background: new Stack(children: <Widget>[
  147.                 new Container(
  148.                   decoration: new BoxDecoration(
  149.                     image: new DecorationImage(
  150.                       image: new AssetImage(
  151.                           "assets/background_image/${widget.game}_big.png"),
  152.                       fit: BoxFit.fill,
  153.                     ),
  154.                   ),
  155.                 ),
  156.                 Container(
  157.                     padding: EdgeInsets.only(bottom: 20.0),
  158.                     child: Center(
  159.                       child: new Hero(
  160.                         tag: 'assets/hoodie/${widget.game}.png',
  161.                         child: new Image.asset(
  162.                           'assets/hoodie/${widget.game}.png',
  163.                           scale: .4,
  164.                         ),
  165.                       ),
  166.                     ))
  167.               ]),
  168.               // centerTitle: true,
  169.             )),
  170.         new SliverList(
  171.             delegate: new SliverChildListDelegate(_buildCategoriesButtons())),
  172.         new SliverToBoxAdapter(
  173.           child: new Container(height: 2.0, color: Colors.yellow),
  174.         ),
  175.       ],
  176.     );
  177.   }
  178.  
  179.   List<Widget> _buildCategoriesButtons() {
  180.     List<Widget> buttons = [];
  181.     int colorIndex = 0;
  182.     conceptIdMap.forEach((conceptId, list) {
  183.       String mainCategoryName = widget.concepts[conceptId].name;
  184.       if (list.length == 1) {
  185.         buttons.add(_buildButtonCategory(
  186.             mainCategoryName, list.first.id, tileColors[colorIndex++]));
  187.       } else {
  188.         GlobalKey<ControlledExpansionTileState> expansionKey = new GlobalObjectKey("tile-$conceptId");
  189.  
  190.         buttons.add(Container(
  191.           color: tileColors[colorIndex++],
  192.           child: new ControlledExpansionTile(
  193.             key: expansionKey,
  194.             onExpansionChanged : (bool value) {
  195.               if (value) {
  196.                 if (currentExpandedTile != null) {
  197.                   currentExpandedTile.currentState?.handleTap();
  198.                 }
  199.                 currentExpandedTile = expansionKey;
  200.               } else {
  201.                 currentExpandedTile = null;
  202.               }
  203.             },
  204.             title: Container(
  205.             height: 154.0,
  206.             child: Padding(
  207.               padding: const EdgeInsets.fromLTRB(0.0, 60.0, 0.0, 0.0),
  208.               child: new Text(mainCategoryName,
  209.                   style: TextStyle(
  210.                       letterSpacing: 2.0,
  211.                       color: Colors.white,
  212.                       fontSize: 30.0,
  213.                       fontWeight: FontWeight.bold)),
  214.             ),
  215.           ),
  216.           children: list.map((gameCategoryData) {
  217.             return _buildButtonchildren(gameCategoryData.name,
  218.                 gameCategoryData.id, tileColors[colorIndex - 1]);
  219.           }).toList(),
  220.         ),
  221.         ));
  222.     }
  223.     });
  224.     return buttons;
  225.   }
  226.  
  227.   Widget _buildButtonchildren(
  228.       String mainCategoryName, int gameCategoryId, Color color) {
  229.     return new Container(
  230.         height: 154.0,
  231.         color: color,
  232.         child: Container(
  233.           decoration: BoxDecoration(
  234.               border: BorderDirectional(
  235.                   bottom: BorderSide(width: 2.0, color: Colors.black.withOpacity(0.2)))),
  236.           child: ListTile(
  237.             title: Padding(
  238.               padding: const EdgeInsets.fromLTRB(0.0, 60.0, 0.0, 0.0),
  239.               child: Text(mainCategoryName,
  240.                   style: TextStyle(
  241.                       color: Colors.white,
  242.                       fontSize: 30.0,
  243.                       fontWeight: FontWeight.bold)),
  244.             ),
  245.             onTap: () => goToGame(context, widget.game, gameCategoryId,
  246.                 widget.gameDisplay, widget.gameMode,
  247.                 otherUser: widget.otherUser),
  248.           ),
  249.         ));
  250.   }
  251.  
  252.   Widget _buildButtonCategory(
  253.       String mainCategoryName, int gameCategoryId, Color color) {
  254.     return new Container(
  255.       height: 154.0,
  256.       color: color,
  257.       child: ListTile(
  258.         title: new Container(
  259.             child: Padding(
  260.               padding: const EdgeInsets.fromLTRB(0.0, 60.0, 0.0, 0.0),
  261.               child: Text(mainCategoryName,
  262.                   style: TextStyle(
  263.                       color: Colors.white,
  264.                       fontSize: 30.0,
  265.                       fontWeight: FontWeight.bold)),
  266.             )),
  267.         onTap: () => goToGame(context, widget.game, gameCategoryId,
  268.             widget.gameDisplay, widget.gameMode,
  269.             otherUser: widget.otherUser),
  270.       ),
  271.     );
  272.   }
  273.  
  274.   void goToGame(BuildContext context, String gameName, int gameCategoryId,
  275.       GameDisplay gameDisplay, GameMode gameMode,
  276.       {User otherUser}) {
  277.     Random random = new Random();
  278.     var gameConfig = new GameConfig(
  279.         gameCategoryId: gameCategoryId,
  280.         questionUnitMode: UnitMode.values[random.nextInt(3)],
  281.         answerUnitMode: UnitMode.values[random.nextInt(3)],
  282.         level: random.nextInt(10) + 1);
  283.     print('goToGame: $gameName $gameCategoryId, $gameDisplay, $gameMode');
  284.     switch (gameDisplay) {
  285.       case GameDisplay.single:
  286.         gameMode == GameMode.iterations
  287.             ? Navigator.of(context).push(
  288.           MaterialPageRoute<Null>(builder: (BuildContext context) {
  289.             gameConfig.gameDisplay = GameDisplay.single;
  290.             gameConfig.amICurrentPlayer = true;
  291.             gameConfig.myScore = 0;
  292.             gameConfig.myUser =
  293.                 AppStateContainer.of(context).state.loggedInUser;
  294.             gameConfig.otherScore = 0;
  295.             gameConfig.orientation = MediaQuery.of(context).orientation;
  296.             return new SingleGame(
  297.               gameName,
  298.               gameMode: GameMode.iterations,
  299.               gameConfig: gameConfig,
  300.             );
  301.           }),
  302.         )
  303.             : Navigator.of(context).push(
  304.           MaterialPageRoute<Null>(builder: (BuildContext context) {
  305.             gameConfig.gameDisplay = GameDisplay.single;
  306.             gameConfig.orientation = MediaQuery.of(context).orientation;
  307.             gameConfig.myUser =
  308.                 AppStateContainer.of(context).state.loggedInUser;
  309.             return new SingleGame(
  310.               gameName,
  311.               gameMode: GameMode.timed,
  312.               gameConfig: gameConfig,
  313.             );
  314.           }),
  315.         );
  316.         break;
  317.       case GameDisplay.localTurnByTurn:
  318.         Navigator.of(context).push(
  319.           MaterialPageRoute<Null>(builder: (BuildContext context) {
  320.             gameConfig.gameDisplay = GameDisplay.localTurnByTurn;
  321.             gameConfig.amICurrentPlayer = true;
  322.             gameConfig.myUser =
  323.                 AppStateContainer.of(context).state.loggedInUser;
  324.             gameConfig.otherUser = otherUser;
  325.             gameConfig.myScore = 0;
  326.             gameConfig.otherScore = 0;
  327.             gameConfig.orientation = MediaQuery.of(context).orientation;
  328.             return new SingleGame(
  329.               gameName,
  330.               gameMode: GameMode.iterations,
  331.               gameConfig: gameConfig,
  332.             );
  333.           }),
  334.         );
  335.         break;
  336.       case GameDisplay.networkTurnByTurn:
  337.         Navigator.of(context).push(
  338.           MaterialPageRoute<Null>(builder: (BuildContext context) {
  339.             gameConfig.gameDisplay = GameDisplay.networkTurnByTurn;
  340.             gameConfig.amICurrentPlayer = true;
  341.             gameConfig.myUser =
  342.                 AppStateContainer.of(context).state.loggedInUser;
  343.             gameConfig.otherUser = otherUser;
  344.             gameConfig.myScore = 0;
  345.             gameConfig.otherScore = 0;
  346.             gameConfig.orientation = MediaQuery.of(context).orientation;
  347.             return new SingleGame(
  348.               gameName,
  349.               gameMode: GameMode.iterations,
  350.               gameConfig: gameConfig,
  351.             );
  352.           }),
  353.         );
  354.         break;
  355.       case GameDisplay.myHeadToHead:
  356.         gameConfig.orientation = Orientation.landscape;
  357.         gameConfig.myUser = AppStateContainer.of(context).state.loggedInUser;
  358.         gameConfig.otherUser = otherUser;
  359.         gameMode == GameMode.iterations
  360.             ? Navigator.of(context).push(MaterialPageRoute<Null>(
  361.           builder: (BuildContext context) => new HeadToHeadGame(
  362.             gameName,
  363.             gameMode: GameMode.iterations,
  364.             gameConfig: gameConfig,
  365.           ),
  366.         ))
  367.             : Navigator.of(context).push(MaterialPageRoute<Null>(
  368.           builder: (BuildContext context) => new HeadToHeadGame(
  369.             gameName,
  370.             gameMode: GameMode.timed,
  371.             gameConfig: gameConfig,
  372.           ),
  373.         ));
  374.     }
  375.   }
  376. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement