当前位置:   article > 正文

Flutter 布局常用的 widgets(Common layout widgets)_flutter galleryview

flutter galleryview

简单列举总结一下常用的布局widget。
Flutter有丰富的layout组件库。其中有一些是常用库。
下面的widget分为两类:标准组件和来自Material Components的特殊组件。
只有Material App能够使用Material Components的组件。

标准组件 - Standard widgets

  • Container
    • 给一个组件添加 padding, margins, 边界(borders), 背景颜色或其它装饰(decorations)。
  • GridView
    • 将多个widget放在一个可滑动的表格中。
  • ListView
    • 将多个widget放在一个可滑动的列表中。
  • Stack
    • 在一个widget上面盖上另一个widget。

Material Components

  • Card
    • 将一些相近的信息装进一个有圆角和阴影的盒子里。
  • ListTile
    • 一个Row中装载最多3行文字;可选则在前面或尾部添加图标。

Container

Container用法比较自由。可以把整个layout放进container里面,然后改变背景颜色或图片。

Container 小结:

  • 添加 padding, margins, 和边界(borders)
  • 能够更好背景颜色和图片
  • 包含一个单独的子widget,这个子widget可以是Row、Column或一个widget树的根widget

container结构

测试代码widgetdemo/container_page.dart

  1. import 'package:flutter/material.dart';
  2. import 'package:demo_flutter/widgetdemo/container_page.dart';
  3. // 引入自定义的包......
  4. void main() => runApp(new MyApp());
  5. class MyApp extends StatelessWidget {
  6. @override
  7. Widget build(BuildContext context) {
  8. return new MaterialApp(
  9. title: 'Container demo 1',
  10. theme: new ThemeData(primarySwatch: Colors.brown),
  11. home: new ContainerDemoPage(), // 这里换上想要测试的界面
  12. );
  13. }
  14. }

widgetdemo/container_page.dart代码

  1. import 'package:flutter/material.dart';
  2. /// container示例界面
  3. class ContainerDemoPage extends StatefulWidget {
  4. @override
  5. State<StatefulWidget> createState() => new _ContainerDemoPageState();
  6. }
  7. class _ContainerDemoPageState extends State<ContainerDemoPage> {
  8. @override
  9. Widget build(BuildContext context) {
  10. Expanded imageExpanded(String img) {
  11. return new Expanded(child: new Container(
  12. decoration: new BoxDecoration(
  13. border: new Border.all(width: 10.0, color: Colors.black38),
  14. borderRadius: const BorderRadius.all(
  15. const Radius.circular(8.0))),
  16. margin: const EdgeInsets.all(4.0),
  17. child: new Image.asset(img),
  18. ));
  19. }
  20. var container = new Container(
  21. decoration: new BoxDecoration(color: Colors.black26),
  22. child: new Column(
  23. children: <Widget>[
  24. new Row(children: <Widget>[
  25. imageExpanded('images/c1.jpg'),
  26. imageExpanded('images/c2.jpg'),
  27. ],),
  28. new Row(children: <Widget>[
  29. imageExpanded('images/d1.jpg'),
  30. imageExpanded('images/d2.jpg'),
  31. ],),
  32. new Row(children: <Widget>[
  33. imageExpanded('images/p1.jpg'),
  34. ],)
  35. ],
  36. ),
  37. );
  38. return new Scaffold(
  39. appBar: new AppBar(title: new Text('Container Page demo'),),
  40. body: new Center(
  41. child: container,
  42. ),
  43. );
  44. }
  45. }

container示例

GridView

用GridView来将widget放入一个2维的列表中。
GridView提供了2个预装配好的列表,也可以自己建立自定义列表。
GridView支持滚动。

GridView 小结:

  • 将多个widget放进一个表格中
  • 当超出渲染范围时,自动提供滚动功能
  • 可自定义格子,也可用下面提供的2种
    • GridView.count 指定列的数目
    • GridView.extent 允许指定子项的最大像素宽度

示例1 - 用GridView.extent

GridView.extent指定子项占据的最大宽度

  1. import 'package:flutter/material.dart';
  2. /// gridView示例界面1
  3. class GridDemo1Page extends StatefulWidget {
  4. @override
  5. State<StatefulWidget> createState() => new _GridDemo1PageState();
  6. }
  7. class _GridDemo1PageState extends State<GridDemo1Page> {
  8. @override
  9. Widget build(BuildContext context) {
  10. return new Scaffold(
  11. appBar: new AppBar(title: new Text('Grid Page 1 demo'),),
  12. body: new Center(
  13. child: buildGrid(),
  14. ),
  15. );
  16. }
  17. List<Container> _buildGridTileList(int count) {
  18. return new List<Container>.generate(count, (int index) =>
  19. new Container(child: new Image.asset('images/pic${index + 1}.jpg'),));
  20. }
  21. Widget buildGrid() {
  22. return new GridView.extent(
  23. maxCrossAxisExtent: 150.0,
  24. padding: const EdgeInsets.all(4.0),
  25. mainAxisSpacing: 4.0,
  26. crossAxisSpacing: 4.0,
  27. children: _buildGridTileList(30),);
  28. }
  29. }

用GridView.extent

示例2 - 用GridView.count

crossAxisCount设为2,分成2列。

  1. Widget buildGrid() {
  2. var countGrid = GridView.count(
  3. crossAxisCount: 2,
  4. mainAxisSpacing: 4.0,
  5. crossAxisSpacing: 4.0,
  6. padding: const EdgeInsets.all(4.0),
  7. childAspectRatio: 1.3,
  8. children: _buildGridTileList(30),
  9. );
  10. return countGrid;
  11. }

GridView.count示例

ListView

ListView能以列的形式展示数据。当内容超过渲染范围时,自动提供滚动的功能。

ListView 小结

  • 把子视图装进列表中
  • 水平或竖直都可以
  • 支持滑动
  • 相比于Column,可选配置比较少,但更易用并且支持滑动

和Android中的ListView差别不大

示例1

ListTile当做子项来装载数据。

  1. import 'package:flutter/material.dart';
  2. class ListViewPage extends StatefulWidget {
  3. @override
  4. State<StatefulWidget> createState() => new _ListViewPageState();
  5. }
  6. class _ListViewPageState extends State<ListViewPage> {
  7. @override
  8. Widget build(BuildContext context) {
  9. List<Widget> list = <Widget>[];
  10. for (int i = 0; i < 30; i++) {
  11. list.add(new ListTile(
  12. title: new Text('title$i', style: _itemTextStyle,),
  13. subtitle: new Text('A'),
  14. leading: i % 3 == 0
  15. ? new Icon(Icons.theaters, color: Colors.blue,)
  16. : new Icon(Icons.restaurant, color: Colors.blue,),
  17. ));
  18. }
  19. return new Scaffold(
  20. appBar: new AppBar(title: new Text('ListView Demo'),),
  21. body: new Center(child: new ListView(children: list,),),
  22. );
  23. }
  24. }
  25. TextStyle _itemTextStyle = new TextStyle(
  26. fontWeight: FontWeight.w500, fontSize: 14.0);

ListView参考效果图1

另外可以参考 https://github.com/flutter/flutter/blob/master/examples/flutter_gallery/lib/demo/colors_demo.dart

Stack

使用Stack在widget之上显示另一些widget,通常用来显示图片。
显示的widget可以完全地把底部widget盖住。

Stack 小结:

  • 用来在当前widget上面再盖上一层widget
  • Stack children中的第一个widget放在最下,后面的widget会一层层盖上去
  • Stack的内容不支持滚动
  • 可以裁剪超出范围的子widget

Stack示例1

显示一个CircleAvatar

  1. import 'package:flutter/material.dart';
  2. class StackPage1 extends StatefulWidget {
  3. @override
  4. State<StatefulWidget> createState() => new _StackPage1State();
  5. }
  6. class _StackPage1State extends State<StackPage1> {
  7. @override
  8. Widget build(BuildContext context) {
  9. var stack = new Stack(
  10. alignment: const Alignment(0.6, 0.6),
  11. children: <Widget>[
  12. new CircleAvatar(
  13. backgroundImage: new AssetImage('images/android_1.jpg'),
  14. radius: 100.0,),
  15. new Container(decoration: new BoxDecoration(color: Colors.black45),
  16. child: new Text(
  17. 'Android Avatar', style: new TextStyle(color: Colors.white70),),),
  18. new Container(decoration: new BoxDecoration(color: Colors.transparent),
  19. padding: const EdgeInsets.fromLTRB(0.0, 0.0, 100.0, 0.0),
  20. child: new CircleAvatar(
  21. backgroundImage: new AssetImage('images/p_box1.png'),
  22. backgroundColor: Colors.transparent,
  23. radius: 10.0,),),
  24. ],
  25. );
  26. return new Scaffold(
  27. appBar: new AppBar(title: new Text('Stack Demo 1'),),
  28. body: new Center(child: stack,),
  29. );
  30. }
  31. }

Stack示例1

Card

Card来自Material组件库,可包含一些数据,通常用ListTile来组装。Card只有一个子widget,可以是column、row、list、grid或其它组合widget。
默认情况下,Card把自己的尺寸缩小为0像素。可以用SizedBox来指定card的尺寸。

Flutter中的Card有圆角和阴影效果。修改elevation可改变阴影效果。

elevation取值范围,参考 Elevation and Shadows

若设置的范围外的值,阴影效果会消失。

Card 小结:

  • 实现了Material Design card
  • 用于展示相关的数据
  • 有一个子项(child),可以是column、row、list、grid或其它组合widget
  • 有圆角和阴影效果
  • 不支持滚动

Card示例1

将前面的ListView示例修改一下

  1. import 'package:flutter/material.dart';
  2. class ListViewPage extends StatefulWidget {
  3. @override
  4. State<StatefulWidget> createState() => new _ListViewPageState();
  5. }
  6. class _ListViewPageState extends State<ListViewPage> {
  7. @override
  8. Widget build(BuildContext context) {
  9. List<Widget> list = <Widget>[];
  10. for (int i = 0; i < 30; i++) {
  11. list.add(new Card(child: new Column(
  12. children: <Widget>[
  13. new Image.asset(
  14. 'images/pic${i + 1}.jpg',),
  15. new ListTile(
  16. title: new Text('title$i', style: _itemTextStyle,),
  17. subtitle: new Text('A'),
  18. leading: i % 3 == 0
  19. ? new Icon(Icons.theaters, color: Colors.blue,)
  20. : new Icon(Icons.restaurant, color: Colors.blue,),
  21. ),
  22. ],
  23. ),));
  24. }
  25. return new Scaffold(
  26. appBar: new AppBar(title: new Text('ListView Demo'),),
  27. body: new Center(child: new ListView(children: list,),),
  28. );
  29. }
  30. }
  31. TextStyle _itemTextStyle = new TextStyle(
  32. fontWeight: FontWeight.w500, fontSize: 14.0);

Card示例1

ListTile

来自Material组件库的横向组件。可自定义3行文字及其可选的头尾图标。
此控件常与Card或ListView一起用。

ListTile 小结:

  • 可定制3行带图标的文字
  • 相比于Row,配置更少,但更易用

加一个主界面

放置一些按钮,点击跳转到相应的界面。
使用Navigator.of(context).pushNamed(routeName)来跳转。

  1. import 'package:flutter/material.dart';
  2. import 'package:demo_flutter/widgetdemo/container_page.dart';
  3. import 'package:demo_flutter/widgetdemo/grid_page.dart';
  4. import 'package:demo_flutter/widgetdemo/listview_demo.dart';
  5. import 'package:demo_flutter/widgetdemo/stack_page1.dart';
  6. import 'package:demo_flutter/widgetdemo/button_page.dart';
  7. const String CONTAINER_DEMO_PAGE = '/a';
  8. void main() {
  9. runApp(new MaterialApp(
  10. home: new HomePage(),
  11. routes: {
  12. CONTAINER_DEMO_PAGE: (BuildContext context) => new ContainerDemoPage(),
  13. '/b': (BuildContext context) => new GridDemo1Page(),
  14. '/c': (BuildContext context) => new ListViewPage(),
  15. '/d': (BuildContext context) => new StackPage1(),
  16. '/e': (BuildContext context) => new ButtonPage(),
  17. },
  18. ));
  19. }
  20. class HomePage extends StatefulWidget {
  21. @override
  22. State<StatefulWidget> createState() => new _HomePageState();
  23. }
  24. class _HomePageState extends State<HomePage> {
  25. @override
  26. Widget build(BuildContext context) {
  27. getGestureDetector(String routeName, String content) {
  28. return new GestureDetector (
  29. onTap: () {
  30. Navigator.of(context).pushNamed(routeName);
  31. },
  32. child: new Container (
  33. padding: EdgeInsets.all(20.0),
  34. child: new Center(child: new Text (content),)),
  35. );
  36. }
  37. return new Scaffold(
  38. appBar: new AppBar(title: new Text('Home'),),
  39. body: new Column(children: <Widget>[
  40. getGestureDetector(CONTAINER_DEMO_PAGE, 'Container Demo'),
  41. getGestureDetector('/b', 'Grid Demo 1'),
  42. getGestureDetector('/c', 'ListView Demo'),
  43. getGestureDetector('/d', 'Stack Demo'),
  44. getGestureDetector('/e', 'Button Page'),
  45. ],),
  46. );
  47. }
  48. }

 



作者:RustFisher
链接:https://www.jianshu.com/p/fccb4c43c268
來源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。

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

闽ICP备14008679号