Post pour l'autre là

Archives by Year - Month - Week - Day - Today

Post #196

EN

Post pour l'autre là

3 min read

Thursday, 24 December 2020 06:12:28 by Khodok

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/// main.dart
import 'package:escape_game/screen/password_screen.dart';
import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      home: PasswordScreen(),//MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;

  void _incrementCounter() {
    setState(() {
      _counter++;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text(
              'You have pushed the button this many times:',
            ),
            Text(
              '$_counter',
              style: Theme.of(context).textTheme.headline4,
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: Icon(Icons.add),
      ),
    );
  }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
/// keyboard.dart
import 'package:escape_game/provider/keys_provider.dart';
import 'package:flutter/material.dart';

class Keyboard extends StatefulWidget {
  @override
  _KeyboardState createState() => _KeyboardState();
}

class _KeyboardState extends State<Keyboard> {
  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        SingleChildScrollView(
          scrollDirection: Axis.horizontal,
          child: Row(
            children: [
              ...KeysProvider.keys,
            ],
          ),
        ),
        Row(
          children: [],
        ),
        Row(
          children: [],
        ),
      ],
    );
  }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
/// keyboard_key.dart
import 'package:escape_game/provider/keys_provider.dart';
import 'package:flutter/material.dart';

class KeyboardKey extends StatelessWidget {

  final String char;

  const KeyboardKey({@required this.char});

  @override
  Widget build(BuildContext context) {
    return RaisedButton(
      onPressed: () {
        print(char);
        KeysProvider.shuffleKeys();
      },
      child: Text(char),
    );
  }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
/// fake_form_field.dart
import 'package:escape_game/provider/keys_provider.dart';
import 'package:escape_game/utils/utils.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

class FakeTextField extends StatelessWidget {
  final String text;
  final Function onTap;
  final Color textColor;

  FakeTextField({@required this.text, @required this.onTap, @required this.textColor});

  @override
  Widget build(BuildContext context) {

    KeysProvider.initKeys();
    return GestureDetector(
      onTap: onTap,
      child: Container(
        width: MediaQuery.of(context).size.width - 10,
        decoration:
            BoxDecoration(border: Border.all(width: 1, color: Colors.grey)),
        child: Padding(
          padding: EdgeInsets.all(8),
          child: Text(
            text,
            style: TextStyle(
              color: textColor,
              fontSize: 12,
            ),
          ),
        ),
      ),
    );
  }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
/// utils.dart

import 'package:escape_game/widget/keyboard_key.dart';

class Utils {
  static String typedPassword = "test";

  static String typedPasswordStars() {
    String s = "";
    for (var i = 0; i < typedPassword.length; i++) {
      s += "*";
    }
    return s;
  }

  static List<String> chars;

  static void initChars() {
    chars = List<String>();
    for (var i = 33; i <= 126; i++) {
      chars.add(String.fromCharCode(i));
    }
    for (var i = 127761; i <= 127773; i++) {
      chars.add(String.fromCharCode(i));
    }
  }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/// password_screen.dart
import 'package:escape_game/utils/utils.dart';
import 'package:escape_game/widget/fake_form_field.dart';
import 'package:escape_game/widget/keyboard.dart';
import 'package:flutter/material.dart';

class PasswordScreen extends StatefulWidget {
  @override
  _PasswordScreenState createState() => _PasswordScreenState();
}

class _PasswordScreenState extends State<PasswordScreen> {
  bool _keyboardEnabled = false;

  void _enableKeyBoard() {
    setState(() {
      _keyboardEnabled = true;
    });
  }

  @override
  Widget build(BuildContext context) {
    Utils.initChars();
    return Scaffold(
      appBar: AppBar(),
      body: Column(
        children: [
          Container(
            height: _keyboardEnabled
                ? (MediaQuery.of(context).size.height -
                        MediaQuery.of(context).padding.top -
                        kToolbarHeight) *
                    2 /
                    3
                : MediaQuery.of(context).size.height -
                    MediaQuery.of(context).padding.top -
                    kToolbarHeight,
            child: Center(
              child: FakeTextField(
                text: _keyboardEnabled
                    ? Utils.typedPasswordStars()
                    : "Entrez le mot de passe",
                onTap: _enableKeyBoard,
                textColor: _keyboardEnabled ? Colors.black : Colors.grey,
              ),
            ),
          ),
          if (_keyboardEnabled)
            Container(
              height: (MediaQuery.of(context).size.height -
                      MediaQuery.of(context).padding.top -
                      kToolbarHeight) /
                  3,
              child: Keyboard(),
            ),
        ],
      ),
    );
  }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/// keys_provider.dart

import 'package:escape_game/utils/utils.dart';
import 'package:escape_game/widget/keyboard_key.dart';
import 'package:flutter/foundation.dart';
import 'package:provider/provider.dart';

class KeysProvider with ChangeNotifier {
  static List<KeyboardKey> keys;

  void initKeys() {
    keys = List<KeyboardKey>();
    for (String char in Utils.chars) {
      keys.add(KeyboardKey(
        char: char,
      ));
    }
  }

  void shuffleKeys() {
    keys.shuffle();
  }
}