gpt4 book ai didi

flutter _debugLifecycleState!= _ElementLifecycle.defunct' : is not true

转载 作者:行者123 更新时间:2023-12-04 08:17:13 27 4
gpt4 key购买 nike

我在我的应用程序中显示了一些动画自定义画家进度条,它显示了一些错误

Error: The following assertion was thrown while notifying listeners for AnimationController:
'package:flutter/src/widgets/framework.dart': Failed assertion: line 4263 pos 12: '_debugLifecycleState != _ElementLifecycle.defunct': is not true.
我有一个简单的主页,其中有一个导航栏。当导航像这样改变时,我只是显示容器
SingleChildScrollView(
child: Container(
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage("images/sidebg.png"),
fit: BoxFit.cover,
),
),
child: Column(
children: [
pageIndex == 0 ? DashboardScreen() : Container(),
pageIndex == 1 ? MapScreen() : Container(),
pageIndex == 3 ? ServiceCenter() : Container(),
pageIndex == 4 ? ProfileScreen() : Container(),
],
)),
),
问题是您可以在索引更改时看到页面更改,但是当我更改页面时,它显示错误,正如我在上面提到的持续循环不停止。
如果我删除此进度指示器一切正常。
这是进度指示器屏幕
import 'dart:math' as math;

import 'package:curved_navigation_bar/curved_navigation_bar.dart';
import 'package:sleek_circular_slider/sleek_circular_slider.dart';
import 'package:liquid_progress_indicator/liquid_progress_indicator.dart';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_rating_bar/flutter_rating_bar.dart';
import 'package:smooth_star_rating/smooth_star_rating.dart';

class DashboardScreen extends StatefulWidget {
@override
_DashboardScreenState createState() => _DashboardScreenState();
}

class _DashboardScreenState extends State<DashboardScreen> {
@override
Widget build(BuildContext context) {
double width = MediaQuery.of(context).size.width;
double height = MediaQuery.of(context).size.height;

return Container(
child: Column(
children: [
SizedBox(
height: height * 0.05,
),
Circular_arc(),

],
),
);
}
}

final Gradient gradient = new LinearGradient(
colors: <Color>[
Colors.greenAccent.withOpacity(1.0),
Colors.yellowAccent.withOpacity(1.0),
Colors.redAccent.withOpacity(1.0),
],
stops: [
0.0,
0.5,
1.0,
],
);

class Circular_arc extends StatefulWidget {
const Circular_arc({
Key key,
}) : super(key: key);

@override
_Circular_arcState createState() => _Circular_arcState();
}

class _Circular_arcState extends State<Circular_arc>
with SingleTickerProviderStateMixin {
Animation<double> animation;
AnimationController animController;

@override
void initState() {
// TODO: implement initState
super.initState();

animController =
AnimationController(duration: Duration(seconds: 3), vsync: this);

final curvedAnimation =
CurvedAnimation(parent: animController, curve: Curves.easeInOutCubic);

animation = Tween<double>(begin: 0.0, end: 2).animate(curvedAnimation)
..addListener(() {
setState(() {});
});
animController.repeat(max: 1);
}

@override
Widget build(BuildContext context) {
double width = MediaQuery.of(context).size.width;
double height = MediaQuery.of(context).size.height;
return Container(
child: Stack(
children: [
CustomPaint(
size: Size(300, 300),
painter: ProgressArc(null, Colors.black54, true),
),

CustomPaint(
size: Size(300, 300),
painter: ProgressArc(animation.value, Colors.redAccent, false),
),
Positioned(
top: height * 0.07,
left: width * 0.2,
child: Column(
children: [
Image.asset(
'images/star-icon-fill@3x.png',
height: height * 0.045,
),
RichText(
text: new TextSpan(
// Note: Styles for TextSpans must be explicitly defined.
// Child text spans will inherit styles from parent
style: new TextStyle(
fontSize: 14.0,
color: Colors.black,
),
children: <TextSpan>[
new TextSpan(
text: '4.6',
style: new TextStyle(
fontSize: 40, fontFamily: 'UbuntuRegular')),
new TextSpan(
text: ' /5',
style: TextStyle(
fontSize: 25,
color: Colors.grey[400],
fontFamily: 'UbuntuRegular')),
],
),
),
Text(
'FIFTEEN DAYS SCORE',
style: TextStyle(
color: Colors.grey[400], fontFamily: 'UbuntuMedium'),
)
],
),
)
],
),
);
}
}

class ProgressArc extends CustomPainter {
bool isBackground;
double arc;
Color progressColor;

ProgressArc(this.arc, this.progressColor, this.isBackground);

@override
void paint(Canvas canvas, Size size) {
final rect = Rect.fromLTRB(0, 0, 300, 300);
final startAngle = -math.pi;
final sweepAngle = arc != null ? arc : math.pi;
final userCenter = false;
final paint = Paint()
..strokeCap = StrokeCap.round
..color = progressColor
..style = PaintingStyle.stroke
..strokeWidth = 10;

if (!isBackground) {
paint.shader = gradient.createShader(rect);
}
canvas.drawArc(rect, startAngle, sweepAngle, userCenter, paint);
}

@override
bool shouldRepaint(covariant CustomPainter oldDelegate) {
// TODO: implement shouldRepaint
return true;
}
}
错误在连续循环中,它不会停止。当我更改页面索引时它也开始了(意味着当我从主页更改导航时)。

最佳答案

在您的 _CircularArcState , 请...

  • 调用 animController.forward();animController.repeat(max: 1); 之后
  • 保存CircularArc的状态, 添加mixin AutomaticKeepAliveClientMixin_CircularArcState .然后覆盖 wantKeepAlive并返回 true .另外,请调用 super.build(context);里面 build(...) .
  • import 'dart:math' as math;
    import 'dart:ui';

    import 'package:flutter/material.dart';

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

    class MyApp extends StatelessWidget {
    @override
    Widget build(BuildContext context) {
    return MaterialApp(
    home: Scaffold(
    body: PageView(
    scrollDirection: Axis.vertical,
    children: [
    DashboardScreen(),
    Container(color: Colors.orange),
    Container(color: Colors.blue),
    Container(color: Colors.green),
    ],
    ),
    ),
    );
    }
    }

    class DashboardScreen extends StatelessWidget {
    @override
    Widget build(BuildContext context) {
    double width = MediaQuery.of(context).size.width;
    double height = MediaQuery.of(context).size.height;

    return Container(
    child: Column(
    children: [
    SizedBox(
    height: height * 0.05,
    ),
    CircularArc(),
    ],
    ),
    );
    }
    }

    final Gradient gradient = new LinearGradient(
    colors: <Color>[
    Colors.greenAccent.withOpacity(1.0),
    Colors.yellowAccent.withOpacity(1.0),
    Colors.redAccent.withOpacity(1.0),
    ],
    stops: [0.0, 0.5, 1.0],
    );

    class CircularArc extends StatefulWidget {
    const CircularArc({
    Key key,
    }) : super(key: key);

    @override
    _CircularArcState createState() => _CircularArcState();
    }

    class _CircularArcState extends State<CircularArc>
    with SingleTickerProviderStateMixin, AutomaticKeepAliveClientMixin {
    double width;
    double height;
    Animation<double> animation;

    @override
    void initState() {
    super.initState();

    final AnimationController animController =
    AnimationController(duration: Duration(seconds: 3), vsync: this);
    final curvedAnimation =
    CurvedAnimation(parent: animController, curve: Curves.easeInOutCubic);
    animation = Tween<double>(begin: 0.0, end: 2).animate(curvedAnimation)
    ..addListener(() {
    setState(() {});
    });
    animController.repeat(max: 1);
    animController.forward();
    }

    @override
    bool get wantKeepAlive => true;

    @override
    Widget build(BuildContext context) {
    super.build(context);

    if (width == null && height == null) {
    width = MediaQuery.of(context).size.width;
    height = MediaQuery.of(context).size.height;
    }

    return Container(
    child: Stack(
    children: [
    CustomPaint(
    size: Size(300, 300),
    painter: ProgressArc(null, Colors.black54, true),
    ),
    CustomPaint(
    size: Size(300, 300),
    painter: ProgressArc(animation.value, Colors.redAccent, false),
    ),
    Positioned(
    top: height * 0.07,
    left: width * 0.2,
    child: Column(
    children: [
    // Image.asset(
    // 'images/star-icon-fill@3x.png',
    // height: height * 0.045,
    // ),
    RichText(
    text: new TextSpan(
    // Note: Styles for TextSpans must be explicitly defined.
    // Child text spans will inherit styles from parent
    style: new TextStyle(
    fontSize: 14.0,
    color: Colors.black,
    ),
    children: <TextSpan>[
    new TextSpan(
    text: '4.6',
    style: new TextStyle(
    fontSize: 40, fontFamily: 'UbuntuRegular')),
    new TextSpan(
    text: ' /5',
    style: TextStyle(
    fontSize: 25,
    color: Colors.grey[400],
    fontFamily: 'UbuntuRegular')),
    ],
    ),
    ),
    Text(
    'FIFTEEN DAYS SCORE',
    style: TextStyle(
    color: Colors.grey[400], fontFamily: 'UbuntuMedium'),
    )
    ],
    ),
    )
    ],
    ),
    );
    }
    }

    class ProgressArc extends CustomPainter {
    bool isBackground;
    double arc;
    Color progressColor;

    ProgressArc(this.arc, this.progressColor, this.isBackground);

    @override
    void paint(Canvas canvas, Size size) {
    final rect = Rect.fromLTRB(0, 0, 300, 300);
    final startAngle = -math.pi;
    final sweepAngle = arc != null ? arc : math.pi;
    final userCenter = false;
    final paint = Paint()
    ..strokeCap = StrokeCap.round
    ..color = progressColor
    ..style = PaintingStyle.stroke
    ..strokeWidth = 10;

    if (!isBackground) {
    paint.shader = gradient.createShader(rect);
    }
    canvas.drawArc(rect, startAngle, sweepAngle, userCenter, paint);
    }

    @override
    bool shouldRepaint(covariant CustomPainter oldDelegate) {
    // TODO: implement shouldRepaint
    return true;
    }
    }

    关于 flutter _debugLifecycleState!= _ElementLifecycle.defunct' : is not true,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65657495/

    27 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com