Post Detail

Post #196

Post pour l'autre là

 1/// main.dart
 2import 'package:escape_game/screen/password_screen.dart';
 3import 'package:flutter/material.dart';
 4
 5void main() {
 6  runApp(MyApp());
 7}
 8
 9class MyApp extends StatelessWidget {
10  // This widget is the root of your application.
11  @override
12  Widget build(BuildContext context) {
13    return MaterialApp(
14      title: 'Flutter Demo',
15      theme: ThemeData(
16        primarySwatch: Colors.blue,
17        visualDensity: VisualDensity.adaptivePlatformDensity,
18      ),
19      home: PasswordScreen(),//MyHomePage(title: 'Flutter Demo Home Page'),
20    );
21  }
22}
23
24class MyHomePage extends StatefulWidget {
25  MyHomePage({Key key, this.title}) : super(key: key);
26
27  final String title;
28
29  @override
30  _MyHomePageState createState() => _MyHomePageState();
31}
32
33class _MyHomePageState extends State<MyHomePage> {
34  int _counter = 0;
35
36  void _incrementCounter() {
37    setState(() {
38      _counter++;
39    });
40  }
41
42  @override
43  Widget build(BuildContext context) {
44    return Scaffold(
45      appBar: AppBar(
46        title: Text(widget.title),
47      ),
48      body: Center(
49        child: Column(
50          mainAxisAlignment: MainAxisAlignment.center,
51          children: <Widget>[
52            Text(
53              'You have pushed the button this many times:',
54            ),
55            Text(
56              '$_counter',
57              style: Theme.of(context).textTheme.headline4,
58            ),
59          ],
60        ),
61      ),
62      floatingActionButton: FloatingActionButton(
63        onPressed: _incrementCounter,
64        tooltip: 'Increment',
65        child: Icon(Icons.add),
66      ),
67    );
68  }
69}
 1/// keyboard.dart
 2import 'package:escape_game/provider/keys_provider.dart';
 3import 'package:flutter/material.dart';
 4
 5class Keyboard extends StatefulWidget {
 6  @override
 7  _KeyboardState createState() => _KeyboardState();
 8}
 9
10class _KeyboardState extends State<Keyboard> {
11  @override
12  Widget build(BuildContext context) {
13    return Column(
14      children: [
15        SingleChildScrollView(
16          scrollDirection: Axis.horizontal,
17          child: Row(
18            children: [
19              ...KeysProvider.keys,
20            ],
21          ),
22        ),
23        Row(
24          children: [],
25        ),
26        Row(
27          children: [],
28        ),
29      ],
30    );
31  }
32}
 1/// keyboard_key.dart
 2import 'package:escape_game/provider/keys_provider.dart';
 3import 'package:flutter/material.dart';
 4
 5class KeyboardKey extends StatelessWidget {
 6
 7  final String char;
 8
 9  const KeyboardKey({@required this.char});
10
11  @override
12  Widget build(BuildContext context) {
13    return RaisedButton(
14      onPressed: () {
15        print(char);
16        KeysProvider.shuffleKeys();
17      },
18      child: Text(char),
19    );
20  }
21}
 1/// fake_form_field.dart
 2import 'package:escape_game/provider/keys_provider.dart';
 3import 'package:escape_game/utils/utils.dart';
 4import 'package:flutter/material.dart';
 5import 'package:provider/provider.dart';
 6
 7class FakeTextField extends StatelessWidget {
 8  final String text;
 9  final Function onTap;
10  final Color textColor;
11
12  FakeTextField({@required this.text, @required this.onTap, @required this.textColor});
13
14  @override
15  Widget build(BuildContext context) {
16
17    KeysProvider.initKeys();
18    return GestureDetector(
19      onTap: onTap,
20      child: Container(
21        width: MediaQuery.of(context).size.width - 10,
22        decoration:
23            BoxDecoration(border: Border.all(width: 1, color: Colors.grey)),
24        child: Padding(
25          padding: EdgeInsets.all(8),
26          child: Text(
27            text,
28            style: TextStyle(
29              color: textColor,
30              fontSize: 12,
31            ),
32          ),
33        ),
34      ),
35    );
36  }
37}
 1/// utils.dart
 2
 3import 'package:escape_game/widget/keyboard_key.dart';
 4
 5class Utils {
 6  static String typedPassword = "test";
 7
 8  static String typedPasswordStars() {
 9    String s = "";
10    for (var i = 0; i < typedPassword.length; i++) {
11      s += "*";
12    }
13    return s;
14  }
15
16  static List<String> chars;
17
18  static void initChars() {
19    chars = List<String>();
20    for (var i = 33; i <= 126; i++) {
21      chars.add(String.fromCharCode(i));
22    }
23    for (var i = 127761; i <= 127773; i++) {
24      chars.add(String.fromCharCode(i));
25    }
26  }
27}
 1/// password_screen.dart
 2import 'package:escape_game/utils/utils.dart';
 3import 'package:escape_game/widget/fake_form_field.dart';
 4import 'package:escape_game/widget/keyboard.dart';
 5import 'package:flutter/material.dart';
 6
 7class PasswordScreen extends StatefulWidget {
 8  @override
 9  _PasswordScreenState createState() => _PasswordScreenState();
10}
11
12class _PasswordScreenState extends State<PasswordScreen> {
13  bool _keyboardEnabled = false;
14
15  void _enableKeyBoard() {
16    setState(() {
17      _keyboardEnabled = true;
18    });
19  }
20
21  @override
22  Widget build(BuildContext context) {
23    Utils.initChars();
24    return Scaffold(
25      appBar: AppBar(),
26      body: Column(
27        children: [
28          Container(
29            height: _keyboardEnabled
30                ? (MediaQuery.of(context).size.height -
31                        MediaQuery.of(context).padding.top -
32                        kToolbarHeight) *
33                    2 /
34                    3
35                : MediaQuery.of(context).size.height -
36                    MediaQuery.of(context).padding.top -
37                    kToolbarHeight,
38            child: Center(
39              child: FakeTextField(
40                text: _keyboardEnabled
41                    ? Utils.typedPasswordStars()
42                    : "Entrez le mot de passe",
43                onTap: _enableKeyBoard,
44                textColor: _keyboardEnabled ? Colors.black : Colors.grey,
45              ),
46            ),
47          ),
48          if (_keyboardEnabled)
49            Container(
50              height: (MediaQuery.of(context).size.height -
51                      MediaQuery.of(context).padding.top -
52                      kToolbarHeight) /
53                  3,
54              child: Keyboard(),
55            ),
56        ],
57      ),
58    );
59  }
60}
 1/// keys_provider.dart
 2
 3import 'package:escape_game/utils/utils.dart';
 4import 'package:escape_game/widget/keyboard_key.dart';
 5import 'package:flutter/foundation.dart';
 6import 'package:provider/provider.dart';
 7
 8class KeysProvider with ChangeNotifier {
 9  static List<KeyboardKey> keys;
10
11  void initKeys() {
12    keys = List<KeyboardKey>();
13    for (String char in Utils.chars) {
14      keys.add(KeyboardKey(
15        char: char,
16      ));
17    }
18  }
19
20  void shuffleKeys() {
21    keys.shuffle();
22  }
23}