/* * @Author : Linloir * @Date : 2022-03-05 21:40:51 * @LastEditTime : 2022-03-06 11:42:36 * @Description : Validation Provider class */ import 'package:flutter/material.dart'; import './event_bus.dart'; enum InputType { singleCharacter, inputConfirmation } class InputNotification extends Notification { const InputNotification({required this.type, required this.msg}); final InputType type; final String msg; } class ValidationProvider extends StatefulWidget { const ValidationProvider({Key? key, required this.child}) : super(key: key); final Widget child; @override State createState() => _ValidationProviderState(); } class _ValidationProviderState extends State { String answer = "WORDLE"; Set letterSet = {'W', 'O', 'R', 'D', 'L', 'E'}; String curAttempt = ""; int curAttemptCount = 0; bool acceptInput = true; void _newGame(String answer) { this.answer = answer; letterSet.clear(); letterSet.addAll(answer.split('')); curAttempt = ""; curAttemptCount = 0; acceptInput = true; } void _onGameWin() { acceptInput = false; _showResult(true); } void _onGameLoose() { acceptInput = false; _showResult(false); } void _showResult(bool result) { showDialog( context: context, builder: (context) { return AlertDialog( title: const Text('Result'), content: Text(result ? "Won" : "Lost"), ); } ); } bool _isWordValidate(String word) { return true; } @override void initState(){ super.initState(); mainBus.onBus(event: "NewGame", onEvent: (args) => _newGame(args as String)); mainBus.onBus(event: "Result", onEvent: (args) => args as bool ? _onGameWin() : _onGameLoose()); } @override Widget build(BuildContext context) { return NotificationListener( child: widget.child, onNotification: (noti) { print('Get'); if(noti.type == InputType.inputConfirmation){ if(curAttempt.length < 5) { print('failed'); //Not enough return true; } else { //Check validation if(_isWordValidate(curAttempt)) { //emit current attempt mainBus.emit( event: "Attempt", args: [ for(int i = 0; i < 5; i++) if(curAttempt[i] == answer[i]) 1 else if(letterSet.lookup(curAttempt[i]) != null) 2 else -1 ], ); print('Emited'); curAttempt = ""; curAttemptCount++; } else{ showDialog( context: context, builder: (context) { return AlertDialog( title: const Text('Info'), content: const Text('Not a word!'), actions: [ TextButton( child: const Text('OK'), onPressed: () => Navigator.of(context).pop(), ) ], ); } ); } } } else{ if(acceptInput && curAttempt.length < 5) { curAttempt += noti.msg; } } return true; }, ); } }