【问题标题】:How can I set two container on top of each other?如何将两个容器放在一起?
【发布时间】:2021-07-03 06:48:25
【问题描述】:

我试图将 2 个容器放在一起。我想要的是显示一颗星星,当用户按下星星时,我想打开 5 颗星,这样他就可以投票了。我不确定我的想法是否正确。但是,我想创建一个容器,并在该星形容器正上方的另一个容器中显示所有其他按钮,只是按钮可以正常计时。我希望你明白我想做什么。因此,当我将星形容器也放在同一个容器中时,它们在打开所有星形时会按按钮,整个容器不会移动吗? 所以这是我想要做的。


class VideoPage extends StatelessWidget {
  static const route = '/Video';
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Column(
        children: [
          Container(
            height: 100,
            color: Colors.yellow[300],
          ),
          Expanded(
            child: Row(
              children: [
                Expanded(
                  child: Container(color: Colors.green[300]),
                ),
                Container(
                  width: 100,
                  color: Colors.orange[300],
                ),
                Container(
                  width: 100,
                  color: Colors.red[300],
                ),
              ],
            ),
          ),
          Container(
            height: 80,
            color: Colors.blue[300],
          ),
        ],
      ),
    );
  }
}

看起来像这样:

我希望橙色的在红色的正下方,所以会有第一颗星,按下它会打开 5 颗星。这可能是我正在尝试的吗?如果是这样,请帮助。如果没有请帮忙哈哈

【问题讨论】:

    标签: flutter dart containers flutter-design


    【解决方案1】:

    您需要使用的是Stack Widget:

    Stack(
      children: <Widget>[
        BottomWidget(),
        MiddleWidget(),
        TopWidget(),
      ],
    ),
    

    孩子的最后一个小部件是顶层,结构递减。

    更多信息:https://medium.com/flutter-community/a-deep-dive-into-stack-in-flutter-3264619b3a77

    编辑: 在你的情况下,如果你想要下面的橙色,你必须在你的结构中做一些改变,并将它从你的 Row() 中拆分出来。


    示例

    import 'package:flutter/material.dart';
    
    final Color darkBlue = Color.fromARGB(255, 18, 32, 47);
    
    void main() {
      runApp(MyApp());
    }
    
    class MyApp extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          theme: ThemeData.dark().copyWith(scaffoldBackgroundColor: darkBlue),
          debugShowCheckedModeBanner: false,
          home: Scaffold(
            body: Center(
              child: MyWidget(),
            ),
          ),
        );
      }
    }
    
    class MyWidget extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          body: Column(
            children: [
              Container(
                height: 100,
                color: Colors.yellow[300],
              ),
              Stack(
                children: <Widget>[
                  
                  Container(
                      width: MediaQuery.of(context).size.width *1,
                      height: 200,
                      color: Colors.orange,
                    ),
                  
                 Container(
                   height:200,
                   child: Row(
                  children: [
                    Expanded(
                      child: Container(color: Colors.green[300]),
                    ),
                    
                    Container(
                      width: 100,
                      color: Colors.red.withOpacity(0.5),
                    ),
                  ],
                )),
                  
      ],
              
              ),
              Container(
                height: 80,
                color: Colors.blue[300],
              ),
            ],
          ),
        );
      }
    }
    

    【讨论】:

    • 橙色应该在红色下面。当我将两者都放入堆栈并从行中删除时,我再也看不到它们了
    • 检查我编辑的答案,我为您提供了一个显示下方橙色的示例。要将其更改为适合您的尺寸,您将不得不对结构进行调整,但这为您提供了位于其余部分下方的容器。
    • 仍然没有看到橙色的。你之前有测试过吗?
    • 橙色的应该在红色的下方,蓝色的上方
    • 是的,我什至降低了红色的不透明度,以便您可以看到下面的橙色。我用整个宽度做了橙色的。如果您希望它高于蓝色,您还应该将蓝色从中拆分出来。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-05-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多