Container(容器控件)在 Flutter 是经常使用的控件,它就相当于我们 HTML 里的<div>标签,每个页面或者说每个视图都离不开它。
Alignment 属性
其实容器的作用就是方便我们进行布局的,Flutter 这点也作的很好,我们先来看容器属性中的Alignment。
这个属性针对的是Container内child的对齐方式,也就是容器子内容的对齐方式,并不是容器本身的对齐方式。
先作一个效果:建立一个容器,然后容器内加入一段文字Hello Mybj, 并让它居中对齐。
import 'package:flutter/material.dart';
void main () => runApp(MyApp());
class MyApp extends StatelessWidget{
@override
Widget build(BuildContext context ){
return MaterialApp(
title:'Text widget',
home:Scaffold(
body:Center(
child:Container(
child:new Text('Hello Mybj',style: TextStyle(fontSize: 40.0),),
alignment: Alignment.center,
),
),
),
);
}
}
效果如下:

关键代码其实就是:
border:Border.all(width:2.0,color:Colors.red),
本文就到这里,希望小伙伴们都能动手练习起来。在这里附上全部代码,方便小伙伴们学习。
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Text widget',
home: Scaffold(
body: Center(
child: Container(
child: Container(
child: new Text(
'Hello Mybj',
style: TextStyle(fontSize: 40.0),
),
alignment: Alignment.topLeft,
width: 500.0,
height: 400.0,
// color: Colors.lightBlue,
padding: const EdgeInsets.fromLTRB(20.0, 30.0, 0.0, 0.0),
margin: const EdgeInsets.fromLTRB(30.0, 20.0, 15.0, 10.0),
decoration: new BoxDecoration(
gradient: const LinearGradient(colors: [
Colors.lightBlue,
Colors.greenAccent,
Colors.purple
]),
border: Border.all(width: 2.0, color: Colors.red)),
),
alignment: Alignment.center,
),
),
),
);
}
}