Flutter学习历程

 

目录

1.Container组件简介

2.Image组件的使用

3.ListView组件

4.万事皆组件

5.列表组件,传值

6.网格

7.垂直布局

8.布局层叠

9.路由导航父子界面

10.跳转传参数

11.页面跳转并返回参数跳转传参数 


 

1.Container组件简介

import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      title: 'Startup Name Generator',
      theme: new ThemeData(
        primaryColor: Colors.blue,
      ),
      home: Scaffold(
        appBar: AppBar(
          title: Text('Welcome .....toFlutter'),
        ),
        body: Center(
          child: Container(
            child: new Text(
              "Hellow Word",
              style: TextStyle(fontSize: 40.0),
            ),
            alignment:Alignment.topLeft,
            width: 500.0,
            height: 400.0,
            padding: const EdgeInsets.fromLTRB(10.0,30.0,0.0,0.0),
            margin: const EdgeInsets.all(10.0),
            decoration: new BoxDecoration(
              gradient: const LinearGradient(colors: [Colors.lightBlue,Colors.greenAccent,Colors.purple]),
              border: Border.all(width: 2.0,color: Colors.red)
            ),

          ),
        ),
      ),
    );
  }
}

2.Image组件的使用

import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      title: 'Startup Name Generator',
      theme: new ThemeData(
        primaryColor: Colors.blue,
      ),
      home: Scaffold(
        appBar: AppBar(
          title: Text('Welcome .....toFlutter'),
        ),
        body: Center(
          child: Container(
            child: new Image.network(
              'https://avatar.csdnimg.cn/0/5/D/3_u010194271.jpg',
              repeat: ImageRepeat.repeatX,
            ),
            width: 300.0,
            height: 200.0,
            color: Colors.lightBlue,
          ),
        ),
      ),
    );
  }
}

3.ListView组件

import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        title: 'JsPangFlutter',
        home: Scaffold(
          appBar: new AppBar(title: new Text('ListView Widget')),
          body: new ListView(
            children: <Widget>[
              new ListTile(
                leading: new Icon(Icons.perm_camera_mic),
                title: new Text('perm_camera_mic'),
              ),
              new ListTile(
                leading: new Icon(Icons.perm_contact_calendar),
                title: new Text('perm_contact_calendar'),
              ),
              new Image.network(
                  'https://avatar.csdnimg.cn/0/5/D/3_u010194271.jpg'),
              new Image.network(
                  'https://avatar.csdnimg.cn/0/5/D/3_u010194271.jpg'),
              new Image.network(
                  'https://avatar.csdnimg.cn/0/5/D/3_u010194271.jpg'),
              new Image.network(
                  'https://avatar.csdnimg.cn/0/5/D/3_u010194271.jpg'),
            ],
          ),
        ));
  }
}

4.万事皆组件

import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        title: 'JsPangFlutter',
        home: Center(
          child: Container(
            height: 200.0,
            child:MyList(),
          ),
        ));
  }
}

class MyList extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return ListView(
      scrollDirection: Axis.horizontal,
      children: <Widget>[
        new Container(width: 180.0, color: Colors.lightBlue),
        new Container(width: 180.0, color: Colors.lightGreen),
        new Container(width: 180.0, color: Colors.orangeAccent),
      ],
    );
  }
}

5.列表组件,传值

import 'package:flutter/material.dart';

void main() => runApp(MyApp(
    items: new List<String>.generate(1000, (i) => "Item $i")));

class MyApp extends StatelessWidget {
  final List<String> items;

  MyApp({Key key, @required this.items}) :super(key: key);

 @override
  Widget build(BuildContext context) {
    return MaterialApp(
        title: 'JsPangFlutter',
        home: Scaffold(
          appBar: new AppBar(title: new Text("ListView")),
          body: new ListView.builder(
            itemCount: items.length,
            itemBuilder: (context, index) {
              return new ListTile(
                title: new Text('${items[index]}'),
              );
            },
          ),

        ));
  }
}

class MyList extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return ListView(
      scrollDirection: Axis.horizontal,
      children: <Widget>[
        new Container(width: 180.0, color: Colors.lightBlue),
        new Container(width: 180.0, color: Colors.lightGreen),
        new Container(width: 180.0, color: Colors.orangeAccent),
      ],
    );
  }
}

6.网格

import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'JSJXF Flutter Demo',
      home: Scaffold(
          appBar: new AppBar(title: new Text('ListView Widget')),
          body: GridView(
            gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                crossAxisCount: 3,
                mainAxisSpacing: 2.0,
                crossAxisSpacing: 2.0,
                childAspectRatio: 1.5),
            children: <Widget>[
              new Image.network(
                  'http://img5.mtime.cn/mg/2020/05/28/111843.63993498_285X160X4.jpg',fit: BoxFit.fill),
              new Image.network(
                  'http://img5.mtime.cn/mg/2020/05/28/102840.71908635_285X160X4.jpg',fit: BoxFit.fill),
              new Image.network(
                  'http://img5.mtime.cn/mg/2020/05/28/093352.16747651_285X160X4.jpg',fit: BoxFit.fill),
              new Image.network(
                  'http://img5.mtime.cn/mg/2020/05/27/210145.79060565_285X160X4.jpg',fit: BoxFit.fill),
            ],
          )),
    );
  }
}

import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'JSJXF Flutter Demo',
      home: Scaffold(
          appBar: new AppBar(title: new Text('ListView Widget')),
          body: new Row(
            children: <Widget>[
              Expanded(
                  child: new RaisedButton(
                onPressed: () {},
                color: Colors.lightBlue,
                child: new Text('lightBlue'),
              )),
              Expanded(
                  child: new RaisedButton(
                onPressed: () {},
                color: Colors.orange,
                child: new Text('lightBlue'),
              )),
              Expanded(
                  child: new RaisedButton(
                onPressed: () {},
                color: Colors.pink,
                child: new Text('lightBlue'),
              ))
            ],
          )),
    );
  }
}


7.垂直布局

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

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'JSJXF Flutter Demo',
      home: Scaffold(
          appBar: new AppBar(title: new Text('ListView Widget')),
          body: Center(
            child: new Column(
              crossAxisAlignment: CrossAxisAlignment.center,
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                Text('I am JXF'),
                Expanded(child: Text('I am JXF I am JXF I am JXF')),
                Text('I am JXF')
              ],
            ),
          )),
    );
  }
}

8.布局层叠

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

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    var stack = new Stack(
      alignment: const FractionalOffset(0.5, 0.8),
      children: <Widget>[
        new CircleAvatar(
          backgroundImage: new NetworkImage(
              'http://img5.mtime.cn/mg/2020/01/06/144226.95498850.jpg'),
          radius: 100.0,
        ),
        new Container(
          decoration: new BoxDecoration(color: Colors.lightBlue),
          padding: EdgeInsets.all(5.0),
          child: Text('JS 技术群'),
        )
      ],
    );
    return MaterialApp(
      title: 'Demo',
      home: Scaffold(
          appBar: new AppBar(title: new Text('Demo')),
          body: Center(
            child: stack,
          )),
    );
  }
}

9.路由导航父子界面

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:hellowword/ui/page/oneDemo.dart';

void main() {
  runApp(MaterialApp(
    title: '导航演示01',
    home: new MyApp(),
  ));
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('标题')),
      body: Center(
        child: RaisedButton(
          child: Text("查看商品详情页"),
          onPressed: () {
            Navigator.push(context,
                MaterialPageRoute(builder: (context) => new SecondScream()));
          },
        ),
      ),
    );
  }
}

class SecondScream extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('商品详情')),
      body: Center(
        child: RaisedButton(
          child: Text('返回'),
          onPressed: () {
            Navigator.pop(context);
          },
        ),
      ),
    );
  }
}

10.跳转传参数

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

class Product {
  final String title;
  final String description;

  Product(this.title, this.description);
}

void main() {
  runApp(MaterialApp(
    title: '导航的数据传递和接受',
    home: ProductList(
        products: List.generate(
            20, (i) => Product('Transformers商品 $i', '这是一个商品详情,编号是$i'))),
  ));
}

class ProductList extends StatelessWidget {
  final List<Product> products;

  ProductList({Key key, @required this.products}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('商品列表'),
      ),
      body: ListView.builder(
        itemCount: products.length,
        itemBuilder: (context, index) {
          return ListTile(
            title: Text(products[index].title),
            onTap: () {
              Navigator.push(
                  context,
                  MaterialPageRoute(
                      builder: (context) =>
                          ProductDetail(product: products[index])));
            },
          );
        },
      ),
    );
  }
}

class ProductDetail extends StatelessWidget {
  final Product product;

  ProductDetail({Key key, @required this.product}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(title: Text('${product.title}')),
        body: Center(
          child: Text('${product.description}'),
        ));
  }
}

11.页面跳转并返回参数跳转传参数 

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

void main() {
  runApp(MaterialApp(title: '页面跳转返回数据', home: FirstPage()));
}

class FirstPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("找小姐姐要电话"),
      ),
      body: Center(
        child: RouteButton(),
      ),
    );
  }
}

class RouteButton extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return RaisedButton(
      onPressed: () {
        _navigateToXiaoJieJie(context);
      },
      child: Text('去找小姐姐'),
    );
  }
}

_navigateToXiaoJieJie(BuildContext context) async {
  final result = await Navigator.push(
      context, MaterialPageRoute(builder: (context) => XiaoJie()));
  Scaffold.of(context).showSnackBar(SnackBar(content: Text('$result')));
}

class XiaoJie extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('我是小姐姐')),
      body: Center(
        child: Column(
          children: <Widget>[
            RaisedButton(
              child: Text('大长腿小姐姐'),
              onPressed: (){
                Navigator.pop(context,'大长腿小姐姐:155555555');
                
              },
            ),
            RaisedButton(
              child: Text('小蛮腰小姐姐'),
              onPressed: (){
                Navigator.pop(context,'小蛮腰小姐姐:15555888888');

              },
            ),
          ],
        ),
      ),
    );
  }
}

12.静态资源和项目图片的处理 

 在 pubspec.yaml 文件中 加入下面代码(assets必须在flutter下),在项目目录下心间images目录,把图片放在里面

flutter:
  assets:
    - images/iv_jxf_header.jpg

 

import 'package:flutter/material.dart';
void main()=>runApp(MyApp());
class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Container(
      child: Image.asset("images/iv_jxf_header.jpg"),
    );
  }
}

 13.Flutter的打包

  密钥的生成,打包步骤见下一篇文章

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

黄毛火烧雪下

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值