Advertisement
Guest User

Untitled

a guest
Jun 21st, 2018
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 5.76 KB | None | 0 0
  1. import 'package:flutter/material.dart';
  2. import 'package:maui/db/entity/unit.dart';
  3. import 'package:maui/games/single_game.dart';
  4. import 'package:maui/repos/unit_repo.dart';
  5. import 'package:maui/state/app_state_container.dart';
  6. import 'package:maui/state/app_state.dart';
  7. import 'package:meta/meta.dart';
  8. import 'dart:math';
  9.  
  10. import 'flash_card.dart';
  11.  
  12. class UnitButton extends StatefulWidget {
  13.   final String text;
  14.   final VoidCallback onPress;
  15.   final UnitMode unitMode;
  16.   final bool disabled;
  17.   final bool highlighted;
  18.   final bool primary;
  19.   final bool showHelp;
  20.   final String bgImage;
  21.   final double maxWidth;
  22.   final double maxHeight;
  23.   final double fontSize;
  24.  
  25.   UnitButton(
  26.       {Key key,
  27.       @required this.text,
  28.       this.onPress,
  29.       this.disabled = false,
  30.       this.showHelp = true,
  31.       this.highlighted = false,
  32.       this.primary = true,
  33.       this.bgImage,
  34.       this.maxHeight,
  35.       this.maxWidth,
  36.       this.fontSize,
  37.       this.unitMode = UnitMode.text})
  38.       : super(key: key);
  39.  
  40.   @override
  41.   _UnitButtonState createState() {
  42.     return new _UnitButtonState();
  43.   }
  44.  
  45.   static void saveButtonSize(
  46.       BuildContext context, int maxChars, double maxWidth, double maxHeight) {
  47.     AppState state = AppStateContainer.of(context).state;
  48.     final fontWidthFactor = maxChars == 1 ? 1.1 : 0.7;
  49.     final fontSizeByWidth = maxWidth / (maxChars * fontWidthFactor);
  50.     final fontSizeByHeight = maxHeight / 1.8;
  51.     state.buttonFontSize = min(fontSizeByHeight, fontSizeByWidth);
  52.     state.buttonRadius = min(maxWidth, maxHeight) / 8.0;
  53.  
  54.     state.buttonWidth = (maxChars == 1)
  55.         ? min(maxWidth, maxHeight)
  56.         : state.buttonFontSize * maxChars * 0.7;
  57.     state.buttonHeight = (maxChars == 1)
  58.         ? min(maxWidth, maxHeight)
  59.         : min(maxHeight, maxWidth * 0.75);
  60.     print(
  61.         'width: ${state.buttonWidth} height: ${state.buttonHeight} maxWidth: ${maxWidth} maxHeight: ${maxHeight} maxChars: ${maxChars}');
  62.     print(
  63.         'fontsize: ${state.buttonFontSize} fontSizeByWidth: ${fontSizeByWidth} fontSizeByHeight ${fontSizeByHeight}');
  64.   }
  65. }
  66.  
  67. class _UnitButtonState extends State<UnitButton> {
  68.   Unit _unit;
  69.   bool _isLoading = true;
  70.   UnitMode get _unitMode => widget.unitMode;
  71.  
  72.   @override
  73.   void initState() {
  74.     super.initState();
  75. //    _unitMode = widget.unitMode;
  76.     print('initState');
  77.     _getData();
  78.   }
  79.  
  80.   void _getData() async {
  81.     if (_unitMode == UnitMode.audio || _unitMode == UnitMode.image) {
  82.       _unit = await new UnitRepo().getUnit(widget.text.toLowerCase());
  83.       print(_unit);
  84.       if ((_unitMode == UnitMode.audio && (_unit.sound?.length ?? 0) == 0) ||
  85.           (_unitMode == UnitMode.image && (_unit.image?.length ?? 0) == 0)) {
  86.         _unitMode = UnitMode.text;
  87.       }
  88.     }
  89.     setState(() => _isLoading = false);
  90.   }
  91.  
  92.   @override
  93.   Widget build(BuildContext context) {
  94.     return widget.showHelp
  95.         ? new GestureDetector(
  96.             onLongPress: () {
  97.               AppStateContainer.of(context).play(widget.text.toLowerCase());
  98.               if (_unitMode != UnitMode.audio) {
  99.                 AppStateContainer.of(context).display(context, widget.text.toLowerCase());  
  100.               }
  101.             },
  102.             child: _buildButton(context))
  103.         : _buildButton(context);
  104.   }
  105.  
  106.   Widget _buildButton(BuildContext context) {
  107.     AppState state = AppStateContainer.of(context).state;
  108.     return Container(
  109.         constraints: BoxConstraints.tightFor(
  110.             height: widget.maxHeight ?? state.buttonHeight,
  111.             width: widget.maxWidth ?? state.buttonWidth),
  112.         decoration: new BoxDecoration(
  113.             image: widget.bgImage != null
  114.                 ? new DecorationImage(
  115.                     image: new AssetImage(widget.bgImage), fit: BoxFit.contain)
  116.                 : null),
  117.         child: FlatButton(
  118.             color: widget.highlighted
  119.                 ? Theme.of(context).primaryColor
  120.                 : Colors.transparent,
  121.             splashColor: Theme.of(context).accentColor,
  122.             highlightColor: Theme.of(context).accentColor,
  123.             disabledColor: Color(0xFFDDDDDD),
  124.             onPressed: widget.disabled
  125.                 ? null
  126.                 : () {
  127.                     AppStateContainer
  128.                         .of(context)
  129.                         .play(widget.text.toLowerCase());
  130.                     widget.onPress();
  131.                   },
  132.             padding: EdgeInsets.all(0.0),
  133.             shape: new RoundedRectangleBorder(
  134.                 side: new BorderSide(
  135.                     color: widget.disabled
  136.                         ? Color(0xFFDDDDDD)
  137.                         : widget.primary
  138.                             ? Theme.of(context).primaryColor
  139.                             : Colors.white,
  140.                     width: 4.0),
  141.                 borderRadius: BorderRadius
  142.                     .all(Radius.circular(state.buttonRadius ?? 8.0))),
  143.             child: _buildUnit(widget.fontSize ?? state.buttonFontSize)));
  144.   }
  145.  
  146.   Widget _buildUnit(double fontSize) {
  147.     print("UnitButton _buildUnit ${_unitMode}  ${widget.text}");
  148.     if (_unitMode == UnitMode.audio) {
  149.       return new Icon(Icons.volume_up);
  150.     } else if (_unitMode == UnitMode.image) {
  151.       return _isLoading
  152.           ? new Container()
  153.           : new Image.asset('assets/dict/${widget.text.toLowerCase()}.png');
  154.     } else {
  155.       return Center(
  156.           child: Text(widget.text,
  157.               style: new TextStyle(
  158.                   color: widget.highlighted || !widget.primary
  159.                       ? Colors.white
  160.                       : Theme
  161.                       .of(context)
  162.                       .primaryColor,
  163.                   fontSize: fontSize)));
  164.     }
  165.   }
  166.  
  167.   @override
  168.   void dispose() {
  169.     super.dispose();
  170.   }
  171. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement