简体   繁体   English

无论屏幕大小如何,如何使容器保持原位

[英]How Can I Make a Container stay in position no matter the size of the screen in flutter

I've got a really cool design where I have a green container hidden behind the first line of containers in my grid.我有一个非常酷的设计,我在网格的第一行容器后面隐藏了一个绿色容器。 I used media query to give the container a height so it appears halfway behind the top white containers.我使用媒体查询给容器一个高度,使它出现在顶部白色容器的中间。 The problem now is whenever my app is used on a larger device like a tab or in landscape mode, the screen height distorts and the green container doesn't appear in the right position since I specifically gave it a hard-coded height which will change when the screen is of another size.现在的问题是,每当我的应用程序在较大的设备(如选项卡或横向模式)上使用时,屏幕高度就会扭曲,并且绿色容器不会出现在正确的位置,因为我专门给它一个硬编码的高度,它会改变当屏幕是另一个尺寸时。 Please how do I get the green container to always stay in the position I want (behind the first line of white containers) no matter the screen size of the device?无论设备的屏幕尺寸如何,如何让绿色容器始终保持在我想要的位置(在第一行白色容器之后)? Mind you, I don't want to use landscape mode in my app, I'm just using it as an example.请注意,我不想在我的应用程序中使用横向模式,我只是将其用作示例。 I want the position of the scree box to always stay where I want it no matter if its in an iPad, tablet of on web.我希望碎石盒的位置始终保持在我想要的位置,无论它是在 iPad、平板电脑还是网络上。

image 1图 1

here is the code for my page这是我的页面的代码

class ActivityPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final lightModeOn = Theme.of(context).brightness == Brightness.light;
    var size = MediaQuery.of(context)
        .size; //this gonna give us total height and with of our device
    return Scaffold(
      backgroundColor: Theme.of(context).scaffoldBackgroundColor,
      body: Stack(
        children: <Widget>[
          Container(
            // Here the height of the green container is 30% of our total height
            height: size.height * .30,
            decoration: BoxDecoration(
              color: Theme.of(context).canvasColor,
            ),
          ),
          SafeArea(
            child: Padding(
              padding: const EdgeInsets.symmetric(horizontal: 20),
              child: Column(
                children: <Widget>[
                  Padding(
                    padding: EdgeInsets.fromLTRB(0, 50, 0, 10),
                    child: Center(
                      child: TitleText(
                        text: "activity",
                      ),
                    ),
                  ),
                  Expanded(
                    child: Padding(
                      padding: const EdgeInsets.all(10.0),
                      child: ResponsiveGridList(
                        horizontalGridSpacing: 20, // Horizontal space between grid items// Vertical space between grid items
                        verticalGridSpacing: 20,
                        minItemWidth: 100, // The minimum item width (can be smaller, if the layout constraints are smaller)
                        minItemsPerRow: 2, // The minimum items to show in a single row. Takes precedence over minItemWidth
                        maxItemsPerRow: 4,
                        children: <Widget>[
                          ActivityContainer(
                            title: 'rate app',
                            image: 'assets/images/rateapp.png',
                            press: () {
                              showModalBottomSheet(
                                shape: RoundedRectangleBorder(
                                  borderRadius: BorderRadius.only(
                                      topLeft: Radius.circular(10),
                                      topRight: Radius.circular(10)),
                                ),
                                isDismissible: false,
                                isScrollControlled: true,
                                context: context,
                                builder: (context) => RateUsModal(),
                              );
                            },
                          ),
                          ActivityContainer(
                            title: 'share app',
                            image: 'assets/images/shareapp.png',
                            press: () {
                              showModalBottomSheet(
                                shape: RoundedRectangleBorder(
                                  borderRadius: BorderRadius.only(
                                      topLeft: Radius.circular(10),
                                      topRight: Radius.circular(10)),
                                ),
                                isDismissible: false,
                                isScrollControlled: true,
                                context: context,
                                builder: (context) => ShareAppModal(),
                              );
                            },
                          ),
                          ActivityContainer(
                            title: 'contact us',
                            image: 'assets/images/contactus.png',
                            press: () {
                              showModalBottomSheet(
                                shape: RoundedRectangleBorder(
                                  borderRadius: BorderRadius.only(
                                      topLeft: Radius.circular(10),
                                      topRight: Radius.circular(10)),
                                ),
                                isDismissible: false,
                                isScrollControlled: true,
                                backgroundColor: lightModeOn
                                    ? Color(0xFFFFFFFF)
                                    : Color(0xFF282828),
                                context: context,
                                builder: (context) => ContactUsModal(),
                              );
                            },
                          ),
                          ActivityContainer(
                            title: lightModeOn
                                ? 'light !!!'
                                : 'dark !!!',
                            image: lightModeOn
                                ? 'assets/images/lighttheme.png'
                                : 'assets/images/darktheme.png',
                            press: () {
                              showModalBottomSheet(
                                shape: RoundedRectangleBorder(
                                  borderRadius: BorderRadius.only(
                                      topLeft: Radius.circular(10),
                                      topRight: Radius.circular(10)),
                                ),
                                isDismissible: false,
                                isScrollControlled: true,
                                backgroundColor: lightModeOn
                                    ? Color(0xFFFFFFFF)
                                    : Color(0xFF282828),
                                context: context,
                                builder: (context) => SwitchThemeModal(),
                              );
                            },
                          ),
                        ],
                      ),
                    ),
                  ),
                ],
              ),
            ),
          ),
        ],
      ),
    );
  }
}

the second image is of the page when it's in landscape mode.第二张图片是页面处于横向模式时的图片。 The green box doesn't appear behind the line of containers again because the screen size is now smaller and since the container takes 30 percent of the screen it appears above the white container.绿色框不再出现在容器行的后面,因为屏幕尺寸现在变小了,而且容器占据了屏幕的 30%,它出现在白色容器的上方。 How can I fix我该如何解决

image 2 landscape图 2 风景

Instead of using a percentage I would use a constant value.我不会使用百分比,而是使用恒定值。 I assume you want to have the green area stop somewhere in the middle of the first tiles in the grid?我假设您想让绿色区域停在网格中第一个图块中间的某个地方? You could make the sum of those paddings (I think that's 20 + 50 + 10 + half of a tile height) and use that as the Container 's height.您可以将这些填充的总和(我认为这是 20 + 50 + 10 + 瓷砖高度的一半)并将其用作Container的高度。

Since you're also using a SafeArea , you might want to have a SizedBox and SafeArea in the green Container .由于您也在使用SafeArea ,因此您可能希望在绿色Container中有一个SizedBoxSafeArea

const _contentPaddingTop = 20.0;
const _titleTextPaddingTop = 50.0;
const _gridPaddingTop = 10.0;
const _tileHeight = 100; // making a guess here, don't see it in your code
const _heightUntilMidFirstGridTiles = _contentPaddingTop + _titleTextPaddingTop + _gridPaddingTop + _tileHeight / 2;

// ... 
Container(
  decoration: BoxDecoration(
    color: Theme.of(context).canvasColor,
  ),
  child: SafeArea(
    child: SizedBox(
      height: _heightUntilMidFirstGridTiles,
    ),
  ),
),
// ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM