Code Monkey home page Code Monkey logo

2022beginnertrainingflutter's People

Stargazers

likecoffeejp avatar Wassy | Ryoga Washizawa avatar Yuya Aizawa avatar Kazuki Tanaka avatar  avatar Hasegawa.Masaki avatar Hiroto Noda avatar Carlos Paniagua avatar  avatar Ryota Omomo avatar nzk avatar atsuki asada avatar Sugyyy avatar mukai_masaki avatar Tatsuya Arai avatar Isao Kono avatar

Watchers

 avatar Takuji TERADA avatar Takao Kaburaki avatar Atsuto Yamada avatar Emi Sugita avatar Naohiro Kurihara avatar Hagihara Ryosuke avatar kosuke yagi avatar Tomohiro Koike avatar Hisada avatar Kazuki Okamoto avatar  avatar Takuya Iitsuka avatar takuya-ebata avatar

2022beginnertrainingflutter's Issues

答え合わせ Mutation を使わなかったチームのためにあわてて作った回答例

flutter_02

import 'package:flutter/material.dart';
import 'package:uuid/uuid.dart';
import 'package:graphql/client.dart';

class Answer {
  const Answer({
    required this.char,
    required this.position,
    required this.judge,
  });
  final String char;
  final int position;
  final String judge;
}

class WordlePage extends StatefulWidget {
  const WordlePage({Key? key}) : super(key: key);

  @override
  State<StatefulWidget> createState() {
    return _WordlePageState();
  }
}

class AnswerLetter extends StatelessWidget {
  const AnswerLetter({Key? key, required this.answer}) : super(key: key);
  final Answer answer;

  @override
  Widget build(BuildContext context) {
    return Container(
      height: 50,
      width: 50,
      decoration: BoxDecoration(
        color: (answer.judge == "CORRECT")
            ? Colors.lightGreenAccent
            : (answer.judge == "EXISTING")
                ? Colors.amberAccent
                : Colors.grey,
      ),
      child: Text(answer.char),
    );
  }
}

class _WordlePageState extends State<WordlePage> {
  bool loading = false;
  List<List<Answer>> results = [];

  String word = 'mixi';
  String mean = '弊社';

  String inputText = '';
  bool giveup = false;

  void getWord() async {
    final _link = HttpLink(
      'https://serene-garden-89220.herokuapp.com/query',
    );
    final _client = GraphQLClient(link: _link, cache: GraphQLCache());

    final wordId = const Uuid().v1();

    const String wordQuery = r'''
query A($id: String!) {
  correctWord(wordId: $id) {
    word
    mean
  }
}
    ''';

    setState(() {
      loading = true;
    });
    final result = await _client.query(QueryOptions(
      document: gql(wordQuery),
      variables: <String, dynamic>{
        'id': wordId,
      },
    ));

    if (result.hasException) {
      print(result.exception.toString());
    } else {
      print(result.data);
      final data = result.data;
      if (data != null) {
        setState(() {
          word = data['correctWord']['word'];
          mean = data['correctWord']['mean'];
          results = [];
          giveup = false;
        });
      }
    }

    setState(() {
      loading = false;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text("GraphQL"),
      ),
      body: Column(
        children: [
          TextButton(
            child: const Text("新しい単語"),
            onPressed: getWord,
          ),
          TextField(
            onChanged: (value) {
              inputText = value;
            },
          ),
          IconButton(
            icon: const Icon(Icons.favorite),
            onPressed: () {
              if (inputText.length < 4) {
                return;
              }
              List<Answer> anss = [];
              for (var i = 0; i < 4; i++) {
                final c = inputText[i];

                var judge = "NOTHING";
                if (c == word[i]) {
                  judge = "CORRECT";
                } else if (word.contains(c)) {
                  judge = "EXISTING";
                }

                anss.add(Answer(
                  char: c,
                  position: i,
                  judge: judge,
                ));
              }

              setState(() {
                results.add(anss);
              });
            },
          ),
          Flexible(
            child: SingleChildScrollView(
              child: Column(
                children: [
                  Padding(
                    padding: const EdgeInsets.all(10),
                    child: Column(
                      children: results.map((e) {
                        return Row(
                          children: e.map((r) {
                            return AnswerLetter(answer: r);
                          }).toList(),
                        );
                      }).toList(),
                    ),
                  ),
                  TextButton(
                    child: const Text("ギブアップ"),
                    onPressed: () {
                      setState(() {
                        giveup = true;
                      });
                    },
                  ),
                  if (giveup)
                    Column(
                      children: [
                        Text(word),
                        Text(mean),
                      ],
                    )
                ],
              ),
            ),
          )
        ],
      ),
    );
  }
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.