1.项目的创建一个flutter项目

进入Android Studio直接创建new flutter Project(如若创建时过慢也可以直接在项目目录打开命令指示符输入flutter create 项目名即可快速创建)

2.Android Studio与夜神模拟器连接

https://blog.csdn.net/weixin_42433094/article/details/113399762

3.flutter简单了解

引入import ‘package:flutter/material.dart’; flutter标准UI包
启动项目可用控制台输入flutter run启动 p显示网格(可看对齐线) o切换模式(ios和安卓切换) q关闭

4.flutter基础讲解

基础界面设置的了解

  1. import 'package:flutter/material.dart';
  2. void main() => runApp(MyApp());
  3. class MyApp extends StatelessWidget{
  4. @override
  5. Widget build(BuildContext context){
  6. return MaterialApp(
  7. title: "后台标题",
  8. home: Scaffold(
  9. appBar: AppBar(
  10. title: Text("内部标题")
  11. ),
  12. body: Center(
  13. child: Text("这是一个内部的文本,主要是内部的东西,",
  14. textAlign: TextAlign.left,//对齐方式
  15. maxLines: 1, //最多显示行 比下面那个优先级高
  16. overflow: TextOverflow.ellipsis,//超出一行省略号代替
  17. style: TextStyle( //文本字体设置
  18. fontSize: 25.0,//字体大小
  19. color: Color.fromARGB(255, 5, 125, 125),//颜色配置
  20. decoration: TextDecoration.underline,//下划线
  21. decorationStyle: TextDecorationStyle.solid,//线的属性(实线,虚线等等)
  22. ),
  23. ),
  24. ),
  25. ),
  26. );
  27. }
  28. }

Container容器的基础(类似于div)

  1. import 'package:flutter/material.dart';
  2. void main() => runApp(MyApp());
  3. class MyApp extends StatelessWidget{
  4. @override
  5. Widget build(BuildContext context){
  6. return MaterialApp(
  7. title: "后台标题",
  8. home: Scaffold(
  9. body: Center(
  10. child: Container( //类似于css的div
  11. child: new Text("Hello wwt",style: TextStyle(fontSize: 40.0)),
  12. alignment: Alignment.topLeft ,//Container下的子内容对齐方式
  13. width: 500.0, //Container块的宽
  14. height: 400.0,//Container块的高
  15. //color: Colors.lightBlue,//Container背景颜色
  16. padding: const EdgeInsets.fromLTRB(10.0,30.0,0,0),//Container容器对里面的内边距
  17. margin: const EdgeInsets.all(20.0),//Container容器对里面的外边距
  18. decoration: new BoxDecoration( //Container容器装饰
  19. gradient: const LinearGradient(//背景渐变色
  20. colors: [Colors.lightBlue,Colors.lightGreenAccent,Colors.pinkAccent,Colors.white12]//可以一直叠加
  21. ),
  22. border: Border.all(width: 5.0,color: Colors.red)//Container容器的边框
  23. ),
  24. ),
  25. ),
  26. ),
  27. );
  28. }
  29. }

图片的插入Image

微信截图_20210315104238.png

  1. import 'package:flutter/material.dart';
  2. void main() => runApp(MyApp());
  3. class MyApp extends StatelessWidget{
  4. @override
  5. Widget build(BuildContext context){
  6. return MaterialApp(
  7. title: "后台标题",
  8. home: Scaffold(
  9. body: Center(
  10. child: Container( //类似于css的div
  11. child: new Image.network(//网上图片
  12. "https://i1.hdslb.com/bfs/face/5e78623de1267feccac3bf0e20303e00e91694ca.jpg@140w_140h_1c.webp",
  13. //图片的设置;contain拉伸但不影响图片比例,fill铺满Container容器影响比例,fitWidth横向填充拉伸不影响比例但不够会被裁切,fitHeight与前面相反
  14. //fit: BoxFit.fitHeight,
  15. //color: Colors.lightBlueAccent,//图片颜色
  16. //colorBlendMode: BlendMode.modulate,//颜色模式
  17. repeat: ImageRepeat.repeat,//相对于Container容器铺满方式,
  18. ),
  19. width: 300.0,
  20. height: 200.0,
  21. color: Colors.pinkAccent,
  22. ),
  23. ),
  24. ),
  25. );
  26. }
  27. }

加载项目源文件的资源图片

image.png

  1. child: new Image.asset('images/1.jpg'),

ListView列表视图

  1. import 'package:flutter/material.dart';
  2. void main() => runApp(MyApp());
  3. class MyApp extends StatelessWidget{
  4. @override
  5. Widget build(BuildContext context){
  6. return MaterialApp(
  7. title: "后台标题",
  8. home: Scaffold(
  9. appBar: new AppBar(title: new Text("ListView图标组件"),),
  10. body: new ListView( //列表视图
  11. /* children: <Widget>[ //列表元素,文本
  12. new ListTile(
  13. leading: new Icon(Icons.perm_camera_mic), //小标签
  14. title: new Text("画虎刻鹄"), //文本
  15. ),new ListTile(
  16. leading: new Icon(Icons.repeat),
  17. title: new Text("有勇有谋"),
  18. ),
  19. ],*/
  20. children: <Widget>[ //列表插入图片
  21. new Image.asset("images/1.jpg"),
  22. new Image.network("https://i1.hdslb.com/bfs/face/5e78623de1267feccac3bf0e20303e00e91694ca.jpg@140w_140h_1c.webp"),
  23. new Image.asset("images/1.jpg"),
  24. new Image.network("https://i1.hdslb.com/bfs/face/5e78623de1267feccac3bf0e20303e00e91694ca.jpg@140w_140h_1c.webp"),
  25. ],
  26. ),
  27. ),
  28. );
  29. }
  30. }

ListView横向列表+分组件减少单体嵌套过多

  1. import 'package:flutter/material.dart';
  2. void main() => runApp(MyApp());
  3. class MyApp extends StatelessWidget{
  4. @override
  5. Widget build(BuildContext context){
  6. return MaterialApp(
  7. title: "后台标题",
  8. home: Scaffold(
  9. appBar: new AppBar(title: new Text("ListView图标组件"),),
  10. body:Center(
  11. child: Container(
  12. height: 200.0,
  13. child: MyList(), //分模块写
  14. ),
  15. ),
  16. ),
  17. );
  18. }
  19. }
  20. class MyList extends StatelessWidget{
  21. @override
  22. Widget build(BuildContext context){
  23. return ListView(
  24. scrollDirection: Axis.horizontal,//列表横向设置
  25. children: <Widget>[
  26. new Container(
  27. width: 180.0,
  28. color: Colors.indigoAccent,
  29. ),new Container(
  30. width: 180.0,
  31. color: Colors.pinkAccent,
  32. ),new Container(
  33. width: 180.0,
  34. color: Colors.deepOrangeAccent,
  35. ),
  36. ],
  37. );
  38. }
  39. }

ListView纵向动态列表+List数组+动态列表构建(写完后需要重新运行加载数据缓存)

  1. import 'package:flutter/material.dart';
  2. void main() => runApp(MyApp(
  3. items:new List<String>.generate(1000,(i)=>"列表第 $i")//自己编辑一个list
  4. ));
  5. class MyApp extends StatelessWidget{
  6. final List<String> items; //声明一个常量
  7. //固定写法 传递的参数 无名无参的父类
  8. MyApp({Key key,@required this.items}):super(key: key);
  9. @override
  10. Widget build(BuildContext context){
  11. return MaterialApp(
  12. title: "后台标题",
  13. home: Scaffold(
  14. appBar: new AppBar(title: new Text("ListView图标组件"),),
  15. body:new ListView.builder( //构建动态列表
  16. itemCount: items.length, //生成条数
  17. itemBuilder: (context,index){ //遍历list
  18. return new ListTile(
  19. title: new Text("${items[index]}"),
  20. );
  21. },
  22. ),
  23. ),
  24. );
  25. }
  26. }

GridView网格列表(图片列表)

写法一
  1. import 'package:flutter/material.dart';
  2. void main() => runApp(MyApp());
  3. class MyApp extends StatelessWidget {
  4. @override
  5. Widget build(BuildContext context) {
  6. return MaterialApp(
  7. title: "后台标题",
  8. home: Scaffold(
  9. appBar: new AppBar(title: new Text("ListView图标组件"),),
  10. body: GridView.count( //网格
  11. padding: const EdgeInsets.all(20.0),//距离外部容器的边距
  12. crossAxisCount: 3,//设置三行
  13. crossAxisSpacing: 10.0,//每个网格的边距
  14. children: <Widget>[
  15. const Text("我是a"),
  16. const Text("我是b"),
  17. const Text("我是c"),
  18. const Text("我是d"),
  19. const Text("我是e"),
  20. const Text("我是f"),
  21. ],
  22. ),
  23. ),
  24. );
  25. }
  26. }

写法二

fit: BoxFit.cover填满容器裁切,图片的属性,fit可以完美配合容器进行裁切

  1. import 'package:flutter/material.dart';
  2. void main() => runApp(MyApp());
  3. class MyApp extends StatelessWidget {
  4. @override
  5. Widget build(BuildContext context) {
  6. return MaterialApp(
  7. title: "后台标题",
  8. home: Scaffold(
  9. appBar: new AppBar(title: new Text("ListView图标组件"),),
  10. body: GridView(
  11. gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
  12. crossAxisCount: 3,//一行三个
  13. mainAxisSpacing: 2.0,//上下两行的边距
  14. crossAxisSpacing: 2.0,//左右边距
  15. childAspectRatio: 0.7,//图片比例 宽高比
  16. ),
  17. children: <Widget>[
  18. new Image.network("http://img5.mtime.cn/mt/2021/03/04/101410.25280244_1280X720X2.jpg",fit: BoxFit.cover),//fit: BoxFit.cover填满容器裁切
  19. new Image.network("http://img5.mtime.cn/mt/2021/01/06/094232.18209247_1280X720X2.jpg",fit: BoxFit.cover),
  20. new Image.network("http://img5.mtime.cn/mt/2021/03/04/103317.70446282_1280X720X2.jpg",fit: BoxFit.cover),
  21. new Image.network("http://img5.mtime.cn/mt/2021/03/11/094158.30870791_1280X720X2.jpg",fit: BoxFit.cover),
  22. new Image.network("http://img5.mtime.cn/mt/2021/02/04/143001.40483517_1280X720X2.jpg",fit: BoxFit.cover),
  23. new Image.network("http://img5.mtime.cn/mt/2021/03/09/200645.34627873_1280X720X2.jpg",fit: BoxFit.cover),
  24. ],
  25. ),
  26. ),
  27. );
  28. }
  29. }