简体   繁体   English

将一个小部件定位在另一个小部件颤动上

[英]Positioning a widget on another widget flutter

Positioning the widget like the image below: (green over the red.)如下图所示定位小部件:(绿色覆盖红色。)

这里是代表

You can use a Stack with an overflow property of Overflow.visible and an alignment of Alignemnt.center , together with a Positioned widget with negative bottom value to achieve what you want.您可以使用Stackoverflow的财产Overflow.visiblealignmentAlignemnt.center ,连同Positioned与负小部件bottom值来达到你想要什么。 Here is a snippet I wrote for you, I tried to imitate the image you shared.这是我为您写的片段,我试图模仿您分享的图像。 You can run it on DartPad if you need to manipulate the parameters or explore further.如果您需要操作参数或进一步探索,您可以在 DartPad 上运行它。

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      debugShowCheckedModeBanner: false,
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Stack(
          overflow: Overflow.visible,
          alignment: Alignment.center,
          children: <Widget>[
            Container(
              height: 150,
              width: 300,
              color: Colors.red
            ),
            Positioned(
              bottom: -50,
              child: Container(
                height: 100,
                width: 150,
                color: Colors.green
              ),
            ),
          ],
        ),
      ),
    );
  }
}

Here is what it looks like:这是它的样子:

结果

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

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