【问题标题】:How do I Align my widgets with MediaQuery for all devices in Flutter?如何将我的小部件与 Flutter 中所有设备的 MediaQuery 对齐?
【发布时间】:2020-07-04 14:52:56
【问题描述】:

我尝试在屏幕上对齐三个小部件以相互引用。

我有一个金色边框,我想将其设置为我的背景,中心的轮子和轮子的中心,这也是一个单独的资产。

我已经使用 MediaQuery 对齐了资产,但是每次我在不同的设备上调试它时,MediaQuery 都会有一些差异,并且 alignmnet 不正确。 这就是我调用我的小部件的方式:

    Widget build(BuildContext context) {
        return Scaffold(
          body: Stack(
            children: <Widget>[
//this is for the GOLDEN BORDER
              Positioned(
                  top: MediaQuery.of(context).size.height * 0.082,
                  child: Image.asset(
                    'assets/app/border3.png',
                    height: MediaQuery.of(context).size.width,
                  )),

//This is the WHEEL
              Column(
          children: <Widget>[
            SizedBox(height: MediaQuery.of(context).size.height * 0.344),
            Container(
              //color: Theme.of(context).primaryColor.withAlpha(180),
              child: Center(
                child: Winwheel(
                  handleCallback: ((handler) {
                    ctrl = handler;
                  }),
                  textFontFamily: 'Netflix',
                  controller: ctrl,
                  numSegments: 3,
                  outerRadius: MediaQuery.of(context).size.height * 0.41 / 2,
                  innerRadius: 28,
                  strokeStyle: Colors.white,
                  textFontSize: 20.0,
                  textFillStyle: Colors.white,
                  textFontWeight: FontWeight.bold,
                  textAlignment: WinwheelTextAlignment.center,
                  textOrientation: WinwheelTextOrientation.horizontal,
                  wheelImage: 'assets/app/spin2.png',
                  drawMode: WinwheelDrawMode.code,
                  drawText: true,
                  imageOverlay: false,
                  textMargin: 0,
                  pointerAngle: 0,
                  pointerGuide: PointerGuide(
                    display: true,
                  ),
                  segments: <Segment>[
                    Segment(
                        textFontFamily: 'Netflix',
                        fillStyle: Color(0xff9b57fc),
                        textFillStyle: Colors.white,
                        text: '400',
                        strokeStyle: Colors.transparent),
                    Segment(
                      textFontFamily: 'Netflix',
                      fillStyle: Color(0xff17a8f9),
                      textFillStyle: Colors.white,
                      text: '400',
                      strokeStyle: Colors.transparent,
                    ),
                    Segment(
                      textFontFamily: 'Netflix',
                      fillStyle: Colors.pink,
                      textFillStyle: Colors.white,
                      text: '900',
                      strokeStyle: Colors.transparent,
                    ),
                    Segment(
                      textFontFamily: 'Netflix',
                      fillStyle: Color(0xffef225b),
                      textFillStyle: Colors.white,
                      text: '500',
                      strokeStyle: Colors.transparent,
                    ),
                  ],
                  pins: Pin(
                    visible: false,
                    number: 16,
                    margin: 6,
                    // outerRadius: 5,
                    fillStyle: Colors.transparent,
                  ),
                  animation: WinwheelAnimation(
                    type: WinwheelAnimationType.spinToStop,
                    spins: 4,
                    duration: const Duration(
                      seconds: 15,
                    ),
                    callbackFinished: (int segment) {
                      setState(() {
                        isPlaying = false;
                      });
    
                      print('animation finished');
                      print(segment);
                    },
                    callbackBefore: () {
                      setState(() {
                        isPlaying = true;
                      });
                    },
                  ),
                ),
              ),
            ),
          ],
        ),

//This is the GOLDEN CENTER

              Positioned(
                  top: MediaQuery.of(context).size.height * 0.285,
                  left: MediaQuery.of(context).size.width * 0.359,
                  child: Image.asset('assets/app/center.png',
                      height: MediaQuery.of(context).size.height * 0.16)),
            ],
          ),
        );
      }

我正在为 Spinning wheel 使用 winwheel 依赖项。 这就是最终输出的样子。尽管在图像中看起来不错,但它并未在所有设备中正确对齐

.

【问题讨论】:

    标签: flutter user-interface dart


    【解决方案1】:

    也许您可以尝试使用 aspectRatio 为 1.0 的 AspectRatio Widget 以保持宽度和高度相同(作为一个圆圈)并将堆栈居中对齐

    Scaffold(
      body: Center(
        child: Padding(
          padding: EdgeInsets.all(8),
            child: AspectRatio(
              aspectRatio: 1.0, //Give it an aspectRatio of 1
              child: MyWidget()
            ),
          )
      ),
    ),
    
    class MyWidget extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return Stack(
          alignment: Alignment.center, //align the Widgets to the Center of the Stack
          children: [
            //this is for the WHEEL
            Container(
              decoration: BoxDecoration(
                color: Colors.orange[800],
                shape: BoxShape.circle,
              ),
            ),
            //This is the GOLDEN BORDER
            Container(
              decoration: BoxDecoration(
                border: Border.all(width: 20, color: Colors.yellow),
                color: Colors.transparent,
                shape: BoxShape.circle,
              ),
            ),
            //This is the GOLDEN CENTER
            Container(
              width: MediaQuery.of(context).size.width / 12,
              decoration: BoxDecoration(
                color: Colors.yellow[200],
                shape: BoxShape.circle,
              ),
            ),
          ]
        );
      }
    }
    

    我没有资产,所以我制作了一些带有颜色的容器,但它应该看起来像这样

    所以你可以在没有定位小部件的情况下尝试它(堆栈现在居中,所以不需要)

    class MyWidget extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return Stack(
          alignment: Alignment.center, //align the Widgets to the Center of the Stack
          children: [
            //This is the WHEEL
            Winwheel(
             ....
            ),
            //this is for the GOLDEN BORDER
            Image.asset('assets/app/border3.png', fit: BoxFit.contain), //or try BoxFit.scaledown if that doesn't work
            //This is the GOLDEN CENTER
            Image.asset('assets/app/center.png', width: MediaQuery.of(context).size.width / 12, fit: BoxFit.scaleDown)
          ]
        );
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2020-02-21
      • 2019-08-11
      • 2021-07-30
      • 2020-10-08
      • 2020-06-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-16
      相关资源
      最近更新 更多