【自学Flutter】14.1 ConstrainedBox 和 SizeBox 的使用

14.1 ConstrainedBox 和 SizeBox 的使用

1.源代码

import 'package:flutter/material.dart';

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

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {

  Widget blueBox = DecoratedBox(decoration: BoxDecoration(color: Colors.blue));

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        home: Scaffold(
            backgroundColor: Colors.black,
            body: Center(
              child: Column(
                children: <Widget>[
                  ConstrainedBox(
                    constraints: BoxConstraints.tight(Size(80.0, 80.0)),
                    child: Padding(padding: EdgeInsets.only(top: 10.0),child: blueBox,),
                  ),
                  ConstrainedBox(
                    constraints: BoxConstraints.tightFor(height: 80.0,width: 80.0),
                    child: Padding(padding: EdgeInsets.only(top: 10.0),child: blueBox,),
                  ),
                  ConstrainedBox(
                    constraints: BoxConstraints(minWidth: 80.0,minHeight: 80.0,maxWidth: 80.0,maxHeight: 80.0),
                    child: Padding(padding: EdgeInsets.only(top: 10.0),child: blueBox,),
                  ),
                  SizedBox(
                    width: 80.0,
                    height: 80.0,
                    child: Padding(padding: EdgeInsets.only(top: 10.0),child: blueBox,),
                  )
                ],
              )
            )

        )
    );
  }
}

2.解释源代码

import 'package:flutter/material.dart';

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

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {

  Widget blueBox = DecoratedBox(decoration: BoxDecoration(color: Colors.blue));

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        home: Scaffold(
            backgroundColor: Colors.black,
            body: Center(
              child: Column(
                children: <Widget>[
                  //描绘下方效果图中的正方形的第1种方法
                  ConstrainedBox(
                    constraints: BoxConstraints.tight(Size(80.0, 80.0)),
                    child: Padding(padding: EdgeInsets.only(top: 10.0),child: blueBox,),
                  ),
                  //描绘下方效果图中的正方形的第2种方法
                  ConstrainedBox(
                    constraints: BoxConstraints.tightFor(height: 80.0,width: 80.0),
                    child: Padding(padding: EdgeInsets.only(top: 10.0),child: blueBox,),
                  ),
                  //描绘下方效果图中的正方形的第3种方法
                  ConstrainedBox(
                    constraints: BoxConstraints(minWidth: 80.0,minHeight: 80.0,maxWidth: 80.0,maxHeight: 80.0),
                    child: Padding(padding: EdgeInsets.only(top: 10.0),child: blueBox,),
                  ),
                  //描绘下方效果图中的正方形的第4种方法
                  SizedBox(
                    width: 80.0,
                    height: 80.0,
                    child: Padding(padding: EdgeInsets.only(top: 10.0),child: blueBox,),
                  )
                ],
              )
            )

        )
    );
  }
}

3.效果图

【自学Flutter】14.1 ConstrainedBox 和 SizeBox 的使用_第1张图片

你可能感兴趣的:(自学Flutter)