<|start_filename|>section_3/3_interactive/3-3_main.dart<|end_filename|>
/* 3-3. スイッチ */

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: "My Simple App",
      home: Scaffold(
        appBar: AppBar(
          title: Text("Live!人工知能"),
        ),
        body: Center(
          child: MyForm(),
        ),
      ),
    );
  }
}

class MyForm extends StatefulWidget {
  @override
  _MyFormState createState() => _MyFormState();
}

class _MyFormState extends State<MyForm> {
  bool _active = false;

  void _changeSwitch(bool e) {
    setState(() {  // 状態を保持する変数を変更する処理は、setState内に記述する
      _active = e;
    });
  }

  Widget build(BuildContext context) {
    return Container(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Center(
              child: Icon(
                Icons.thumb_up,
                color: _active ? Colors.orange : Colors.grey,
                size: 100.0,
              ),
            ),
            Switch(  // スイッチ
              value: _active,
              onChanged: _changeSwitch,
            )
          ],
        )
    );
  }
}


<|start_filename|>section_3/2_layout/2-3_main.dart<|end_filename|>
/* 2-3. Expanded */

import 'package:flutter/material.dart';

void main() => runApp(MyApp()); // 引数のWidgetが画面いっぱいに表示される

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: "My Simple App",
      home: Container(
        child: Row(
          children: <Widget>[
            Expanded(  // 引き延ばして表示
              flex: 1,
                child:Container( color: Colors.red)
            ),
            Expanded(
              flex: 2,
              child:Container( color: Colors.green),
            ),
            Expanded(
              child:Container( color: Colors.blue),
              flex: 3,
            ),
          ],
        ),
      ),
    );
  }
}


<|start_filename|>section_4/main_without_fb.dart<|end_filename|>
import 'package:flutter/material.dart';

final dummyData = [
  {"name": "Pochi", "votes": 0},
  {"name": "Taro", "votes": 0},
  {"name": "Jiro", "votes": 0},
  {"name": "Shiro", "votes": 0},
  {"name": "Hachi", "votes": 0},
];

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: "Dog Name Voting",
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() {
    return _MyHomePageState();
  }
}

class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text("Dog Name Voting")),
      body: _buildBody(),
    );
  }

  Widget _buildBody() {
    return _buildList(dummyData);
  }

  Widget _buildList(List<Map<String, dynamic>> dataList) {
    return ListView.builder(
        padding: const EdgeInsets.all(18.0),
        itemCount: dataList.length,
        itemBuilder: (context, i) {
          return _buildListItem(dataList[i]);
        }
    );
  }

  Widget _buildListItem(Map<String, dynamic> data) {
    return Padding(
      padding: const EdgeInsets.symmetric(horizontal: 18.0, vertical:9.0),
      child: Container(
        decoration: BoxDecoration(
          border: Border.all(color: Colors.grey),
          borderRadius: BorderRadius.circular(8.0),
        ),
        child: ListTile(
          title: Text(data["name"]),
          trailing: Text(data["votes"].toString()),
          onTap: () {
            setState(() { data["votes"] += 1; });
            },
        ),
      ),
    );
  }
}


<|start_filename|>section_3/2_layout/2-1_main.dart<|end_filename|>
/* 2-1. Container */

import 'package:flutter/material.dart';

void main() => runApp(MyApp()); // 引数のWidgetが画面いっぱいに表示される

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: "My Simple App",
      home: Scaffold(
        body: Center(
          child: Container(
            color: Colors.blue,
            width: 300.0,
            height: 300.0,
            child: Text("Live!人工知能"),
            alignment: Alignment.center,
          ),
        ),
      ),
    );
  }
}


<|start_filename|>section_3/1_simple_app/1-4_main.dart<|end_filename|>
/* 1-4. ListView */

import 'package:flutter/material.dart';
import 'package:english_words/english_words.dart';

void main() => runApp(MyApp()); // 引数のWidgetが画面いっぱいに表示される

// 状態を管理するクラスは、Stateクラスを継承
class RandomWordsState extends State<RandomWords> {
  // _を付けるとライブラリ外からアクセスできなくなる
  // ListViewのWidgetを作るメソッド
  Widget _buildSuggestions() {
    final _wordPairs = <WordPair>[];  // 単語のペアを格納するリスト
    return ListView.builder(
      itemBuilder: (context, i) { // itemBuilderで一行ごとに処理が呼ばれる
        if (i.isOdd) return Divider();  // 奇数行には水平線を表示

        final index = i ~/ 2;  // ~/は結果が整数の割り算
        if (index >= _wordPairs.length) {  // 行数がリストの要素数を越えれば
          _wordPairs.addAll(generateWordPairs().take(10));  // 単語のペアを10個追加
        }
        return _buildRow(_wordPairs[index]);
      }
    );
  }

  // 単語のペアから、形式を整えた行のWidgetを作るメソッド
  Widget _buildRow(WordPair pair) {
    return ListTile(
      title: Text(
        pair.asPascalCase,
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Live!人工知能"),
      ),
      body: _buildSuggestions(),
    );
  }
}

// StatefulなWidgetのクラスは、StatefulWidgetを継承
class RandomWords extends StatefulWidget {
  @override
  RandomWordsState createState() => RandomWordsState();
}

// 最初に表示するWidgetのクラス
class MyApp extends StatelessWidget {  // StatelessWidgetを継承
  @override
  Widget build(BuildContext context) {  //buildメソッドでUIを作成
    return MaterialApp(  // マテリアルデザインのアプリ
      title: "My Simple App",  // アプリのタイトル
      home: RandomWords()
    );
  }
}


<|start_filename|>section_3/2_layout/model_answer2.dart<|end_filename|>
/* 演習2の解答例 */

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: "My Simple App",
      home: Container(
        child: Row(  // 行
          mainAxisAlignment: MainAxisAlignment.spaceAround,
          children: <Widget>[
            Column(  // 列
              mainAxisAlignment: MainAxisAlignment.start,
              children: <Widget>[
                Container( color: Colors.red, width: 100, height:100 ),
                Container( color: Colors.green, width: 100, height:100 ),
                Container( color: Colors.blue, width: 100, height:100 ),
                Container( color: Colors.yellow, width: 100, height:100 ),
              ],
            ),
            Column(  // 列
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                Container( color: Colors.red, width: 100, height:100 ),
                Container( color: Colors.green, width: 100, height:100 ),
                Container( color: Colors.blue, width: 100, height:100 ),
                Container( color: Colors.yellow, width: 100, height:100 ),
              ],
            ),
            Column(  // 列
              mainAxisAlignment: MainAxisAlignment.end,
              children: <Widget>[
                Container( color: Colors.red, width: 100, height:100 ),
                Container( color: Colors.green, width: 100, height:100 ),
                Container( color: Colors.blue, width: 100, height:100 ),
                Container( color: Colors.yellow, width: 100, height:100 ),
              ],
            ),
          ],
        ),
      ),
    );
  }
}


<|start_filename|>section_3/3_interactive/3-4_main.dart<|end_filename|>
/* 3-4. スライダー */

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: "My Simple App",
      home: Scaffold(
        appBar: AppBar(
          title: Text("Live!人工知能"),
        ),
        body: Center(
          child: MyForm(),
        ),
      ),
    );
  }
}

class MyForm extends StatefulWidget {
  @override
  _MyFormState createState() => _MyFormState();
}

class _MyFormState extends State<MyForm> {
  double _value = 0.0;

  void _changeSlider(double e) {
    setState(() {  // 状態を保持する変数を変更する処理は、setState内に記述する
      _value = e;
    });
  }

  Widget build(BuildContext context) {
    return Container(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Center(child:Text("値：${_value}")),
            Slider(  // スライダー
              label: '${_value}',
              min: 0,
              max: 10,
              value: _value,
              activeColor: Colors.orange,
              inactiveColor: Colors.blueAccent,
              divisions: 10,
              onChanged: _changeSlider,
            )
          ],
        )
    );
  }
}


<|start_filename|>section_5/main_without_mlkit.dart<|end_filename|>
import 'dart:io';

import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.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: "Face Finder",
      theme: ThemeData(
        primarySwatch: Colors.blue,
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      home: FaceFinder(),
    );
  }
}

class FaceFinder extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => _FaceFinderState();
}

class _FaceFinderState extends State<FaceFinder> {
  File _imageFile;
  final ImagePicker _picker = ImagePicker();

  void _getImageAndFindFace(ImageSource imageSource) async {
    setState(() {
      _imageFile = null;
    });

    final PickedFile pickedImage = await _picker.getImage(source: imageSource);
    final File imageFile = File(pickedImage.path);

    setState(() {
      _imageFile = imageFile;
    });
  }

  Widget _makeImage() {
    return Container(
      constraints: BoxConstraints.expand(),
      decoration: BoxDecoration(
        image: DecorationImage(
          image: Image.file(_imageFile).image,
          fit: BoxFit.contain,
        ),
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Face Finder"),
      ),
      body: _imageFile == null
          ? Center(child: Text("No image selected."))
          : _makeImage(),
      floatingActionButton: Column(
        mainAxisAlignment: MainAxisAlignment.end,
        children: <Widget>[
          FloatingActionButton(
            onPressed:(){_getImageAndFindFace(ImageSource.gallery);} ,
            tooltip: "Select Image",
            child: Icon(Icons.add_photo_alternate),
          ),
          Padding(padding: EdgeInsets.all(10.0)),
          FloatingActionButton(
            onPressed:(){_getImageAndFindFace(ImageSource.camera);} ,
            tooltip: "Take Photo",
            child: Icon(Icons.add_a_photo),
          ),
        ],
      )
    );
  }
}


<|start_filename|>section_3/4_transition/4-3_main.dart<|end_filename|>
/* 4-3. Drawer */

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: MainPage(),
    );
  }
}

class MainPage extends StatefulWidget {
  @override
  _MainPageState createState() => _MainPageState();
}

class _MainPageState extends State<MainPage> {

  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text("Live!人工知能"),
      ),
      drawer: Drawer( // Drawerの配置
        child: ListView(
          children: <Widget>[
            DrawerHeader(
              child: Text("ヘッダーです。"),
              decoration: BoxDecoration(  // Boxによる装飾
                color: Colors.blue,
              ),
            ),
            ListTile(
              title: Text("アイテムその1"),
              trailing: Icon(Icons.arrow_forward),
            ),
            ListTile(
              title: Text("アイテムその2"),
              trailing: Icon(Icons.arrow_forward),
            ),
            ListTile(
              title: Text("アイテムその3"),
              trailing: Icon(Icons.arrow_forward),
            ),
          ],
        ),
      ),
    );
  }
}


<|start_filename|>section_3/4_transition/4-1_main.dart<|end_filename|>
/* 4-1. 画面遷移 */

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: MainPage(),
      routes: <String, WidgetBuilder> {
        '/home': (BuildContext context) => MainPage(),  // 最初のページ
        '/subpage': (BuildContext context) => SubPage()  // 次のページ
      },
    );
  }
}

class MainPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Mainページ"),
      ),
      body: Container(
        child: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              Text("Main"),
              RaisedButton( // 立体的なボタン
                onPressed: () => Navigator.of(context).pushNamed("/subpage"),  // 次の画面を乗せる
                child: Text("Subページへ"),
              )
            ],
          ),
        ),
      ),
    );
  }
}

class SubPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Subページ"),
      ),
      body: Container(
        child: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              Text("Sub"),
              RaisedButton(
                onPressed: () => Navigator.of(context).pop(),  // この画面を取り除く
                child: Text("戻る"),
              )
            ],
          ),
        ),
      ),
    );
  }
}


<|start_filename|>section_3/3_interactive/3-2_main.dart<|end_filename|>
/* 3-2. テキストの入力 */

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: "My Simple App",
      home: Scaffold(
        appBar: AppBar(
          title: Text("Live!人工知能"),
        ),
        body: Center(
          child: MyForm(),
        ),
      ),
    );
  }
}

class MyForm extends StatefulWidget {
  @override
  _MyFormState createState() => _MyFormState();
}

class _MyFormState extends State<MyForm> {
  String _text = '';

  void _handleText(String e) {
    setState(() {  // 状態を保持する変数を変更する処理は、setState内に記述する
      _text = e;
    });
  }

  Widget build(BuildContext context) {
    return Container(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text(
              "$_text",
              style: TextStyle(
                color:Colors.blueAccent,
                fontSize: 30.0,
              ),
            ),
            TextField(  // テキストを表示
              style: TextStyle(color: Colors.red),
              onChanged: _handleText,
            ),
          ],
        )
    );
  }
}


<|start_filename|>section_3/2_layout/2-4_main.dart<|end_filename|>
/* 2-4. Stack */

import 'package:flutter/material.dart';

void main() => runApp(MyApp()); // 引数のWidgetが画面いっぱいに表示される

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        home: Scaffold(
            appBar: AppBar(
              title: Text("My Simple App"),
            ),
            body: Stack(  // 重ねて表示
              children: <Widget>[
                Container(
                    width: 200,
                    height: 200,
                    color:Colors.red
                ),
                Container(
                  width: 100,
                  height: 120,
                  color: Colors.green,
                ),
                Container(
                  width: 50,
                  height: 70,
                  color: Colors.blue,
                )
              ],
            )
        )
    );
  }
}


<|start_filename|>section_3/4_transition/model_answer4.dart<|end_filename|>
/* 演習4の解答例 */

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: MainPage(),
      routes: <String, WidgetBuilder> {
        '/home': (BuildContext context) => MainPage(),
        '/subpage1': (BuildContext context) => SubPage1(),
        '/subpage2': (BuildContext context) => SubPage2(),
      },
    );
  }
}

class MainPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Mainページ"),
      ),
      body: Container(
        child: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              Text("Main"),
              RaisedButton( // 立体的なボタン
                onPressed: () => Navigator.of(context).pushNamed("/subpage1"),  // 次の画面を乗せる
                child: Text("Sub1へ"),
              )
            ],
          ),
        ),
      ),
    );
  }
}

class SubPage1 extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Sub1"),
      ),
      body: Container(
        child: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              Text("Sub1"),
              RaisedButton( // 立体的なボタン
                onPressed: () => Navigator.of(context).pushNamed("/subpage2"),  // 次の画面を乗せる
                child: Text("Sub2へ"),
              )
            ],
          ),
        ),
      ),
    );
  }
}

class SubPage2 extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Sub2"),
      ),
      body: Container(
        child: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              Text("Sub2"),
            ],
          ),
        ),
      ),
    );
  }
}
