2022beginnertrainingflutter's People
2022beginnertrainingflutter's Issues
comments
答え合わせ Mutation を使わなかったチームのためにあわてて作った回答例
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
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.