0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Dart/flutter勉強中【コードメモ001】

0
Last updated at Posted at 2019-10-31

チュートリアルで作った簡単プログラムたち

このコードが教えてくれること

  • row
  • column
  • expanded

image.png

import 'package:flutter/material.dart';

void main() {
  runApp(MaterialApp(
    home: Home(),
  ));
}

class Home extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('my first app'),
        centerTitle: true,
        backgroundColor: Colors.red[600],
      ),
      body: Row(//Columnだと縦置きになる
        mainAxisAlignment:MainAxisAlignment.start,
        crossAxisAlignment:CrossAxisAlignment.center,
        children: <Widget>[
          Expanded(
            flex: 3,
            child: Image.network("https://images.freeimages.com/images/large-previews/5b7/on-the-road-7-1384791.jpg")
            ),
          Expanded(
            flex: 1,
            child: Container(
              color: Colors.cyan,
              padding: EdgeInsets.all(30),
              child: Text('1'),
            ),
          ),
          Expanded(
            flex: 1,
            child: Container(
              color: Colors.pinkAccent,
              padding: EdgeInsets.all(30),
              child: Text('2'),
            ),
          ),
          Expanded(
            flex: 1,
            child: Container(
              color: Colors.amber,
              padding: EdgeInsets.all(30),
              child: Text('3'),
            ),
          ),
        ],
      ),
      floatingActionButton: FloatingActionButton(
        child: Text('click?'),
        backgroundColor: Colors.red[600],
        onPressed: () {},
      ),
    );
  }
}

このコードが教えてくれること

  • StatefulWidget
  • Stateの使い方

image.png

import 'package:flutter/material.dart';

void main() => runApp(MaterialApp(
    home:NinjaCard(),
));

class NinjaCard extends StatefulWidget {
  @override
  _NinjaCardState createState() => _NinjaCardState();
}

class _NinjaCardState extends State<NinjaCard> {

  int ninjaLevel = 0;


  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.grey[900],
      appBar: AppBar(
        title: Text("Ninja ID Card"),
        centerTitle: true,
        backgroundColor: Colors.grey[800],
        elevation:0.0,
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: (){
          // ninjaLevel += 1;コレだけだと描画が更新されない
          setState((){
            ninjaLevel += 1;
          });
        },
        child:Icon(Icons.add),
        backgroundColor: Colors.grey[800],
      ),
      body: Padding(
        padding: EdgeInsets.fromLTRB(40, 40, 40, 0),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: <Widget>[
            Center(
              child: CircleAvatar(
                backgroundImage: AssetImage("images/chun-li.jpg"),
                radius: 80,
              ),
            ),
            Divider(
              height:60,
              color: Colors.grey[800],
            ),
            Text(
              "NAME",
              style:TextStyle(
                color: Colors.grey,
                letterSpacing: 2.0,
              )
            ),
            SizedBox(height:20),
            Text(
              "Chun-Li",
              style:TextStyle(
                color: Colors.amberAccent[200],
                letterSpacing: 2.0,
                fontSize: 30,
                fontWeight: FontWeight.bold,
              )
            ),
            SizedBox(height:40),
            Text(
              "CURENT NINJA LEVEL",
              style:TextStyle(
                color: Colors.grey,
                letterSpacing: 2.0,
              )
            ),
            SizedBox(height:20),
            Text(
              '$ninjaLevel',
              style:TextStyle(
                color: Colors.amberAccent[200],
                letterSpacing: 2.0,
                fontSize: 30,
                fontWeight: FontWeight.bold,
              )
            ),
            SizedBox(height:40),
            Row(
              children: <Widget>[
                Icon(
                  Icons.email,
                  color:Colors.grey[400],
                ),
                SizedBox(width:20),
                Text(
                  'chun.li@ninja.co.uk',
                  style: TextStyle(
                    color:Colors.grey[400],
                    fontSize: 20,
                    letterSpacing: 1,
                  ),
                ),
              ],
            ),
          ],
        ),
      ),
    );
  }
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?