【问题标题】:Flutter relative position of elements inside container颤动容器内元素的相对位置
【发布时间】:2020-10-10 06:28:31
【问题描述】:

我有以下稍作修改的标准应用程序,其中 按钮和结果放在一个容器中。

import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      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: containerWidget()
      ),
    );
  }
}

class containerWidget extends StatefulWidget {
  @override
  _containerWidgetState createState() => _containerWidgetState();
}

class _containerWidgetState extends State<containerWidget> {

  int _counter = 0;

  void _incrementCounter() {
    setState(() {
      _counter++;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      color: Colors.red,
      height : 300,
      width  : 300,
      child:
      Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
          Text(
            'You have pushed the button this many times:',
          ),
          Text(
            '$_counter',
            style: Theme.of(context).textTheme.headline4,
          ),
          FloatingActionButton(
                  onPressed: _incrementCounter,
                  tooltip: 'Increment',
                  child: Icon(Icons.add),
                
              ),
        ],
      ),
    );
  }
}

第一步,我想获取屏幕的宽度和大小,以设置容器小部件在屏幕内的相对位置。

在第二步中,我想修改 Container 小部件中 Button 的位置,这意味着要么

  • 通过获取容器的绝对位置来设置按钮相对于屏幕大小的绝对位置

  • 通过设置按钮在容器内的相对位置
Positioned(left: 30.0,
           top: 50.0,
           child: Container(
                width: 100.0,
                height: 80.0,
                decoration: new BoxDecoration(color: Colors.red),
                child: ...

【问题讨论】:

  • 你到底想做什么?听起来您应该能够使用中心小部件和填充来完成此操作,或者至少使用 Flex 小部件。
  • 是的,但我想完全控制定位。

标签: flutter dart


【解决方案1】:

对于屏幕的高度和宽度,您可以查看以下答案: Flutter screen size

如果您想相对于父容器移动按钮,您可以使用 Stack 小部件包装容器,并将容器和按钮设置为其子项(这意味着您必须将按钮移动到容器外),然后只需使用 Positioned 小部件包装按钮并使用参数right : , left : , top : , bottom : , 来控制按钮相对于容器的位置

//for example, this means the container is 30px to the left and 50px from the top
Positioned(left: 30.0,
           top: 50.0,
           child: Container()),

【讨论】:

    猜你喜欢
    • 2020-08-17
    • 2011-09-23
    • 2012-07-21
    • 1970-01-01
    • 1970-01-01
    • 2010-09-11
    • 2020-06-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多