【问题标题】:Position child container at a fractional location将子容器定位在小数位置
【发布时间】:2018-04-23 01:41:59
【问题描述】:

如何将具有设定大小的正方形放置在堆栈内的特定(小数)位置?

我可以求助于画家,但我想看看是否可以使用小部件。

【问题讨论】:

    标签: flutter


    【解决方案1】:

    您可以在设置大小的Container 中使用DecoratedBox 来绘制一个正方形,并将其放置在Stack 中的任何位置,方法是将其包装在AlignmentPositioned 小部件中:

    import 'package:flutter/material.dart';
    
    void main() {
      runApp(new MyApp());
    }
    
    class MyApp extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return new MaterialApp(
          title: 'Flutter Demo',
          theme: new ThemeData(
            primarySwatch: Colors.blue,
          ),
          home: new StackExamplePage(),
        );
      }
    }
    
    class StackExamplePage extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return new Scaffold(
            backgroundColor: Colors.grey,
            appBar: new AppBar(
                title: new Text('Stack Example')
            ),
            body: _createStack()
        );
      }
    
      _createStack() {
        return new Stack(
            children: <Widget>[
              new Image.network(
                'https://i.imgur.com/FsXL8vI.jpg',
              ),
              // Black square centered in stack
              new Align(
                alignment: new Alignment(0.0, 0.0),
                child: new Container(
                  height: 50.0,
                  width: 50.0,
                  child: new DecoratedBox(
                    decoration: new BoxDecoration(
                        color: Colors.black
                    ),
                  ),
                ),
              ),
              new Align(
                // alignment: Alignment.topLeft,
                alignment: const Alignment(-1.0, -1.0),
                child: new Text('Top Left',
                    style: new TextStyle(color: Colors.yellow)),
              ),
              new Align(
                // alignment: Alignment.bottomRight,
                alignment: const Alignment(1.0, 1.0),
                child: new Text('Bottom Right',
                    style: new TextStyle(color: Colors.yellow)),
              ),
              new Align(
                alignment: new Alignment(-0.8, -0.8),
                child: new Text(
                    '10% in', style: new TextStyle(color: Colors.yellow)),
              ),
              new Align(
                alignment: new Alignment(0.8, 0.8),
                child: new Text(
                    '90% in', style: new TextStyle(color: Colors.yellow)),
              ),
            ]
        );
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-07-09
      • 2011-07-20
      • 2011-02-07
      • 2018-10-06
      • 2012-11-10
      • 2021-07-18
      • 1970-01-01
      相关资源
      最近更新 更多