Flutter 初探(三):容器类Widgets

栏目: ASP.NET · 发布时间: 5年前

内容简介:以下是容器类Widgets的部分汇总:以下是一些效果截图:容器类愈加抽象,但是给布局和各种酷炫实现打下了坚实的基础和广阔的想象力,是个诱惑而充满挑战的体验。

以下是容器类Widgets的部分汇总:

  • Padding
  • 布局限制类容器ConstrainedBox和SizedBox
  • 装饰类容器DecoratedBox
  • 变换Transform
  • Container容器
  • Scaffold、底部导航

各个容器简易实现

// Padding
class NewPadding extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: AppBar(title: Text('Padding学习')),
      body: new Padding(
          // 上下左右各添加16像素空白
          padding: EdgeInsets.all(16.0),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: <Widget>[
              Padding(
                // 左边添加8像素补白
                padding: const EdgeInsets.only(left: 8.0),
                child: Text('Hello world'),
              ),
              Padding(
                //上下各添加8像素补白
                padding: const EdgeInsets.symmetric(vertical: 8.0),
                child: Text("I am Jack"),
              ),
              Padding(
                // 分别指定四个方向的补白
                padding: const EdgeInsets.fromLTRB(20.0, .0, 20.0, 20.0),
                child: Text("Your friend"),
              )
            ],
          )),
    );
  }
}

// 布局限制类容器 ConstrianedBox、SizeBox
// 预先定义一个redBox,一个红色背景的盒子
Widget redBox = DecoratedBox(
  decoration: BoxDecoration(color: Colors.red),
);

class NewConstrainedBox extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new Scaffold(
        appBar: AppBar(title: Text("ConstrianedBox、SizeBox学习")),
        body: new Column(
          children: <Widget>[
            new Text('ConstrainedBox:'),
            new ConstrainedBox(
              // 最小高度50,宽度尽可能大的红色容器
              constraints:
                  BoxConstraints(minWidth: double.infinity, minHeight: 50.0),
              // 虽然container高度为5但是,容器的最小高度为50,所以最终生效的是50
              child: Container(height: 5.0, child: redBox),
            ),
            // SizedBox用于给子Widget指定固定的宽高
            new Text('SizedBox:'),
            SizedBox(
              width: 80.0,
              height: 80.0,
              child: redBox,
            ),
            new Text('多重限制案例:'),
            // 多重限制案例
            ConstrainedBox(
                // 父
                constraints: BoxConstraints(minWidth: 60.0, minHeight: 60.0),
                child: ConstrainedBox(
                  constraints: BoxConstraints(minWidth: 90.0, minHeight: 20.0),
                  child: redBox,
                )),
            new Text('调换限制条件:'),
            ConstrainedBox(
                // 父
                constraints: BoxConstraints(minWidth: 90.0, minHeight: 20.0),
                child: ConstrainedBox(
                  constraints: BoxConstraints(minWidth: 60.0, minHeight: 60.0),
                  child: redBox,
                )),
            new Text("'去除'多重限制"),
            ConstrainedBox(
              constraints: BoxConstraints(minWidth: 60, minHeight: 100),
              child: UnconstrainedBox(
                child: ConstrainedBox(
                  constraints: BoxConstraints(minWidth: 90, minHeight: 20),
                  child: redBox,
                ),
              ),
            ),
          ],
        ));
  }
}

// 装饰容器DecoratedBox 漂亮警告
class NewDecoratedBox extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: AppBar(title: Text('NewDecoratedBox学习')),
      body: new DecoratedBox(
          decoration: BoxDecoration(
            // 背景渐变
            gradient: LinearGradient(colors: [Colors.red, Colors.orange[700]]),
            // 像素圆角
            borderRadius: BorderRadius.circular(3.0),
            boxShadow: [
              BoxShadow(
                  color: Colors.black54,
                  offset: Offset(2.0, 2.0),
                  blurRadius: 4.0),
            ],
          ),
          child: new Padding(
            padding: EdgeInsets.symmetric(horizontal: 80.0, vertical: 18.0),
            child: Text(
              "Login",
              style: TextStyle(color: Colors.white),
            ),
          )),
    );
  }
}

class NewTransformAndContainer extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new Scaffold(
        appBar: AppBar(title: Text("Transform学习")),
        body: new Column(
          children: <Widget>[
            Container(
              color: Colors.black,
              child: new Transform(
                alignment: Alignment.topRight,
                transform: new Matrix4.skewY(0.3),
                child: new Container(
                  padding: const EdgeInsets.all(8.0),
                  color: Colors.deepOrange,
                  child: const Text("Apartment for rent!"),
                ),
              ),
            ),
            DecoratedBox(
                decoration: BoxDecoration(color: Colors.red),
                child: Transform.translate(
                  offset: Offset(-20.0, -5.0),
                  child: Text("Hello world"),
                )),
            DecoratedBox(
              decoration: BoxDecoration(color: Colors.red),
              child: Transform.rotate(
                //旋转90度
                angle: math.pi / 2,
                child: Text("Hello world"),
              ),
            ),
            DecoratedBox(
                decoration: BoxDecoration(color: Colors.red),
                child: Transform.scale(
                    scale: 1.5, //放大到1.5倍
                    child: Text("Hello world"))),
            Container(
              margin: EdgeInsets.all(20.0), //容器外补白
              color: Colors.orange,
              child: Text("Hello world!"),
            ),
            Container(
              padding: EdgeInsets.all(20.0), //容器内补白
              color: Colors.orange,
              child: Text("Hello world!"),
            ),
            Padding(
              padding: EdgeInsets.all(20.0),
              child: DecoratedBox(
                decoration: BoxDecoration(color: Colors.orange),
                child: Text("Hello world!"),
              ),
            ),
            DecoratedBox(
              decoration: BoxDecoration(color: Colors.orange),
              child: Padding(
                padding: const EdgeInsets.all(20.0),
                child: Text("Hello world!"),
              ),
            ),
            Container(
              margin: EdgeInsets.only(top: 50.0, left: 120.0), //容器外补白
              constraints:
                  BoxConstraints.tightFor(width: 200.0, height: 150.0), //卡片大小
              decoration: BoxDecoration(
                  //背景装饰
                  gradient: RadialGradient(
                      //背景径向渐变
                      colors: [Colors.red, Colors.orange],
                      center: Alignment.topLeft,
                      radius: .98),
                  boxShadow: [
                    //卡片阴影
                    BoxShadow(
                        color: Colors.black54,
                        offset: Offset(2.0, 2.0),
                        blurRadius: 4.0)
                  ]),
              transform: Matrix4.rotationZ(.2), //卡片倾斜变换
              alignment: Alignment.center, //卡片内文字居中
              child: Text(
                //卡片文字
                "5.20", style: TextStyle(color: Colors.white, fontSize: 40.0),
              ),
            ),
          ],
        ));
  }
}

// Scaffold、TabBar、底部导航
class ScaffoldRoute extends StatefulWidget {
  @override
  _ScaffoldRouteState createState() => new _ScaffoldRouteState();
}

class _ScaffoldRouteState extends State<ScaffoldRoute> {
  

  
  int _selectedIndex = 1;
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        //导航栏
        title: Text("App Name"),
        // 手动设置leading自定义菜单图标
        // leading: Builder(builder: (context){
        //   return IconButton(icon: Icon(Icons.dashboard), color: Colors.white,
        //     onPressed: (){
        //       Scaffold.of(context).openDrawer();
        //     },
        //   );
        // },),
        actions: <Widget>[
          //导航栏右侧菜单
          IconButton(icon: Icon(Icons.share), onPressed: () {}),
        ],
      ),
      // drawer: new MyDrawer(), //抽屉
      bottomNavigationBar: BottomNavigationBar(
        items: <BottomNavigationBarItem>[
          BottomNavigationBarItem(icon: Icon(Icons.home), title: Text('home')),
          BottomNavigationBarItem(icon: Icon(Icons.business), title: Text('business')),
          BottomNavigationBarItem(icon: Icon(Icons.school), title: Text("school")),

        ],
        currentIndex: _selectedIndex,
        fixedColor: Colors.blue,
        onTap: _onItemTapped,
      ),
      floatingActionButton: FloatingActionButton(
        child: Icon(Icons.ac_unit),
        onPressed: _onAdd,
      ),
    );
  }

  void _onItemTapped(int index) {
    setState(() {
      _selectedIndex = index;
    });
  }

  void _onAdd() {}
}


复制代码

以下是一些效果截图:

Flutter 初探(三):容器类Widgets
Flutter 初探(三):容器类Widgets
Flutter 初探(三):容器类Widgets
Flutter 初探(三):容器类Widgets
Flutter 初探(三):容器类Widgets

Summary

容器类愈加抽象,但是给布局和各种酷炫实现打下了坚实的基础和广阔的想象力,是个诱惑而充满挑战的体验。


以上所述就是小编给大家介绍的《Flutter 初探(三):容器类Widgets》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

深入浅出 HTTPS:从原理到实战

深入浅出 HTTPS:从原理到实战

虞卫东 / 电子工业出版社 / 2018-6 / 89

本书是一本专业的HTTPS书籍,全面讲解了HTTPS领域的相关知识,内容包括密码学、OpenSSL命令行、证书、TLS协议、HTTPS网站性能优化、HTTPS网站优秀实践、大型网站HTTPS架构设计等。本书有几个特点:(1)内容全面而新颖,基于RFC文档、国外书籍、社区等一手资料,总结了大部分最新的HTTPS知识;(2)由浅入深,从基础到进阶全面掌握HTTPS,读者能够轻松构建一个HTTPS网站,......一起来看看 《深入浅出 HTTPS:从原理到实战》 这本书的介绍吧!

HTML 压缩/解压工具
HTML 压缩/解压工具

在线压缩/解压 HTML 代码

JS 压缩/解压工具
JS 压缩/解压工具

在线压缩/解压 JS 代码

HSV CMYK 转换工具
HSV CMYK 转换工具

HSV CMYK互换工具