Created
November 27, 2019 13:52
-
-
Save zamahaka/aa76378d160743daaab7210ba76a1f37 to your computer and use it in GitHub Desktop.
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:flutter/material.dart'; | |
import 'package:speakee_flutter/configs/speakee_calculator.dart'; | |
import 'package:speakee_flutter/configs/speakee_colors.dart'; | |
import 'package:speakee_flutter/data/model/word.dart'; | |
import 'package:speakee_flutter/factories/build_a_word_presenter_factory.dart'; | |
import 'package:speakee_flutter/lessons/build_a_word/build_a_word_contract.dart'; | |
import 'package:speakee_flutter/lessons/remove_a_word/widgets/word_widget.dart'; | |
import 'package:speakee_flutter/lessons/sentence_building/widgets/draggable_word_card.dart'; | |
import 'package:speakee_flutter/lessons/sentence_building/widgets/word_card_model.dart'; | |
import 'package:speakee_flutter/utils/shake_view.dart'; | |
import 'package:speakee_flutter/utils/single_touch_recognizer.dart'; | |
class BuildAWord extends StatefulWidget { | |
final Widget topWidget; | |
final List<String> choices; | |
final Word word; | |
final VoidCallback onFinishListener; | |
BuildAWord({ | |
this.topWidget, | |
this.word, | |
this.choices, | |
this.onFinishListener, | |
}); | |
@override | |
_BuildAWordState createState() => _BuildAWordState(); | |
} | |
class _BuildAWordState extends State<BuildAWord> implements BuildAWordView { | |
ShakeController _shakeController; | |
BuildAWordPresenter _presenter; | |
List<Choice> _choices; | |
String _input; | |
@override | |
void initState() { | |
_presenter = BuildAWordPresenterFactory().createPresenter(this); | |
_buildWordPeaces(); | |
super.initState(); | |
} | |
@override | |
void didUpdateWidget(BuildAWord oldWidget) { | |
_buildWordPeaces(); | |
super.didUpdateWidget(oldWidget); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return RawGestureDetector( | |
behavior: HitTestBehavior.opaque, | |
gestures: <Type, GestureRecognizerFactory>{ | |
SingleTouchRecognizer: | |
GestureRecognizerFactoryWithHandlers<SingleTouchRecognizer>( | |
() => SingleTouchRecognizer(), | |
(SingleTouchRecognizer instance) {}, | |
), | |
}, | |
child: Column( | |
crossAxisAlignment: CrossAxisAlignment.stretch, | |
children: <Widget>[ | |
Expanded( | |
flex: 29, | |
child: widget.topWidget, | |
), | |
SizedBox(height: SpeakeeCalculator.height(45.0)), | |
Expanded( | |
flex: 30, | |
child: new SingleChildScrollView( | |
child: Padding( | |
padding: EdgeInsets.symmetric( | |
horizontal: SpeakeeCalculator.width(12.0), | |
), | |
child: Stack( | |
children: [ | |
Padding( | |
padding: | |
EdgeInsets.only(top: SpeakeeCalculator.height(56)), | |
child: Center( | |
child: Divider( | |
thickness: 1, | |
indent: SpeakeeCalculator.width(5), | |
endIndent: SpeakeeCalculator.width(5), | |
), | |
), | |
), | |
DragTarget<WordCard>( | |
builder: (context, List<WordCard> candidateData, | |
rejectedData) { | |
return _mainWordCardList.isEmpty | |
? Container( | |
width: MediaQuery.of(context).size.width, | |
height: SpeakeeCalculator.height(200), | |
color: Colors.transparent, | |
) | |
: _buildMainWordCardPeaces(); | |
}, | |
), | |
], | |
), | |
), | |
), | |
), | |
Expanded( | |
flex: 36, | |
child: Container( | |
decoration: BoxDecoration( | |
color: Colors.white, | |
border: Border.all(color: SpeakeeColors.borderColor, width: 1), | |
boxShadow: [ | |
BoxShadow( | |
color: SpeakeeColors.buttonShadowColor, | |
blurRadius: 8.0, | |
) | |
], | |
), | |
child: Padding( | |
padding: EdgeInsets.all(SpeakeeCalculator.width(12.0)), | |
child: new SingleChildScrollView( | |
child: __buildBottomWordCardPeaces(_choices.where(Choice.isActive)), | |
), | |
), | |
), | |
) | |
], | |
), | |
); | |
} | |
@override | |
void showShakeAnimation() { | |
_shakeController.shake(); | |
} | |
void _onFinish() { | |
widget.onFinishListener(); | |
} | |
void _onWordCardTapped(int index, ShakeController controller) { | |
_shakeController = controller; | |
_presenter.checkWordCard( | |
_mainWordCardList.length, _bottomWordCardList[index]); | |
} | |
Widget __buildBottomWordCardPeaces(List<Choice> choices) { | |
choices.map((choice) { | |
DraggableWordCard( | |
word: choice, | |
onWordTapped: (choice, controller) { | |
_onWordCardTapped(choice, controller); | |
}, | |
isActive: active.contains(choice), | |
); | |
}); | |
} | |
Widget _buildMainWordCardPeaces(List<Choice> choices) { | |
List<WordCard> wordCards = List<WordCard>(); | |
_mainWordCardList.forEach((wordCard) { | |
wordCards.add( | |
WordCard( | |
word: wordCard.word, | |
wordIndex: wordCard.wordIndex, | |
positionInSentence: wordCard.positionInSentence, | |
onWordTapped: (index, position) => | |
wordCard.onWordTapped(index, position, null), | |
isActive: wordCard.isActive, | |
borderColor: wordCard.borderColor, | |
), | |
); | |
}); | |
return Wrap(children: wordCards); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment