颤振卡颜色

时间:2019-07-26 14:15:25

标签: flutter flutter-layout

我想制作一张我已分成几列的卡片(扩展的小部件),我面临的问题是:我无法将颜色容器(右列的父对象)设置为全高,因此只显示了一部分彩色背景。

我所拥有的:

enter image description here

我想要的东西: enter image description here

  • 我尝试了Flutter Inspector工具,但发现Container及其子列未达到完整高度(即使在键入mainAxisSize:MainAxisSize.max之后)

  • 我也尝试使用FractionSized ..而不是使用Expanded。

代码:

    import 'package:flutter/material.dart';

    class SubjectPage extends StatelessWidget {
      final String dayStr;
      SubjectPage(this.dayStr);

      @override
      Widget build(BuildContext context) {
        return Padding(
          padding: EdgeInsets.fromLTRB(20.0, 40.0, 20.0, 0),
          child: Column(
            children: <Widget>[
              Card(
                child: Row(
                  children: <Widget>[
                    // Column 1
                    Expanded(
                      flex: 7,
                      child: Padding(
                        padding: EdgeInsets.all(20.0),
                        child: Column(
                          mainAxisSize: MainAxisSize.min,
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: <Widget>[
                            getTitle("UEC607"),
                            getSubName("Data Communication & Protocol"),
                            getVenue("E-204"),
                          ],
                        ),
                      ),
                    ),
                    // Column 2
                    // The Place where I am Stuck//
                    Expanded(
                      flex: 3,
                      child: Container(
                        color: Colors.blue,
                        child: Column(
                          mainAxisSize: MainAxisSize.max,
                          children: <Widget>[
                            getType("L"),
                            getTime("9:00"),
                          ],
                        ),
                      ),
                    )
                  // COlumn 2 End
                  ],
                ),
              )
            ],
          ),
        );
      }

      // Get Title Widget
      Widget getTitle(String title) {
        return Padding(
          padding: EdgeInsets.only(bottom: 8.0),
          child: Text(title),
        );
      }

      // Get Subject Name
      Widget getSubName(String subName) {
        return Padding(
          padding: EdgeInsets.only(bottom: 25.0),
          child: Text(subName),
        );
      }

      // Get Venue Name
      Widget getVenue(String venue) {
        return Padding(
          padding: EdgeInsets.only(bottom: 0.0),
          child: Text(venue),
        );
      }

      Widget getType(String type) {
        return Padding(
          padding: EdgeInsets.only(bottom: 10.0),
          child: Text(type),
        );
      }

      Color getColor(String type) {
        if (type == "L") {
          return Color(0xff74B1E9);
        }
      }

      Widget getTime(String time) {
        return Text(time);
      }
    }

4 个答案:

答案 0 :(得分:1)

对我来说,有助于确保哪个Widget应该使用固定的空间/大小,哪些应该扩展以适合空间/大小

然后,我使用ColumnRow来构建窗口小部件的布局(这使得可以很容易地进行预修复并决定将窗口小部件包装在ExpandedFlexible中的位置) )

但这是一个简短的示例:

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        centerTitle: true,
        title: Text('Stackoverflow'),
      ),

      body: Container(
        margin: EdgeInsets.all(10),

        width: double.infinity,
        height: 100,

        child: Row(
          children: <Widget>[
            // Texts
            Expanded(
              child: Column(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: <Widget>[
                  // UEC607 & Digital communication text
                  Column(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: <Widget>[
                      Row(
                        mainAxisAlignment: MainAxisAlignment.start,
                        children: <Widget>[
                          Text(
                            'UEC607',                      
                            style: TextStyle(
                              fontSize: 16,
                              fontWeight: FontWeight.bold,
                              color: Colors.grey
                            ),                      
                          ),
                        ],
                      ),
                      SizedBox(
                        height: 5,
                      ),
                      Row(
                        mainAxisAlignment: MainAxisAlignment.start,
                        children: <Widget>[
                          Text(
                            'Digital communication',
                            style: TextStyle(
                              fontSize: 20,                        
                              color: Colors.black
                            ),
                          ),
                        ],
                      )
                    ],
                  ),

                  Expanded(
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.start,
                      crossAxisAlignment: CrossAxisAlignment.end,
                      children: <Widget>[
                        Text(
                          'E-206',
                          style: TextStyle(
                            fontSize: 18,
                            fontWeight: FontWeight.bold,
                            color: Colors.black
                          ),
                        ),
                      ],
                    ),
                  )
                ],
              ),
            ),

            // Card
            Container(
              width: 90,

              color: Colors.lightBlue.withOpacity(.8),

              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: <Widget>[
                  Text(
                    'L',
                    style: TextStyle(
                      fontSize: 24,
                      fontWeight: FontWeight.bold,
                      color: Colors.white
                    ),
                  ),
                  SizedBox(
                    height: 5,
                  ),
                  Text(
                    '09:00',
                    style: TextStyle(
                      fontSize: 24,
                      fontWeight: FontWeight.bold,
                      color: Colors.white
                    ),
                  ),
                ],
              ),
            )
          ],
        ),
      ),
    );
  }

它看起来像这样:

enter image description here

答案 1 :(得分:1)

使用FittedBox包装蓝色容器

Expanded(
                  flex: 2,
                  child: FittedBox(
                    child: Container(
                      color: Colors.blue,
                      child: Column(
                        mainAxisSize: MainAxisSize.max,
                        children: <Widget>[
                          getType("L"),
                          getTime("9:00"),
                        ],
                      ),
                    ),
                  ),
                )

答案 2 :(得分:1)

您应该将 Row 小部件放在 IntrinsicHeight 小部件中,并将 crossAxisAlignmentRow 属性设置为等于 CrossAxisAlignment.stretch。它将解决您的问题。

IntrinsicHeight(
   child: Row(
      crossAxisAlignment: CrossAxisAlignment.stretch,
      children: <Widget>[]
   ),
)

答案 3 :(得分:0)

设置容器高度

Expanded(
                      flex: 3,
                      child: Container(
                        height: double.infinity,
                        color: Colors.blue,
                        child: Column(
                          mainAxisSize: MainAxisSize.max,
                          children: <Widget>[
                            getType("L"),
                            getTime("9:00"),
                          ],
                        ),
                      ),
                    )