颤振底部溢出无限像素

时间:2019-05-29 07:11:30

标签: flutter dart

我遇到的问题是,当我向列的子级中添加某个小部件时,我会不断出现“无限像素底部溢出”的问题。在添加名为countdown的新窗口小部件之前,现在是这样:

enter image description here

以下是我添加countdown之后发生的事情:

enter image description here

这是屏幕下半部分的代码,我在其中添加countdown

    final countdown = Countdown();

    final quizBottomContent = SingleChildScrollView(
      child: Container(
      width: MediaQuery.of(context).size.width,
        child: Column(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: <Widget>[quizBottomContentText, quizOptions, countdown],
        ),
      )
    ); 

这是倒数计时小部件:

import 'package:flutter/material.dart';
import 'dart:math' as math;

class Countdown extends StatefulWidget {
  @override
  CountdownState createState() => CountdownState();
}

class CountdownState extends State<Countdown> with TickerProviderStateMixin {
  AnimationController controller;

  String get timerString {
    Duration duration = controller.duration * controller.value;
    return '${duration.inMinutes}:${(duration.inSeconds % 60).toString().padLeft(2, '0')}';
  }

  @override
  void initState() {
    super.initState();
    controller = AnimationController(
      vsync: this,
      duration: Duration(seconds: 10),
    );
  }

  @override
  Widget build(BuildContext context) {
    ThemeData themeData = Theme.of(context);
    return Scaffold(
      body: Padding(
        padding: EdgeInsets.all(8.0),
        child: Column(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: <Widget>[
            Expanded(
              child: Align(
                alignment: FractionalOffset.center,
                child: AspectRatio(
                  aspectRatio: 1.0,
                  child: Stack(
                    children: <Widget>[
                      Positioned.fill(
                        child: AnimatedBuilder(
                          animation: controller,
                          builder: (BuildContext context, Widget child) {
                            return CustomPaint(
                                painter: TimerPainter(
                              animation: controller,
                              backgroundColor: Colors.white,
                              color: themeData.indicatorColor,
                            ));
                          },
                        ),
                      ),
                      Align(
                        alignment: FractionalOffset.center,
                        child: Column(
                          mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                          crossAxisAlignment: CrossAxisAlignment.center,
                          children: <Widget>[
                            Text(
                              "Count Down",
                              style: themeData.textTheme.subhead,
                            ),
                            AnimatedBuilder(
                                animation: controller,
                                builder: (BuildContext context, Widget child) {
                                  return Text(
                                    timerString,
                                    style: themeData.textTheme.display4,
                                  );
                                }),
                          ],
                        ),
                      ),
                    ],
                  ),
                ),
              ),
            ),
            Container(
              margin: EdgeInsets.all(8.0),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                children: <Widget>[
                  FloatingActionButton(
                    child: AnimatedBuilder(
                      animation: controller,
                      builder: (BuildContext context, Widget child) {
                        return Icon(controller.isAnimating
                            ? Icons.pause
                            : Icons.play_arrow);
                      },
                    ),
                    onPressed: () {
                      if (controller.isAnimating)
                        controller.stop();
                      else {
                        controller.reverse(
                            from: controller.value == 0.0
                                ? 1.0
                                : controller.value);
                      }
                    },
                  )
                ],
              ),
            )
          ],
        ),
      ),
    );
  }
}

class TimerPainter extends CustomPainter {
  TimerPainter({
    this.animation,
    this.backgroundColor,
    this.color,
  }) : super(repaint: animation);

  final Animation<double> animation;
  final Color backgroundColor, color;

  @override
  void paint(Canvas canvas, Size size) {
    Paint paint = Paint()
      ..color = backgroundColor
      ..strokeWidth = 5.0
      ..strokeCap = StrokeCap.round
      ..style = PaintingStyle.stroke;

    canvas.drawCircle(size.center(Offset.zero), size.width / 2.0, paint);
    paint.color = color;
    double progress = (1.0 - animation.value) * 2 * math.pi;
    canvas.drawArc(Offset.zero & size, math.pi * 1.5, -progress, false, paint);
  }

  @override
  bool shouldRepaint(TimerPainter old) {
    return animation.value != old.animation.value ||
        color != old.color ||
        backgroundColor != old.backgroundColor;
  }
}

某些情况: 倒数基本上是带有动画的计时器,而倒数我是从link中提取的。因此计时器将位于按钮下方。

3 个答案:

答案 0 :(得分:1)

尝试设置列的mainaxissize mainAxisSize: MainAxisSize.min,

更新

好的,因此,如果您尝试将Countdown小部件作为一列中的小部件,则可以从count down小部件的构建方法中删除支架并将其包装在容器中并定义高度

 return Scaffold(
      body: Padding(
        padding: EdgeInsets.all(8.0),
        child: Column(

 return Container(
         height: 100,
         child: Padding(
              padding: EdgeInsets.all(8.0),
              child: Column(

答案 1 :(得分:1)

我有类似的问题,包装 quizBottomContent 使用 Expanded 小部件为我解决了这个问题:

final quizBottomContent = Expanded(
  child: SingleChildScrollView(
    ...
  )
);

答案 2 :(得分:-2)

只需用容器包装该小部件并指定高度即可