【Flutter实战】定位装饰权重组件及柱状图案例

移动开发 作者: 2024-08-24 21:45:01
老孟导读:Flutter中有这么一类组件,用于定位、装饰、控制子组件,比如 Container (定位、装饰)、Expanded (扩展)、SizedBox (固定尺寸)、AspectRatio (宽

Container

Container(
	child: Text('老孟'),)
Container(
	color: Colors.blue,child: Text('老孟'),)
Container(
      color: Colors.blue,child: Container(
        margin: EdgeInsets.all(10),padding: EdgeInsets.all(20),color: Colors.red,),)
Container(
  child: Text('老孟,专注分享Flutter技术及应用'),decoration: BoxDecoration(shape: BoxShape.circle,color: Colors.blue),)
Container(
        child: Text('老孟,专注分享Flutter技术及应用'),padding: EdgeInsets.symmetric(horizontal: 10),decoration: BoxDecoration(
            shape: BoxShape.rectangle,borderRadius: BorderRadius.all(Radius.circular(20)),)
Container(
        child: Text('老孟,专注分享Flutter技术及应用'),decoration: BoxDecoration(
          borderRadius: BorderRadius.circular(12),border: Border.all(
            color: Colors.blue,width: 2,)
Container(
      height: 200,width: 200,decoration: BoxDecoration(
        image:  DecorationImage(
          image: NetworkImage(
              'https://flutter.github.io/assets-for-api-docs/assets/widgets/owl-2.jpg'),fit: BoxFit.cover,border: Border.all(
          color: Colors.blue,borderRadius: BorderRadius.circular(12),)

修改其形状为圆形,代码如下:
Container(
      height: 200,decoration: BoxDecoration(
        image: DecorationImage(
          image: NetworkImage(
              'https://flutter.github.io/assets-for-api-docs/assets/widgets/owl-2.jpg'),shape: BoxShape.circle,)
Container(
        color: Colors.blue,child: Text('老孟,一个有态度的程序员'),alignment: Alignment.center,)
Container(
  alignment: Alignment(-.5,-.5),child: Text('老孟,专注分享Flutter技术及应用'),)
Container(
        color: Colors.blue,height: 60,width: 250,)
Container(
        color: Colors.blue,constraints: BoxConstraints(
          maxHeight: 100,maxWidth: 300,minHeight: 100,minWidth: 100,)
Container(
        color: Colors.blue,transform: Matrix4.rotationZ(0.5),)

SizedBox

SizedBox(
        height: 60,child: Container(
          color: Colors.blue,)
SizedBox(
  height: double.infinity,width: double.infinity,...
)
SizedBox.expand(
  child: Text('老孟,专注分享Flutter技术及应用'),)
Column(
          children: <Widget>[
            Container(height: 30,color: Colors.blue,SizedBox(height: 30,Container(height: 30,],)

AspectRatio

Container(
        height: 300,width: 300,child: AspectRatio(
          aspectRatio: 2 / 1,child: Container(color: Colors.red,)

FractionallySizedBox

Container(
  height: 200,child: FractionallySizedBox(
    widthFactor: .8,heightFactor: .3,child: Container(
      color: Colors.red,)
FractionallySizedBox(
  alignment: Alignment.center,...
)

权重组件

Row(
      children: <Widget>[
        Container(
          color: Colors.blue,height: 50,width: 100,Flexible(
            child: Container(
              color: Colors.red,)
        ),Container(
          color: Colors.blue,)
Column(
      children: <Widget>[
        Flexible(
          flex: 1,child: Container(
            color: Colors.blue,child: Text('1 Flex/ 6 Total',style: TextStyle(color: Colors.white),Flexible(
          flex: 2,child: Container(
            color: Colors.red,child: Text('2 Flex/ 6 Total',Flexible(
          flex: 3,child: Container(
            color: Colors.green,child: Text('3 Flex/ 6 Total',)
  • tight:必须(强制)填满剩余空间。
  • loose:尽可能大的填满剩余空间,但是可以不填满。
Row(
      children: <Widget>[
        Container(
          color: Colors.blue,child: Text('Container',)
Row(
      children: <Widget>[
        Container(
          color: Colors.blue,)

此时又填满剩余空间。
Row(
      children: <Widget>[
        Container(
          color: Colors.blue,Flexible(
          child: OutlineButton(
            child: Text('OutlineButton'),)
class Expanded extends Flexible {
  /// Creates a widget that expands a child of a [Row],[Column],or [Flex]
  /// so that the child fills the available space along the flex widget's
  /// main axis.
  const Expanded({
    Key key,int flex = 1,@required Widget child,}) : super(key: key,flex: flex,fit: FlexFit.tight,child: child);
}
Row(
      children: <Widget>[
        Container(
          color: Colors.blue,Expanded(
          child: OutlineButton(
            child: Text('OutlineButton'),)
@override
Widget build(BuildContext context) {
  return Expanded(
    flex: flex,child: const SizedBox.shrink(),);
}
Row(
  children: <Widget>[
    Container(width: 100,color: Colors.green,Spacer(flex: 2,Container(width: 100,Spacer(),)
  • Spacer 是通过 Expanded 实现的,Expanded继承自Flexible。
  • 填满剩余空间直接使用Expanded更方便。
  • Spacer 用于撑开 Row、Column、Flex 的子组件的空隙。

仿 掘金-我 效果

原创声明
本站部分文章基于互联网的整理,我们会把真正“有用/优质”的文章整理提供给各位开发者。本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
本文链接:http://www.jiecseo.com/news/show_68039.html