flutter - 无限像素溢出的颤动底部
问题描述
我正面临这个问题,当我将某个小部件添加到列的子项中时,我不断得到“底部被无限像素溢出”。现在这是添加名为的新小部件之前的样子countdown
:
以下是我添加后发生的情况countdown
:
这是屏幕下半部分的代码,我在其中添加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;
}
}
一些上下文:倒计时基本上是一个带有动画的计时器,我从这个链接中提取倒计时。所以计时器就在按钮的下方。
解决方案
尝试设置列的主轴大小
mainAxisSize: MainAxisSize.min,
更新
好的,所以如果您尝试将倒计时小部件作为列中的小部件,您可以从倒计时小部件的构建方法中移除脚手架并将其包装在容器中并定义高度
return Scaffold(
body: Padding(
padding: EdgeInsets.all(8.0),
child: Column(
到
return Container(
height: 100,
child: Padding(
padding: EdgeInsets.all(8.0),
child: Column(
推荐阅读
- php - 从 WooCommerce 消息中删除链接
- reactjs - 在 React 中设置 Medium Feed
- javascript - html5隐藏所有div并在按钮单击时显示一个
- kubernetes - Kubernetes maxPods int32 还包括其默认的 pod
- macos - 使用 Open Firmware 设置硬件默认值
- java - 两个Fragment之间的接口通信
- nlp - 用于 CRF 分类器的 StanfordNLP 训练迭代
- r - 如何使用 quantmode 向多个烛台图表添加简单的移动平均线
- asp.net - Visual Studio 2017 不会编译我的 ASP.NET Web 项目,对肯定被引用的库说“无法加载文件或程序集”
- javascript - 空复选框上的 Javascript“空”验证消息