如何在Flutter中删除Column中的小部件之间的空间?

时间:2019-01-08 03:34:30

标签: layout dart flutter

我正在寻找答案,但找不到。

为什么“列”窗口小部件在子窗口小部件之间留出空间?
我想删除此小部件布局。下面是我的代码和空格位置。

enter image description here

代码!

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

void main() {
  SystemChrome.setEnabledSystemUIOverlays([]);
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Clock',
      theme: ThemeData(
        primaryColor: Colors.blue
      ),
      home: Social(),
      debugShowCheckedModeBanner: false,
    );
  }
}

class Social extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Colors.white,
        elevation: 0.0
      ),
      backgroundColor: Colors.white,
      body: Column(
        children: <Widget>[
          Flexible(
            flex: 1,
            child: Row(
              children: <Widget>[
                ClipOval(
                  clipper: CircleClipper(),
                  child: Image.asset('assets/irene.jpg')
                ),
                Expanded(
                  child: Padding(
                    padding: const EdgeInsets.only(left: 8.0),
                    child: Column(
                      mainAxisAlignment: MainAxisAlignment.start,
                      children: <Widget>[
                        Align(
                          alignment: Alignment.centerLeft,
                          child: Text(
                            'Irene',
                            style: TextStyle(
                              color: Colors.blue,
                              fontWeight: FontWeight.bold,
                              fontSize: 15.0
                            )
                          )
                        ),
                        Align(
                          alignment: Alignment.centerLeft,
                          child: Text('Yesterday, Ney York')
                        )
                      ],
                    ),
                  ),
                ),
                Align(
                  alignment: Alignment.topRight,
                  child: IconButton(
                    color: Theme.of(context).primaryColor,
                    icon: Icon(Icons.menu),
                    onPressed: (){},
                  ),
                )
              ],
            ),
          ),
          Container(
            width: MediaQuery.of(context).size.width/1.1,
            height: MediaQuery.of(context).size.height/2,
            child: Card(
              child: Image.asset('assets/irene.jpg'),
              color: Colors.white,
              shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(30.0)),
              elevation: 10.0,
            ),
          ),
          Padding(
            padding: const EdgeInsets.only(left: 30.0,right: 30.0,top: 15.0),
            child: Row(
              children: <Widget>[
                Image(
                  image: AssetImage('assets/like.png'), 
                  width: 30.0,
                  height: 30.0,
                ),
                SizedBox(width: 20.0,),
                Image.asset('assets/chat.png'),
                Expanded(child: Container()),
                Image.asset('assets/share.png'),
              ],
            ),
          ),
          Expanded(child: Container()),
        ],
      ),
    );
  }
}

class CircleClipper extends CustomClipper<Rect> {
  @override
  Rect getClip(Size size) {
    return Rect.fromCircle(center: Offset(size.width/2,size.height/4), radius: size.width/3);
  }

  @override
  bool shouldReclip(CustomClipper<Rect> oldClipper) {
    return true;
  }
}

如何修复它以及必须使用哪些小部件?
要制作Column子控件的大小,必须使用Flexible,Expanded等。
还有其他可以满足此条件的小部件吗?

2 个答案:

答案 0 :(得分:3)

那是因为您使用的是Flexible,它会扩展Widget的内部以填充可用空间。

更改此内容:

   body: Column(
          children: <Widget>[
            Flexible(
              flex: 1,

对此:

   body: Column(
          children: <Widget>[
            SizedBox(
              height: 60.0,

它应该可以工作

答案 1 :(得分:1)

我看到问题不在Column小部件中,而是在它的第一个孩子中,它需要的高度比您预期的长,我建议以后使用Widget inspector诊断布局问题。

这里是解决方法:

1-此处不需要小部件,只需将其删除。

Flexible(
        flex: 1, ...)

2-像这样将其包裹在Container中,以为化身图像设置适当的尺寸:

Container(
  width: 50,
  height: 50,
  child: ClipOval(clipper: CircleClipper(), child: Image.asset('assets/irene.jpg')),
)

最后,这是更新的完整代码:

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

    void main() {
      SystemChrome.setEnabledSystemUIOverlays([]);
      runApp(MyApp());
    }

    class MyApp extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          title: 'Flutter Clock',
          theme: ThemeData(primaryColor: Colors.blue),
          home: Social(),
          debugShowCheckedModeBanner: false,
        );
      }
    }

    class Social extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(backgroundColor: Colors.white, elevation: 0.0),
          backgroundColor: Colors.white,
          body: Column(
            children: <Widget>[
              Row(
                children: <Widget>[
                  Container(
                    width: 50,
                    height: 50,
                    child: ClipOval(clipper: CircleClipper(), child: Image.asset('assets/irene.jpg')),
                  ),
                  Expanded(
                    child: Padding(
                      padding: const EdgeInsets.only(left: 8.0),
                      child: Column(
                        mainAxisAlignment: MainAxisAlignment.start,
                        children: <Widget>[
                          Align(
                              alignment: Alignment.centerLeft,
                              child: Text('Irene',
                                  style: TextStyle(color: Colors.blue, fontWeight: FontWeight.bold, fontSize: 15.0))),
                          Align(alignment: Alignment.centerLeft, child: Text('Yesterday, Ney York'))
                        ],
                      ),
                    ),
                  ),
                  Align(
                    alignment: Alignment.topRight,
                    child: IconButton(
                      color: Theme.of(context).primaryColor,
                      icon: Icon(Icons.menu),
                      onPressed: () {},
                    ),
                  )
                ],
              ),
              Container(
                width: MediaQuery.of(context).size.width / 1.1,
                height: MediaQuery.of(context).size.height / 2,
                child: Card(
                  child: Image.asset('assets/irene.jpg'),
                  color: Colors.white,
                  shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(30.0)),
                  elevation: 10.0,
                ),
              ),
              Padding(
                padding: const EdgeInsets.only(left: 30.0, right: 30.0, top: 15.0),
                child: Row(
                  children: <Widget>[
                    Icon(Icons.thumb_up, color: Colors.black),
                    SizedBox(
                      width: 20.0,
                    ),
                    Icon(Icons.chat, color: Colors.black),
                    Expanded(child: Container()),
                    Icon(Icons.share, color: Colors.black),
                  ],
                ),
              ),
              Expanded(child: Container()),
            ],
          ),
        );
      }
    }