Skip to content
Snippets Groups Projects
Select Git revision
  • 0a8a29ef5241bd2562d45f17ac2d1f335e2951d8
  • master default protected
  • 61-upgrade-framework-and-dependencies
  • 44-improve-app-metadata
  • Release_0.10.0_57 protected
  • Release_0.9.2_56 protected
  • Release_0.9.1_55 protected
  • Release_0.9.0_54 protected
  • Release_0.8.0_53 protected
  • Release_0.7.0_52 protected
  • Release_0.6.0_51 protected
  • Release_0.5.3_50 protected
  • Release_0.5.2_49 protected
  • Release_0.5.1_48 protected
  • Release_0.5.0_47 protected
  • Release_0.4.1_46 protected
  • Release_0.4.0_45 protected
  • Release_0.3.1_44 protected
  • Release_0.3.0_43 protected
  • Release_0.2.1_42 protected
  • Release_0.2.0_41 protected
  • Release_0.1.19_40 protected
  • Release_0.1.18_39 protected
  • Release_0.1.17_38 protected
24 results

app_bar.dart

Blame
  • random_pick.dart 3.16 KiB
    import 'dart:async';
    import 'package:html/parser.dart';
    import 'package:html/dom.dart';
    import 'package:http/http.dart' as http;
    import 'dart:convert';
    import 'package:flutter/services.dart';
    import 'dart:math' show Random;
    import 'package:diacritic/diacritic.dart';
    import 'package:list_french_words/list_french_words.dart';
    
    class RandomPick {
      final String level;
      RandomPick(this.level);
    
      String _url;
      String _word;
      String _clue;
      final random = Random();
    
      init() async {
        switch (level) {
          case 'Thèmes':
            await wordFromLocal();
            break;
          case 'Avancé':
            _url = 'https://www.palabrasaleatorias.com/mots-aleatoires.php?fs=1&fs2=0&Submit=Nouveau+mot';
            await wordFromWeb(_url);
            break;
          case 'Junior':
            _url = 'https://www.palabrasaleatorias.com/mots-aleatoires.php?fs=1&fs2=1&Submit=Nouveau+mot';
            await wordFromWeb(_url);
            break;
          case 'Expert':
            await wordFromPackage();
            break;
          default:
            await wordFromLocal();
        }
      }
    
      Future<void> wordFromWeb(String url) async {
        try {
          var response = await http.Client().get(Uri.parse(url));
          if (response.statusCode == 200) {
            var html = parse(utf8.decode(response.bodyBytes));
            Element element = html.getElementsByTagName('div').last;
            String word = _validate(element.text);
            if (word == null) {
              throw Exception();
            }
            _word = word;
          } else {
            throw Exception();
          }
        } catch (e) {
          _word = null;
        }
      }
    
      String _validate(String word) {
        String wordToValidate = word.trim();
        if (wordToValidate.contains(' ')) {
          return null;
        }
        wordToValidate = removeDiacritics(wordToValidate);
        wordToValidate = wordToValidate.toUpperCase();
        wordToValidate = wordToValidate.replaceAll(RegExp('[0-9]'), '');
        wordToValidate = wordToValidate.replaceAll(RegExp('[^A-Z]'), '');
        if (wordToValidate.length < 3 || wordToValidate.length > 12) {
          return null;