如何在 Flutter 中相對於另一個小部件定位小部件? (how can I position a widget relative to another in Flutter?)


問題描述

如何在 Flutter 中相對於另一個小部件定位小部件? (how can I position a widget relative to another in Flutter?)

我有一張圖片,我想在上面放置小圖片。我已經有了這些圖像相對於原始圖像的位置(寬度、高度、topLeftX、topLeftY、bottomRight..)。

我無法放置這些小圖像(由藍色框表示在下面的附加示例中)關於圖像。它們總是相對於整個屏幕定位。

這是我嘗試過的:

      Stack(

            children:[

            Positioned(
              top: 245,
              left: 43,
             width: 200,height: 200,child:Container(color:Colors.blue)),
          Container(

              child:PhotoView(imageProvider: AssetImage("lib/assets/3D_example_2.png"),
          controller: controller,

          )),])

我怎樣才能相對於圖像放置我的圖像(矩形)?我還想相對於原始圖像縮放這些圖像的寬度和高度 允許它們在縮放時與原始圖像一起移動..

知道如何做到這一點嗎?

謝謝!


參考解法

方法 1:

import 'package:flutter/material.dart';

void main() {
runApp(
MaterialApp(
debugShowCheckedModeBanner: false,
home: HomePage(),
),
);
}

class HomePage extends StatefulWidget {
@override
_HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(
"Rough Work",
),
centerTitle: true,
),
body: Stack(
overflow: Overflow.visible,
children: [
Positioned(
top: 200,
left: 100,
child: Container(
height: 200,
width: 200,
color: Colors.teal,
child: Stack(
children: [
Positioned(
top: 12,
child: Container(
height: 150,
width: 150,
child: Image(
image: NetworkImage(
"https://images.pexels.com/photos/736230/pexels‑photo‑736230.jpeg?auto=compress&amp;cs=tinysrgb&amp;dpr=1&amp;w=500&quot;),
),
),
),
],
),
),
),
],
),
);
}
}
</code></pre>

方法 2:

I would suggest adding the width and height of the boxes to the Container widget instead, but apart from that, there seems to be no problem with your code. So I thing the behavior can be related to a parent widget:

Checkout the following code:

import 'package:flutter/material.dart';

void main() => runApp(
  MaterialApp(home: HomePage(),
    theme: ThemeData.light()
  ),
);
class HomePage extends StatefulWidget {

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

class _HomePageState extends State<HomePage> {

  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            Stack(
              overflow: Overflow.visible,
              children: [
                Positioned(
                  top: 245,
                  left: 43,
                  child: Container(
                    color: Colors.lightGreen,
                    width: 100,
                    height: 100,
                  ),
                ),
                Positioned(
                  top: ‑50,
                  left: 120,
                  child: Container(
                    color: Colors.redAccent,
                    width: 100,
                    height: 100,
                  ),
                ),
                Container(
                  color: Colors.indigo,
                  width: 200,
                  height: 200,
                ),
              ],
            )
          ],
        ),
      ),
    );
  }

}

(by DCodesAjay GajeraRodrigo Cardozo)

參考文件

  1. how can I position a widget relative to another in Flutter? (CC BY‑SA 2.5/3.0/4.0)

#scale #widget #Flutter #position #stack






相關問題

Java縮放像素? (Java scaled pixels?)

Android - Webview 縮放圖像以適應最大尺寸 (Android - Webview scale image to fit biggest dimension)

使用 CGAffineTransform 時限制 UIImageView 的大小 (Limitting UIImageView size while using CGAffineTransform)

Android WallpaperManager 將位圖縮放得太小 (Android WallpaperManager scales Bitmap too small)

如何在沒有像素模糊的情況下調整 html5 畫布的大小 (How to resize html5 canvas without pixel blur)

查看 Spark 中派生的機器學習模型 (view machine learning model derived in Spark)

圖像保持點位置的比例分辨率 (Scale resolution of image keeping point locations)

ScaleGestureDetector.OnScaleGestureListener.onScaleEnd() 方法未命中 (ScaleGestureDetector.OnScaleGestureListener.onScaleEnd() method not being hit)

在 scale_x_continuous 中使用日期與中斷 (Using date in scale_x_continuous with breaks)

如何在不破壞容器邊界半徑的情況下創建與 Safari 兼容的過渡變換比例 (How would I create a Safari-compatible Transition Transform Scale without breaking the container's border-radius)

如何在 Flutter 中相對於另一個小部件定位小部件? (how can I position a widget relative to another in Flutter?)

Android 導入的位圖在 Canvas 上太大 (Android imported Bitmaps too large on Canvas)







留言討論