当前位置:   article > 正文

【Flutter入门到进阶】Flutter基础篇---布局_flutter 网格布局

flutter 网格布局

1 GridView网格布局组件

1.1 说明

1.1.1 图例

1.1.2 说明

        GridView网格布局在实际项目中用的也是非常多的,当我们想让可以滚动的元素使用矩阵方式排列的时 候。此时我们可以用网格列表组件GridView实现布局

GridView创建网格列表主要有下面三种方式

        1、可以通过GridView.count 实现网格布局

        2、可以通过GridView.extent 实现网格布局

        3、通过GridView.builder实现动态网格布局

1.2 常用属性

属性

1.3 案例

1.3.1 GridView.count 实现网格布局

        GridView.count构造函数内部使用了SliverGridDelegateWithFixedCrossAxisCount,我们通过它可以
        快速的创建横轴固定数量子元素的GridView

示例

  1. //GridView.count 实现网格布局
  2. class HomePage extends StatelessWidget {
  3.   const HomePage({Key? key}) : super(key: key);
  4.   @override
  5.   Widget build(BuildContext context) {
  6.     return GridView.count(
  7.       crossAxisCount: 3,
  8.       childAspectRatio: 1.0,
  9.       children: const <Widget>[
  10.         Icon(Icons.home),
  11.         Icon(Icons.ac_unit),
  12.         Icon(Icons.search),
  13.         Icon(Icons.settings),
  14.         Icon(Icons.airport_shuttle),
  15.         Icon(Icons.all_inclusive),
  16.         Icon(Icons.beach_access),
  17.         Icon(Icons.cake),
  18.         Icon(Icons.circle),
  19.       ],
  20.     );
  21.   }
  22. }

1.3.2 GridView.extent实现网格布局

        GridView.extent构造函数内部使用了SliverGridDelegateWithMaxCrossAxisExtent,我们通过它可以 快速的创建横轴子元素为固定最大长度的的GridView

  1. //GridView.extent实现网格布局
  2. class HomePage2 extends StatelessWidget {
  3. const HomePage2({Key? key}) : super(key: key);
  4. @override
  5. Widget build(BuildContext context) {
  6. return GridView.extent(
  7. maxCrossAxisExtent: 80.0,
  8. childAspectRatio: 1.0,
  9. children: const <Widget>[
  10. Icon(Icons.home),
  11. Icon(Icons.ac_unit),
  12. Icon(Icons.search),
  13. Icon(Icons.settings),
  14. Icon(Icons.airport_shuttle),
  15. Icon(Icons.all_inclusive),
  16. Icon(Icons.beach_access),
  17. Icon(Icons.cake),
  18. Icon(Icons.circle),
  19. ],
  20. );
  21. }
  22. }

1.3.3 GridView.count 和 GridView.extent属性详解

  1. //GridView.count 和 GridView.extent属性详解
  2. class HomePage3 extends StatelessWidget {
  3.   const HomePage3({Key? key}) : super(key: key);
  4.   List<Widget> _getListData() {
  5.     List<Widget> list = [];
  6.     for (var i = 0; i < 20; i++) {
  7.       list.add(Container(
  8.         alignment: Alignment.center,
  9.         color: Colors.blue,
  10.         child: Text(
  11.           '这是第$i条数据',
  12.           style: const TextStyle(color: Colors.white, fontSize: 20),
  13.         ),
  14. // height: 400,  //设置高度没有反应
  15.       ));
  16.     }
  17.     return list;
  18.   }
  19.   @override
  20.   Widget build(BuildContext context) {
  21.     return GridView.count(
  22.       crossAxisSpacing: 20.0,
  23.       //水平子 Widget 之间间距
  24.       mainAxisSpacing: 20.0,
  25.       //垂直子 Widget 之间间距
  26.       padding: const EdgeInsets.all(10),
  27.       crossAxisCount: 2,
  28.       //一行的 Widget 数量
  29.       childAspectRatio: 0.8,
  30.       //宽度和高度的比例
  31.       children: _getListData(),
  32.     );
  33.   }
  34. }

1.3.4 GridView实现动态列表

GridView.count实现动态列表

  1. // GridView.count 实现动态列表
  2. class HomePage extends StatelessWidget {
  3. const HomePage({Key? key}) : super(key: key);
  4. List<Widget> _getListData() {
  5. var tempList = listData.map((value) {
  6. return Container(
  7. decoration: BoxDecoration(
  8. border: Border.all(
  9. color: const Color.fromRGBO(233, 233, 233, 0.9), width: 1)),
  10. child: Column(
  11. children: <Widget>[
  12. Image.asset(value['imageUrl']),
  13. const SizedBox(height: 12),
  14. Text(
  15. value['title'],
  16. textAlign: TextAlign.center,
  17. style: const TextStyle(fontSize: 20),
  18. )
  19. ],
  20. ),
  21. );
  22. }); // ('xxx','xxx')
  23. return tempList.toList();
  24. }
  25. @override
  26. Widget build(BuildContext context) {
  27. return GridView.count(
  28. crossAxisSpacing: 10.0,
  29. //水平子 Widget 之间间距
  30. mainAxisSpacing: 10.0,
  31. //垂直子 Widget 之间间距
  32. padding: const EdgeInsets.all(10),
  33. crossAxisCount: 2,
  34. //一行的 Widget 数量
  35. childAspectRatio: 0.7,
  36. //宽度和高度的比例
  37. children: _getListData(),
  38. );
  39. }
  40. }

GridView.build实现动态列表

  1. // GridView.builder实现动态列表
  2. class HomePage extends StatelessWidget {
  3. const HomePage({Key? key}) : super(key: key);
  4. Widget _getListData(context, index) {
  5. return Container(
  6. decoration: BoxDecoration(
  7. border: Border.all(
  8. color: const Color.fromRGBO(233, 233, 233, 0.9), width: 1)),
  9. child: Column(
  10. children: <Widget>[
  11. Image.asset(listData[index]['imageUrl']),
  12. const SizedBox(height: 12),
  13. Text(
  14. listData[index]['title'],
  15. textAlign: TextAlign.center,
  16. style: const TextStyle(fontSize: 20),
  17. )
  18. ],
  19. ),
  20. height: 400,
  21. //设置高度没有反应
  22. );
  23. }
  24. @override
  25. Widget build(BuildContext context) {
  26. return GridView.builder(
  27. //注意
  28. gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
  29. crossAxisSpacing: 10.0, //水平子 Widget 之间间距
  30. mainAxisSpacing: 10.0, //垂直子 Widget 之间间距
  31. crossAxisCount: 2, //一行的 Widget 数量
  32. ),
  33. itemCount: listData.length,
  34. itemBuilder: _getListData,
  35. );
  36. }
  37. }

2 Flutter Paddiing组件

2.1 说明        

        在html中常见的布局标签都有padding属性,但是Flutter中很多Widget是没有padding属性。这个时候 我们可以用Padding组件处理容器与子元素之间的间距

2.2 属性

        padding:padding值, EdgeInsetss设置填充的值

        child:子组件

2.3 示例

  1. import 'package:flutter/material.dart';
  2. import '../../res/listData.dart';
  3. void main() {
  4. runApp(const MyApp());
  5. }
  6. class MyApp extends StatelessWidget {
  7. const MyApp({Key? key})
  8. : super(key: key); // This widget is the root of your application.
  9. @override
  10. Widget build(BuildContext context) {
  11. return MaterialApp(
  12. title: 'Flutter Demo',
  13. theme: ThemeData(
  14. primarySwatch: Colors.blue,
  15. ),
  16. home: Scaffold(
  17. appBar: AppBar(title: const Text("FLutter App")),
  18. body: const HomePage(),
  19. ),
  20. );
  21. }
  22. }
  23. class HomePage extends StatelessWidget {
  24. const HomePage({Key? key}) : super(key: key);
  25. @override
  26. Widget build(BuildContext context) {
  27. return GridView.count(
  28. padding: const EdgeInsets.all(10),
  29. crossAxisCount: 2,
  30. childAspectRatio: 1,
  31. children: [
  32. Padding(
  33. padding: const EdgeInsets.all(10),
  34. child: Image.asset('images/1.png', fit: BoxFit.cover),
  35. ),
  36. Padding(
  37. padding: const EdgeInsets.all(10),
  38. child: Image.asset('images/2.png', fit: BoxFit.cover),
  39. ),
  40. Padding(
  41. padding: const EdgeInsets.all(10),
  42. child: Image.asset('images/3.png', fit: BoxFit.cover),
  43. ),
  44. Padding(
  45. padding: const EdgeInsets.all(10),
  46. child: Image.asset('images/4.png', fit: BoxFit.cover),
  47. ),
  48. Padding(
  49. padding: const EdgeInsets.all(10),
  50. child: Image.asset('images/5.png', fit: BoxFit.cover),
  51. ),
  52. Padding(
  53. padding: const EdgeInsets.all(10),
  54. child: Image.asset('images/6.png', fit: BoxFit.cover),
  55. ),
  56. ],
  57. );
  58. }
  59. }

3 线性布局(Row和Column)

3.1 Row 水平布局组件

3.1.1 Row属性

        mainAxisAlignment 主轴的排序方式

        crossAxisAlignment 次轴的排序方式

        children 组件子元素

3.1.2 示例

  1. import 'package:flutter/material.dart';
  2. import '../../res/listData.dart';
  3. void main() {
  4. runApp(const MyApp());
  5. }
  6. class MyApp extends StatelessWidget {
  7. const MyApp({Key? key})
  8. : super(key: key); // This widget is the root of your application.
  9. @override
  10. Widget build(BuildContext context) {
  11. return MaterialApp(
  12. title: 'Flutter Demo',
  13. theme: ThemeData(
  14. primarySwatch: Colors.blue,
  15. ),
  16. home: Scaffold(
  17. appBar: AppBar(title: const Text("FLutter App")),
  18. body: const HomePage(),
  19. ),
  20. );
  21. }
  22. }
  23. class HomePage extends StatelessWidget {
  24. const HomePage({Key? key}) : super(key: key);
  25. @override
  26. Widget build(BuildContext context) {
  27. return Container(
  28. height: double.infinity,
  29. width: double.infinity,
  30. color: Colors.black26,
  31. child: Row(
  32. crossAxisAlignment: CrossAxisAlignment.center,
  33. mainAxisAlignment: MainAxisAlignment.spaceEvenly,
  34. children: [
  35. IconContainer(Icons.home, color: Colors.red),
  36. IconContainer(Icons.search, color: Colors.blue),
  37. IconContainer(Icons.send, color: Colors.orange),
  38. ],
  39. ),
  40. );
  41. }
  42. }
  43. class IconContainer extends StatelessWidget {
  44. Color color;
  45. double size;
  46. IconData icon;
  47. IconContainer(this.icon,
  48. {Key? key, this.color = Colors.red, this.size = 32.0})
  49. : super(key: key);
  50. @override
  51. Widget build(BuildContext context) {
  52. return Container(
  53. height: 100.0,
  54. width: 100.0,
  55. color: color,
  56. child: Center(child: Icon(icon, size: size, color: Colors.white)),
  57. );
  58. }
  59. }

3.2 Column垂直布局组件

3.2.1 Column属性

        mainAxisAlignment 主轴的排序方式

        crossAxisAlignment 次轴的排序方式

        children 组件子元素

3.2.2 示例

  1. import 'package:flutter/material.dart';
  2. void main() {
  3. runApp(const MyApp());
  4. }
  5. class MyApp extends StatelessWidget {
  6. const MyApp({Key? key})
  7. : super(key: key); // This widget is the root of your application.
  8. @override
  9. Widget build(BuildContext context) {
  10. return MaterialApp(
  11. title: 'Flutter Demo',
  12. theme: ThemeData(
  13. primarySwatch: Colors.blue,
  14. ),
  15. home: Scaffold(
  16. appBar: AppBar(title: const Text("Flutter App")),
  17. body: const HomePage(),
  18. ),
  19. );
  20. }
  21. }
  22. class HomePage extends StatelessWidget {
  23. const HomePage({Key? key}) : super(key: key);
  24. @override
  25. Widget build(BuildContext context) {
  26. return Container(
  27. height: double.infinity,
  28. width: double.infinity,
  29. color: Colors.black26,
  30. child: Column(
  31. crossAxisAlignment: CrossAxisAlignment.center,
  32. mainAxisAlignment: MainAxisAlignment.spaceEvenly,
  33. children: [
  34. IconContainer(Icons.home, color: Colors.red),
  35. IconContainer(Icons.search, color: Colors.blue),
  36. IconContainer(Icons.send, color: Colors.orange),
  37. ],
  38. ),
  39. );
  40. }
  41. }
  42. class IconContainer extends StatelessWidget {
  43. Color color;
  44. double size;
  45. IconData icon;
  46. IconContainer(this.icon,
  47. {Key? key, this.color = Colors.red, this.size = 32.0})
  48. : super(key: key);
  49. @override
  50. Widget build(BuildContext context) {
  51. return Container(
  52. height: 100.0,
  53. width: 100.0,
  54. color: color,
  55. child: Center(child: Icon(icon, size: size, color: Colors.white)),
  56. );
  57. }
  58. }

3.3 double.infinity 和double.maxFinite

3.3.1 说明

double.infinity 和double.maxFinite可以让当前元素的width或者height达到父元素的尺寸

3.3.2 底层代码

static const double nan = 0.0 / 0.0; static const double infinity = 1.0 / 0.0; static const double negativeInfinity = -infinity; static const double minPositive = 5e-324; static const double maxFinite = 1.7976931348623157e+308;

3.3.3 如下可以让Container铺满整个屏幕

  1. Widget build(BuildContext context) {
  2.     return Container(
  3.       height: double.infinity,
  4.       width: double.infinity,
  5.       color: Colors.black26,
  6.       child: Column(
  7.         crossAxisAlignment: CrossAxisAlignment.center,
  8.         mainAxisAlignment: MainAxisAlignment.spaceEvenly,
  9.         children: [
  10.           IconContainer(Icons.home, color: Colors.red),
  11.           IconContainer(Icons.search, color: Colors.blue),
  12.           IconContainer(Icons.send, color: Colors.orange),
  13.         ],
  14.       ),
  15.     );
  16.   }


 

3.3.4 如下可以让Container的宽度和高度等于父元素的宽度高度

  1. @override
  2. Widget build(BuildContext context) {
  3. return Container(
  4. height: 400,
  5. width: 600,
  6. color: Colors.red,
  7. child: Container(
  8. height: double.maxFinite,
  9. width: double.infinity,
  10. color: Colors.black26,
  11. child: Column(
  12. crossAxisAlignment: CrossAxisAlignment.center,
  13. mainAxisAlignment: MainAxisAlignment.spaceEvenly,
  14. children: [
  15. IconContainer(Icons.home, color: Colors.red),
  16. IconContainer(Icons.search, color: Colors.blue),
  17. IconContainer(Icons.send, color: Colors.orange),
  18. ],
  19. ),
  20. ),
  21. );
  22. }

3.4 弹性布局(Flex Expanded)

3.4.1 说明

Flex组件可以沿着水平或垂直方向排列子组件,如果你知道主轴方向,使用 Row或 Column会方便一 些,  因为 Row和 Column都继承自 Flex ,参数基本相同,所以能使用Flex的地方基本上都可以使用    Row或 Column 。 Flex本身功能是很强大的,它也可以和 Expanded组件配合实现弹性布局 。

3.4.2 水平弹性布局

示例1

  1. import 'package:flutter/material.dart';
  2. void main() {
  3.   runApp(const MyApp());
  4. }
  5. class MyApp extends StatelessWidget {
  6.   const MyApp({Key? key}) : super(key: key);
  7. // This widget is the root of your application.
  8.   @override
  9.   Widget build(BuildContext context) {
  10.     return MaterialApp(
  11.       title: 'Flutter Demo',
  12.       theme: ThemeData(
  13.         primarySwatch: Colors.blue,
  14.       ),
  15.       home: Scaffold(
  16.         appBar: AppBar(title: const Text("Flutter App")),
  17.         body: const HomePage(),
  18.       ),
  19.     );
  20.   }
  21. }
  22. class HomePage extends StatelessWidget {
  23.   const HomePage({Key? key}) : super(key: key);
  24.   @override
  25.   Widget build(BuildContext context) {
  26.     return Flex(
  27.       direction: Axis.horizontal,
  28.       children: [
  29.         Expanded(flex: 2, child: IconContainer(Icons.home, color: Colors.red)),
  30.         Expanded(
  31.           flex: 1,
  32.           child: IconContainer(Icons.search, color: Colors.orange),
  33.         )
  34.       ],
  35.     );
  36.   }
  37. }
  38. class IconContainer extends StatelessWidget {
  39.   Color color;
  40.   double size;
  41.   IconData icon;
  42.   IconContainer(this.icon,
  43.       {Key? key, this.color = Colors.red, this.size = 32.0})
  44.       : super(key: key);
  45.   @override
  46.   Widget build(BuildContext context) {
  47.     return Container(
  48.       height: 100.0,
  49.       width: 100.0,
  50.       color: color,
  51.       child: Center(child: Icon(icon, size: size, color: Colors.white)),
  52.     );
  53.   }
  54. }

示例2

  1. import 'package:flutter/material.dart';
  2. void main() {
  3. runApp(const MyApp());
  4. }
  5. class MyApp extends StatelessWidget {
  6. const MyApp({Key? key})
  7. : super(key: key); // This widget is the root of your application.
  8. @override
  9. Widget build(BuildContext context) {
  10. return MaterialApp(
  11. title: 'Flutter Demo',
  12. theme: ThemeData(
  13. primarySwatch: Colors.blue,
  14. ),
  15. home: Scaffold(
  16. appBar: AppBar(title: const Text("Flutter App")),
  17. body: const HomePage(),
  18. ),
  19. );
  20. }
  21. }
  22. class HomePage extends StatelessWidget {
  23. const HomePage({Key? key}) : super(key: key);
  24. @override
  25. Widget build(BuildContext context) {
  26. return Row(
  27. children: [
  28. Expanded(flex: 2, child: IconContainer(Icons.home, color: Colors.red)),
  29. Expanded(
  30. flex: 1,
  31. child: IconContainer(Icons.search, color: Colors.orange),
  32. )
  33. ],
  34. );
  35. }
  36. }
  37. class IconContainer extends StatelessWidget {
  38. Color color;
  39. double size;
  40. IconData icon;
  41. IconContainer(this.icon,
  42. {Key? key, this.color = Colors.red, this.size = 32.0})
  43. : super(key: key);
  44. @override
  45. Widget build(BuildContext context) {
  46. return Container(
  47. height: 100.0,
  48. width: 100.0,
  49. color: color,
  50. child: Center(child: Icon(icon, size: size, color: Colors.white)),
  51. );
  52. }
  53. }

3.4.3 垂直弹性布局

  1. import 'package:flutter/material.dart';
  2. void main() {
  3.   runApp(const MyApp());
  4. }
  5. class MyApp extends StatelessWidget {
  6.   const MyApp({Key? key}) : super(key: key);
  7. // This widget is the root of your application.
  8.   @override
  9.   Widget build(BuildContext context) {
  10.     return MaterialApp(
  11.       title: 'Flutter Demo',
  12.       theme: ThemeData(
  13.         primarySwatch: Colors.blue,
  14.       ),
  15.       home: Scaffold(
  16.         appBar: AppBar(title: const Text("Flutter App")),
  17.         body: const HomePage(),
  18.       ),
  19.     );
  20.   }
  21. }
  22. class HomePage extends StatelessWidget {
  23.   const HomePage({Key? key}) : super(key: key);
  24.   @override
  25.   Widget build(BuildContext context) {
  26.     return Column(
  27.       children: [
  28.         Expanded(flex: 2, child: IconContainer(Icons.home, color: Colors.red)),
  29.         Expanded(
  30.           flex: 1,
  31.           child: IconContainer(Icons.search, color: Colors.orange),
  32.         )
  33.       ],
  34.     );
  35.   }
  36. }
  37. class IconContainer extends StatelessWidget {
  38.   Color color;
  39.   double size;
  40.   IconData icon;
  41.   IconContainer(this.icon,
  42.       {Key? key, this.color = Colors.red, this.size = 32.0})
  43.       : super(key: key);
  44.   @override
  45.   Widget build(BuildContext context) {
  46.     return Container(
  47.       height: 100.0,
  48.       width: 100.0,
  49.       color: color,
  50.       child: Center(child: Icon(icon, size: size, color: Colors.white)),
  51.     );
  52.   }
  53. }

3.4.4 使用 Row 或 Co1umn 结合Expanded实现下面示例

示例图

代码

  1. import 'package:flutter/material.dart';
  2. void main() {
  3.   runApp(const MyApp());
  4. }
  5. class MyApp extends StatelessWidget {
  6.   const MyApp({Key? key}) : super(key: key);
  7. // This widget is the root of your application.
  8.   @override
  9.   Widget build(BuildContext context) {
  10.     return MaterialApp(
  11.       title: 'Flutter Demo',
  12.       theme: ThemeData(
  13.         primarySwatch: Colors.blue,
  14.       ),
  15.       home: Scaffold(
  16.         appBar: AppBar(title: const Text("Flutter App")),
  17.         body: const HomePage(),
  18.       ),
  19.     );
  20.   }
  21. }
  22. class HomePage extends StatelessWidget {
  23.   const HomePage({Key? key}) : super(key: key);
  24.   @override
  25.   Widget build(BuildContext context) {
  26.     return ListView(
  27.       children: [
  28.         Container(
  29.           width: double.infinity,
  30.           height: 200,
  31.           color: Colors.black,
  32.         ),
  33.         const SizedBox(height: 10),
  34.         Row(
  35.           children: [
  36.             Expanded(
  37.               flex: 2,
  38.               child: SizedBox(
  39.                 height: 180,
  40.                 child: Image.asset(
  41.                     "images/2.png",
  42.                     fit: BoxFit.cover),
  43.               ),
  44.             ),
  45.             const SizedBox(width: 10),
  46.             Expanded(
  47.                 flex: 1,
  48.                 child: SizedBox(
  49.                   height: 180,
  50.                   child: Column(
  51.                     children: [
  52.                       Expanded(
  53.                         flex: 1,
  54.                         child: SizedBox(
  55.                           width: double.infinity,
  56.                           child: Image.asset(
  57.                               "images/3.png",
  58.                               fit: BoxFit.cover),
  59.                         ),
  60.                       ),
  61.                       const SizedBox(height: 10),
  62.                       Expanded(
  63.                         flex: 2,
  64.                         child: SizedBox(
  65.                           width: double.infinity,
  66.                           child: Image.asset(
  67.                               "images/4.png",
  68.                               fit: BoxFit.cover),
  69.                         ),
  70.                       )
  71.                     ],
  72.                   ),
  73.                 ))
  74.           ],
  75.         )
  76.       ],
  77.     );
  78.   }
  79. }

4 Flutter Wrap组件

4.1 说明

        Wrap可以实现流布局,单行的Wrap跟Row表现几乎一致,单列的Wrap则跟Column表现几乎一致。但 Row与Column都是单行单列的,  Wrap则突破了这个限制,  mainAxis上空间不足时,则向crossAxis上 去扩展显示。

4.2 属性

4.2.1 direction

        主轴的方向,默认水平

4.2.2 alignment

        主轴的对其方式

4.2.3 spacing

        主轴方向上的间距

4.2.4 textDirection

        文本方向

4.2.5 verticalDirection

        定义了children摆放顺序,默认是down,见Flex相关属性介绍。

4.2.6 runAlignment

        run的对齐方式。  run可以理解为新的行或者列,如果是水平方向布局的话, run可以理解为新的一行

4.2.7 runSpacing

        run的间距

4.3 示例

  1. import 'package:flutter/material.dart';
  2. import '../../res/listData.dart';
  3. void main() {
  4. runApp(const MyApp());
  5. }
  6. class MyApp extends StatelessWidget {
  7. const MyApp({Key? key})
  8. : super(key: key); // This widget is the root of your application.
  9. @override
  10. Widget build(BuildContext context) {
  11. return MaterialApp(
  12. title: 'Flutter Demo',
  13. theme: ThemeData(
  14. primarySwatch: Colors.blue,
  15. ),
  16. home: Scaffold(
  17. appBar: AppBar(title: const Text("FLutter App")),
  18. body: const LayoutDemo(),
  19. ),
  20. );
  21. }
  22. }
  23. class LayoutDemo extends StatelessWidget {
  24. const LayoutDemo({Key? key}) : super(key: key);
  25. @override
  26. Widget build(BuildContext context) {
  27. return Padding(
  28. padding: const EdgeInsets.all(3),
  29. child: Wrap(
  30. spacing: 5,
  31. runSpacing: 5,
  32. direction: Axis.vertical,
  33. alignment: WrapAlignment.start,
  34. runAlignment: WrapAlignment.center,
  35. children: <Widget>[
  36. Button("第1集", onPressed: () {}),
  37. Button("第2集", onPressed: () {}),
  38. Button("第3集", onPressed: () {}),
  39. Button("第4集", onPressed: () {}),
  40. Button("第5集", onPressed: () {}),
  41. Button("第6集", onPressed: () {}),
  42. Button("第7集", onPressed: () {}),
  43. Button("第8集", onPressed: () {}),
  44. Button("第9集", onPressed: () {}),
  45. Button("第10集", onPressed: () {}),
  46. Button("第11集", onPressed: () {}),
  47. Button("第12集", onPressed: () {}),
  48. Button("第13集", onPressed: () {}),
  49. Button("第14集", onPressed: () {}),
  50. Button("第15集", onPressed: () {}),
  51. Button("第16集", onPressed: () {}),
  52. Button("第17集", onPressed: () {}),
  53. Button("第18集", onPressed: () {}),
  54. ],
  55. ),
  56. );
  57. }
  58. }
  59. class Button extends StatelessWidget {
  60. String text;
  61. void Function()? onPressed;
  62. Button(this.text, {Key? key, required this.onPressed}) : super(key: key);
  63. @override
  64. Widget build(BuildContext context) {
  65. return ElevatedButton(
  66. onPressed: onPressed,
  67. style: ButtonStyle(
  68. backgroundColor:
  69. MaterialStateProperty.all(const Color.fromARGB(255, 236, 233, 233)),
  70. foregroundColor: MaterialStateProperty.all(Colors.black45),
  71. ),
  72. child: Text(text),
  73. );
  74. }
  75. }

4.4 示例2

  1. import 'package:flutter/material.dart';
  2. void main() {
  3. runApp(const MyApp());
  4. }
  5. class MyApp extends StatelessWidget {
  6. const MyApp({Key? key})
  7. : super(key: key); // This widget is the root of your application.
  8. @override
  9. Widget build(BuildContext context) {
  10. return MaterialApp(
  11. title: 'Flutter Demo',
  12. theme: ThemeData(
  13. primarySwatch: Colors.blue,
  14. ),
  15. home: Scaffold(
  16. appBar: AppBar(title: const Text("Flutter App")),
  17. body: const LayoutDemo(),
  18. ),
  19. );
  20. }
  21. }
  22. class LayoutDemo extends StatelessWidget {
  23. const LayoutDemo({Key? key}) : super(key: key);
  24. @override
  25. Widget build(BuildContext context) {
  26. return Padding(
  27. padding: const EdgeInsets.all(10),
  28. child: ListView(children: [
  29. Row(
  30. children: [
  31. Text(
  32. "热搜",
  33. style: Theme.of(context).textTheme.headline6,
  34. )
  35. ],
  36. ),
  37. const Divider(),
  38. Wrap(
  39. spacing: 10,
  40. runSpacing: 12,
  41. children: [
  42. Button("女装", onPressed: () {}),
  43. Button("笔记本", onPressed: () {}),
  44. Button("玩具", onPressed: () {}),
  45. Button("文学", onPressed: () {}),
  46. Button("女装", onPressed: () {}),
  47. Button("时尚", onPressed: () {}),
  48. Button("女装", onPressed: () {}),
  49. Button("女装", onPressed: () {}),
  50. ],
  51. ),
  52. const SizedBox(height: 10),
  53. Row(
  54. children: [
  55. Text(
  56. "历史记录",
  57. style: Theme.of(context).textTheme.headline6,
  58. )
  59. ],
  60. ),
  61. const Divider(),
  62. Column(
  63. children: const [
  64. ListTile(
  65. title: Text("女装"),
  66. ),
  67. Divider(),
  68. ListTile(
  69. title: Text("时尚"),
  70. ),
  71. Divider(),
  72. ],
  73. ),
  74. const SizedBox(height: 40),
  75. Padding(
  76. padding: const EdgeInsets.all(20),
  77. child: OutlinedButton.icon(
  78. onPressed: () {},
  79. style: ButtonStyle(
  80. foregroundColor: MaterialStateProperty.all(Colors.black38)),
  81. icon: const Icon(Icons.delete),
  82. label: const Text("清空历史记录")),
  83. )
  84. ]),
  85. );
  86. }
  87. }
  88. class Button extends StatelessWidget {
  89. String text;
  90. void Function()? onPressed;
  91. Button(this.text, {Key? key, required this.onPressed}) : super(key: key);
  92. @override
  93. Widget build(BuildContext context) {
  94. return ElevatedButton(
  95. onPressed: onPressed,
  96. style: ButtonStyle(
  97. backgroundColor:
  98. MaterialStateProperty.all(const Color.fromARGB(255, 236, 233, 233)),
  99. foregroundColor: MaterialStateProperty.all(Colors.black45),
  100. ),
  101. child: Text(text),
  102. );
  103. }
  104. }

5 层叠布局(Stack、  Align、Positioned)

5.1 Flutter Stack组件

5.1.1 说明

        Stack表示堆的意思,我们可以用Stack或者Stack结合Align或者Stack结合 Positiond来实现页面的定位 布局

5.1.2 属性

        alignment:配置所有子元素的显示位置

        children:子组件

5.1.3 示例

  1. import 'package:flutter/material.dart';
  2. void main() {
  3.   runApp(const MyApp());
  4. }
  5. class MyApp extends StatelessWidget {
  6.   const MyApp({Key? key}) : super(key: key);
  7. // This widget is the root of your application.
  8.   @override
  9.   Widget build(BuildContext context) {
  10.     return MaterialApp(
  11.       title: 'Flutter Demo',
  12.       theme: ThemeData(
  13.         primarySwatch: Colors.blue,
  14.       ),
  15.       home: Scaffold(
  16.         appBar: AppBar(title: const Text("Flutter App")),
  17.         body: const HomePage(),
  18.       ),
  19.     );
  20.   }
  21. }
  22. class HomePage extends StatelessWidget {
  23.   const HomePage({Key? key}) : super(key: key);
  24.   @override
  25.   Widget build(BuildContext context) {
  26.     return Container(
  27.       height: 120.0,
  28.       width: 120.0,
  29.       color: Colors.blue.shade50,
  30.       child: const Align(
  31.         alignment: Alignment.topRight,
  32.         child: FlutterLogo(
  33.           size: 60,
  34.         ),
  35.       ),
  36.     );
  37.   }
  38. }
  39. //Align结合Alignment 参数
  40. class HomePage2 extends StatelessWidget {
  41.   const HomePage2({Key? key}) : super(key: key);
  42.   @override
  43.   Widget build(BuildContext context) {
  44.     return Container(
  45.         height: 120.0,
  46.         width: 120.0,
  47.         color: Colors.blue.shade50,
  48.         child: const Align(
  49.           alignment: Alignment(20.0),
  50.           child: FlutterLogo(
  51.             size: 60,
  52.           ),
  53.         ));
  54.   }
  55. }
  56. //Align结合Stack组件
  57. class HomePage3 extends StatelessWidget {
  58.   const HomePage3({Key? key}) : super(key: key);
  59.   @override
  60.   Widget build(BuildContext context) {
  61.     return Center(
  62.       child: Container(
  63.         height: 400,
  64.         width: 300,
  65.         color: Colors.red,
  66.         child: Stack(
  67.         // alignment: Alignment.center,
  68.           children: const <Widget>[
  69.             Align(
  70.               alignment: Alignment(1-0.2),
  71.               child: Icon(Icons.home, size: 40, color: Colors.white),
  72.             ),
  73.             Align(
  74.               alignment: Alignment.center,
  75.               child: Icon(Icons.search, size: 30, color: Colors.white),
  76.             ),
  77.             Align(
  78.               alignment: Alignment.bottomRight,
  79.               child: Icon(Icons.settings_applications,
  80.                   size: 30, color: Colors.white),
  81.             )
  82.           ],
  83.         ),
  84.       ),
  85.     );
  86.   }
  87. }

5.2 Flutter Stack Align

5.2.1 说明

        Align组件可以调整子组件的位置 , Stack组件中结合Align组件也可以控制每个子元素的显示位置

5.2.2 属性

        alignment:配置所有子元素的显示位置

        child:子组件

5.2.3 Align结合Container的使用

说明  

        FlutterLogo 是Flutter SDK 提供的一个组件,内容就是 Flutter 的 log

示例

  1. import 'package:flutter/material.dart';
  2. void main() {
  3. runApp(const MyApp());
  4. }
  5. class MyApp extends StatelessWidget {
  6. const MyApp({Key? key})
  7. : super(key: key); // This widget is the root of your application.
  8. @override
  9. Widget build(BuildContext context) {
  10. return MaterialApp(
  11. title: 'Flutter Demo',
  12. theme: ThemeData(
  13. primarySwatch: Colors.blue,
  14. ),
  15. home: Scaffold(
  16. appBar: AppBar(title: const Text("Flutter App")),
  17. body: const HomePage(),
  18. ),
  19. );
  20. }
  21. }
  22. class HomePage extends StatelessWidget {
  23. const HomePage({Key? key}) : super(key: key);
  24. @override
  25. Widget build(BuildContext context) {
  26. return Center(
  27. child: Stack(
  28. alignment: Alignment.topLeft,
  29. children: [
  30. Container(
  31. height: 400,
  32. width: 300,
  33. color: Colors.red,
  34. ),
  35. const Text(
  36. "这是一个文本",
  37. style: TextStyle(fontSize: 40, color: Colors.amber),
  38. )
  39. ],
  40. ),
  41. );
  42. }
  43. }

5.3 Flutter Stack Positioned

5.3.1 说明

        Stack组件中结合Positioned组件也可以控制每个子元素的显示位置

5.3.2 属性

        top:子元素距离顶部的距离

        bottom:子元素距离底部的距离

        left:子元素距离左侧距离

        right:子元素距离右侧距离

        child:子组件

        width:组件的高度   (注意:宽度和高度必须是固定值,没法使用double.infinity)

        height:子组件的高度

示例

  1. import 'package:flutter/material.dart';
  2. void main() {
  3. runApp(const MyApp());
  4. }
  5. class MyApp extends StatelessWidget {
  6. const MyApp({Key? key})
  7. : super(key: key); // This widget is the root of your application.
  8. @override
  9. Widget build(BuildContext context) {
  10. return MaterialApp(
  11. title: 'Flutter Demo',
  12. theme: ThemeData(
  13. primarySwatch: Colors.blue,
  14. ),
  15. home: Scaffold(
  16. appBar: AppBar(title: const Text("Flutter App")),
  17. body: const HomePage(),
  18. ),
  19. );
  20. }
  21. } //Stack组件中结合Positioned组件也可以控制每个子元素的显示位置
  22. class HomePage extends StatelessWidget {
  23. const HomePage({Key? key}) : super(key: key);
  24. @override
  25. Widget build(BuildContext context) {
  26. // FlutterMediaQuery获取屏幕宽度和高度
  27. final size = MediaQuery.of(context).size;
  28. final width = size.width;
  29. final height = size.height;
  30. return Center(
  31. child: Container(
  32. height: height,
  33. width: width,
  34. color: Colors.red,
  35. child: Stack(
  36. alignment: Alignment.center,
  37. children: const <Widget>[
  38. Positioned(
  39. left: 10,
  40. child: Icon(Icons.home, size: 40, color: Colors.white),
  41. ),
  42. Positioned(
  43. bottom: 0,
  44. left: 100,
  45. child: Icon(Icons.search, size: 30, color: Colors.white),
  46. ),
  47. Positioned(
  48. right: 0,
  49. child: Icon(Icons.settings_applications,
  50. size: 30, color: Colors.white),
  51. )
  52. ],
  53. ),
  54. ),
  55. );
  56. }
  57. }
  58. // Flutter Stack Positioned固定导航案例
  59. class HomePage2 extends StatelessWidget {
  60. const HomePage2({Key? key}) : super(key: key);
  61. @override
  62. Widget build(BuildContext context) {
  63. final size = MediaQuery.of(context).size;
  64. return Stack(
  65. children: [
  66. ListView(
  67. padding: const EdgeInsets.only(top: 45),
  68. children: const [
  69. ListTile(
  70. title: Text("这是一个标题 "),
  71. ),
  72. ListTile(
  73. title: Text("这是一个标题"),
  74. ),
  75. ListTile(
  76. title: Text("这是一个标题"),
  77. ),
  78. ListTile(
  79. title: Text("这是一个标题"),
  80. ),
  81. ListTile(
  82. title: Text("这是一个标题"),
  83. ),
  84. ListTile(
  85. title: Text("这是一个标题"),
  86. ),
  87. ListTile(
  88. title: Text("这是一个标题"),
  89. ),
  90. ListTile(
  91. title: Text("这是一个标题"),
  92. ),
  93. ListTile(
  94. title: Text("这是一个标题"),
  95. ),
  96. ListTile(
  97. title: Text("这是一个标题"),
  98. ),
  99. ListTile(
  100. title: Text("这是一个标题"),
  101. ),
  102. ListTile(
  103. title: Text("这是一个标题"),
  104. ),
  105. ListTile(
  106. title: Text("这是一个标题"),
  107. ),
  108. ListTile(
  109. title: Text("这是一个标题"),
  110. ),
  111. ListTile(
  112. title: Text("这是一个标题"),
  113. ),
  114. ListTile(
  115. title: Text("这是一个标题"),
  116. ),
  117. ListTile(
  118. title: Text("这是一个标题"),
  119. ),
  120. ListTile(
  121. title: Text("这是一个标题"),
  122. ),
  123. ListTile(
  124. title: Text("这是一个标题"),
  125. ),
  126. ListTile(
  127. title: Text("这是一个标题"),
  128. ),
  129. ListTile(
  130. title: Text("这是一个标题"),
  131. ),
  132. ListTile(
  133. title: Text("这是一个标题"),
  134. ),
  135. ListTile(
  136. title: Text("这是一个标题"),
  137. ),
  138. ],
  139. ),
  140. Positioned(
  141. top: 0,
  142. left: 0,
  143. height: 40,
  144. width: size.width,
  145. child: Container(
  146. alignment: Alignment.center,
  147. color: Colors.black,
  148. child: const Text(
  149. "你好FLutter",
  150. style: TextStyle(color: Colors.white),
  151. ),
  152. ))
  153. ],
  154. );
  155. }
  156. }

6 Flutter AspectRatio

6.1 说明

6.1.1 AspectRatio的作用是根据设置调整子元素child的宽高比。

6.1.2 AspectRatio首先会在布局限制条件允许的范围内尽可能的扩展,  widget的高度是由宽度和比率决定 的,类似于BoxFit中的contain,按照固定比率去尽量占满区域。

6.1.3 如果在满足所有限制条件过后无法找到一个可行的尺寸,  AspectRatio最终将会去优先适应布局限制条 件,而忽略所设置的比率。

6.2 属性

6.2.1 aspectRatio

        宽高比,最终可能不会根据这个值去布局,具体则要看综合因素,外层是否允许 按照这种比率进行布局,这只是一个参考值

6.2.2 child

        子组件

6.3 示例

  1. import 'package:flutter/material.dart';
  2. void main() {
  3. runApp(const MyApp());
  4. } //Flutter AspectRatio
  5. class MyApp extends StatelessWidget {
  6. const MyApp({Key? key})
  7. : super(key: key); // This widget is the root of your application.
  8. @override
  9. Widget build(BuildContext context) {
  10. return MaterialApp(
  11. title: 'Flutter Demo',
  12. theme: ThemeData(
  13. primarySwatch: Colors.blue,
  14. ),
  15. home: Scaffold(
  16. appBar: AppBar(title: const Text("Flutter App")),
  17. body: const HomePage(),
  18. ),
  19. );
  20. }
  21. }
  22. class HomePage extends StatelessWidget {
  23. const HomePage({Key? key}) : super(key: key);
  24. @override
  25. Widget build(BuildContext context) {
  26. // TODO: implement build
  27. return Container(
  28. width: 200,
  29. color: Colors.yellow,
  30. child: AspectRatio(
  31. aspectRatio: 2.0 / 1.0,
  32. child: Container(
  33. color: Colors.red,
  34. ),
  35. child: LayoutDemo(),
  36. ),
  37. );
  38. }
  39. }
  40. class LayoutDemo extends StatelessWidget {
  41. @override
  42. Widget build(BuildContext context) {
  43. // TODO: implement build
  44. return AspectRatio(
  45. aspectRatio: 3.0 / 1.0,
  46. child: Container(
  47. color: Colors.blue,
  48. ),
  49. );
  50. }
  51. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/242633
推荐阅读
相关标签
  

闽ICP备14008679号