Created
March 21, 2018 09:00
-
-
Save toshimasa-nanaki/06dab8be867bef083e6ddcc72883e0cc to your computer and use it in GitHub Desktop.
flutterアプリを作ってみたい3-1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
import 'package:english_words/english_words.dart'; | |
void main() => runApp(new MyApp()); | |
class MyApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
/// ランダムな2単語生成はRandomWordStateに任せます。 | |
/// final wordPair = new WordPair.random(); | |
return new MaterialApp( | |
title: 'Flutterへようこそ', | |
home: new Scaffold( | |
appBar: new AppBar( | |
title: new Text('Flutterへようこそ'), | |
), | |
body: new Center( | |
/// childツリーにはTextの追加ではなく、RandomWordsウィジェットの追加にします。 | |
/// Textの追加はRandomWordStateに任せます。 | |
/// child: new Text(wordPair.asPascalCase), | |
child: new RandomWords(), | |
), | |
), | |
); | |
} | |
} | |
/// これがステートフルウィジェットです。 | |
/// ステートフルウィジェットは変化する状態を保持します。 | |
class RandomWords extends StatefulWidget { | |
/// 上記で言った変化する状態というのが、 | |
/// createStateメソッドで作成しているRandomWordsStateインスタンスです。 | |
@override | |
createState() => new RandomWordsState(); | |
} | |
/// これがステートフルウィジェットのステート部分です。 | |
/// RandomWordsウィジェットの状態を保持するのがこのクラスの仕事です。 | |
class RandomWordsState extends State<RandomWords> { | |
/// 今までMyappのbuildメソッドの中でやってたことを、こちらにやらせます。 | |
/// 要は | |
/// RandomWordsウィジェットの状態として文字ペアの変化状態を保持するわけです。 | |
@override | |
Widget build(BuildContext context) { | |
final wordPair = new WordPair.random(); | |
return new Text(wordPair.asPascalCase); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment