flutter页面布局二

news/2024/7/3 23:05:43

Stack

在flutter中,Stack表示堆的意思,可以用来实现页面的定位布局。

Stack组件接收两个可选参数:

  • alignment:配置所有子元素的显示位置
  • children:子组件

  

在上面的Stack组件里面定义了一个Container和两个Text组件,我们会发现这三个组件实现了重叠,并且后定义的在上层,这就是所谓的堆效果,

import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return MaterialApp(
        home: Scaffold(
      appBar: AppBar(title: Text('FlutterDemo')),
      body: LayoutDemo(),
    ));
  }
}

class LayoutDemo extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Center(
      child: Stack(
        alignment: Alignment.center,
        children: <Widget>[              
          Container(
            height: 400,
            width: 300,
            color: Colors.red,
          ),
          Text('我是一个文本',style: TextStyle(
            fontSize: 40,
            color: Colors.white
          ))           
        ],
      ),
    );
  }
}

在上面的例子中,我们给alignment的值是Alignment.center,除此之外,还有Alignment.topLeft等,另外,它也可以接收两个double类型的值,介于-1到1之间:(0,0)表示中心点,(1,1)表示右下角,(-1,-1)表示左上角。

Stack Align 

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

和上面的例子一样,当我们在Stack组件里面定义了多个子元素的时候,会发生重叠,即使使用了alignment进行了定位,也是对所有的元素统一定位,这个时候就可以借助Align来控制每个子元素的位置。

 Align也有两个可选参数属性:

  • alignment :配置所有子元素的显示位置 
  • child :子组件

现在,我们可以这样修改上面的例子:

import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return MaterialApp(
        home: Scaffold(
      appBar: AppBar(title: Text('FlutterDemo')),
      body: LayoutDemo(),
    ));
  }
}

class LayoutDemo extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Center(
      child:  Container(
            height: 400,
            width: 300,
            color: Colors.red,
            child: Stack(
              // alignment: Alignment.center,
              children: <Widget>[
                Align(
                  alignment: Alignment(1,-0.2),
                  child: Icon(Icons.home,size: 40,color: Colors.white),
                ),
                Align(
                  alignment: Alignment.center,
                  child: Icon(Icons.search,size: 30,color: Colors.white),
                ),
                Align(
                  alignment: Alignment.bottomRight,
                  child: Icon(Icons.settings_applications,size: 30,color: Colors.white),
                )
              ],
            ),
      ),
    );
  }
}

 Stack Positioned 

Stack 组件中结合 Positioned 组件也可以控制每个子元素的显示位置 。Positioned 有多个可选参数属性:
  • top :子元素距离顶部的距离 
  • bottom
  • left 
  • right 
  • child 
 我们也可以使用Positioned来实现上面Align的效果:

 
class LayoutDemo extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Center(
      child:  Container(
            height: 400,
            width: 300,
            color: Colors.red,
            child: Stack(
              // alignment: Alignment.center,
              children: <Widget>[
                Positioned(
                //  left: 10,
                  child: Icon(Icons.home,size: 40,color: Colors.white),
                ),
                Positioned(
                 bottom: 0,
                 left: 100,
                  child: Icon(Icons.search,size: 30,color: Colors.white),
                ),
                Positioned(
                  right: 0,
                  child: Icon(Icons.settings_applications,size: 30,color: Colors.white),
                )
              ],
            ),
      ),
    );
  }
}

转载于:https://www.cnblogs.com/yuyujuan/p/10976429.html


http://www.niftyadmin.cn/n/4819768.html

相关文章

指针初始化是非常重要的,当我们在声明一个指针的时候请记得给它赋初值NULL,free释放完指针内容的时候也给他赋null初值。谢谢

没有初始化的指针是最危险的&#xff0c;因为它可能指向任何地址区域。所以如果没有显示的将其初始化为null&#xff0c;那他肯定不为空。这是释放它所指向的内存。肯定会出错。指针初始化是非常重要的&#xff0c;当我们在声明一个指针的时候请记得给它赋初值NULL&#xff0c;…

CxImage类使用入门

过几天我再写,实在是没有时间. 关于这个类,可以看CodeProject上的文章:http://www.codeproject.com/bitmap/CXImage.asp CxImage是一个可以用于MFC的C类&#xff0c;可以打开&#xff0c;保存&#xff0c;显示&#xff0c;转换各种格式的图像文件,比如BMP, JPEG, GIF, PNG, TI…

FAQ:如何从URL取得其在Internet Cache中的文件名

FAQ&#xff1a;如何从URL取得其在Internet Cache中的文件名 选择自 CathyEagle 的 Blog 关键字 FAQ&#xff1a;如何从URL取得其在Internet Cache中的文件名出处 问张硕&#xff0c;你好&#xff0c; 我目前对IE编程感兴趣&#xff0c;在网上看到了你的文章&#xff0…

基础链表实现

前言最近在复习数据结构的相关知识&#xff0c;感觉在初学的时候还是有很多东西没有掌握&#xff0c;不过现在终于算是搞得比较有头绪了&#xff0c;所以就在写出来和大家一起分享&#xff01; 什么是链表简单的说&#xff0c;链表就是由多个结点离散分配&#xff0c;彼此通过指…

Internet Explorer 编程简述(一)WebBrowser还是WebBrowser_V1收藏

Internet Explorer 编程简述&#xff08;一&#xff09;WebBrowser还是WebBrowser_V1收藏 新一篇: Internet Explorer 编程简述&#xff08;二&#xff09;在IE中编辑OLE嵌入文档 | 旧一篇: Internet Explorer 编程简述&#xff08;序&#xff09; <script>function Stor…

第五次个人博客 Android studio测试软件——Ui Automator测试学习及运用

在第十五周的个人博客上要求我们根据最终的团队大作业所用的软件&#xff08;Android studio&#xff09;进行软件测试的介绍&#xff0c;我选择的是基于Android自动化测试的Ui Automator安卓测试软件&#xff0c;下面我将对其进行简单的介绍。 1、UiAutomator简介 Uiautomator…

Java-MyBatis-MyBatis3-XML映射文件:XML映射文件

ylbtech-Java-MyBatis-MyBatis3-XML映射文件&#xff1a;XML映射文件1、 XML 映射文件 MyBatis 的真正强大在于它的映射语句&#xff0c;这是它的魔力所在。由于它的异常强大&#xff0c;映射器的 XML 文件就显得相对简单。如果拿它跟具有相同功能的 JDBC 代码进行对比&#xf…

Windows95/NT下OpenGL编程原理

Windows95/NT下OpenGL编程原理西安交通大学9693信箱蔡茂----科学计算可视化&#xff0c;计算机动画和虚拟现实是现在计算机图形学的三个热点。而这三个热点的核心都是三维真实感图形的绘制。由于OpenGL&#xff08;OpenGraphicsLibrary&#xff09;具有跨平台性、简便、高效、功…