ios 相对于父高度 Flutter 定位小部件

camsedfj  于 2023-06-07  发布在  iOS
关注(0)|答案(1)|浏览(230)

我有一个图像,我想定位在父高度 * .5,所以例如,如果父高度是500,它应该在250。父高度是动态的,所以它是由size.height * .3或类似的东西设置的。以下是我一直在尝试的代码,但它不起作用:

Positioned(
            //mesh
            top: topPadding + 40,
            child: Container(
                width: size.width,
                child: Center(
                    child: Container(
                        width: size.width * .5,
                        height:
                            size.height - size.height * .1 - topPadding - 40,
                        color: Color.fromARGB(255, 86, 86, 86),
                        child: Stack(alignment: Alignment.center, children: [
                          Image.asset('assets/3d_model/Front_Side/mesh.png'),
                          Positioned(
                              top: 138, //I WANT IT TO BE PARENT HEIGHT * .1, so when you resize the container the image stays in the right spot
                              child: Image.asset(
                                  'assets/3d_model/Front_Side/core.png',
                                  width: 67))// And this should be parent width * .whatever, so that it stays relative with the parent container))
                        ])))),
          ),
hsgswve4

hsgswve41#

使用FractionallySizedBox和Alignment bottomCenter。
示例代码:

import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData.dark(
        useMaterial3: true,
      ),
      home: const MyHomePage(),
      debugShowCheckedModeBanner: false,
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key});

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int height = 200;

  void _onRemovePressed() {
    if (height <= 100) {
      return;
    }
    setState(() {
      height -= 50;
    });
  }

  void _onAddPressed() {
    if (height > MediaQuery.sizeOf(context).height - 100) {
      return;
    }
    setState(() {
      height += 50;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Theme.of(context).colorScheme.inversePrimary,
        title: const Text('Demo'),
        actions: [
          IconButton(
            onPressed: _onAddPressed,
            icon: const Icon(Icons.add),
          ),
          IconButton(
            onPressed: _onRemovePressed,
            icon: const Icon(Icons.remove),
          )
        ],
      ),
      //actual code starts here
      body: SizedBox(
        height: height.toDouble(),
        width: double.infinity,
        child: FractionallySizedBox(
          alignment: Alignment.bottomCenter,
          heightFactor: 0.5,
          child: ColoredBox(
            color: Colors.purple.shade200,
          ),
        ),
      ),
    );
  }
}

相关问题