我想使用一个显示小部件的自定义小部件,它在我的一部手机Redmi Note 7s上工作正常,但是当我在Redmi Note 3上对其进行测试时,会引发错误。
我的代码是
import 'package:flutter/material.dart';
import 'package:cpblog/detailsPage.dart';
import 'package:http/http.dart' as http;
import 'dart:convert';
import 'package:cpblog/data.dart';
import 'package:cpblog/ShowUp.dart';
import 'package:marquee/marquee.dart';
import 'package:auto_size_text/auto_size_text.dart';
import 'package:fancy_drawer/fancy_drawer.dart';
class MyHomePage extends StatefulWidget {
final String category;
final String address;
MyHomePage({this.address,this.category});
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> with TickerProviderStateMixin{
List<Posts> posts = List();
List<Posts> ds = List();
bool isLoaded = false;
FancyDrawerController _controller;
int delayAmount = 500;
Future<void> _fetchData() async {
try {
final response = await http.get(widget.address);
if(response.statusCode==200){
final data = json.decode(response.body);
posts = (data['articles'] as List).map((data){
return Posts.fromJSON(data);
}).toList();
setState(() {
this.isLoaded = true;
});
}
}catch(e){
print(e);
}
}
void initState() {
_fetchData();
super.initState();
_controller = FancyDrawerController(
vsync: this, duration: Duration(milliseconds: 250))
..addListener(() {
setState(() {}); // Must call setState
});
}
@override
void dispose() {
_controller.dispose(); // Dispose controller
super.dispose();
}
@override
Widget build(BuildContext context) {
final double width = MediaQuery.of(context).size.width;
final double height = MediaQuery.of(context).size.height;
return Material(
child: FancyDrawerWrapper(
backgroundColor: Colors.white, // Drawer background
controller: _controller,
drawerItems: <Widget>[ListTile(
title: Text('Algorithms',style: TextStyle(
fontFamily: 'Montserrat',
color: Colors.black,
fontSize: width*0.063)),
onTap: () {
Navigator.of(context).pushReplacement(MaterialPageRoute(
builder: (context) => MyHomePage(address:'*********',category: 'Algorithms',)));
},
),
ListTile(
title: Text('Data Structures',style: TextStyle(
fontFamily: 'Montserrat',
color: Colors.black,
fontSize: width*0.063)),
onTap: () {
Navigator.of(context).pushReplacement(MaterialPageRoute(
builder: (context) => MyHomePage(address:'******************',category: 'Data Structures',)));
},
),],
child: Scaffold(
appBar: AppBar(elevation: 0.0,backgroundColor:Color(0xFF21BFBD),
leading: IconButton(
icon: Icon(
Icons.menu,
color: Colors.white,
size: height*0.043,
),
onPressed: () {
_controller.toggle();
},
),),
backgroundColor: Color(0xFF21BFBD),
body: ListView(
children: <Widget>[
Padding(
padding: EdgeInsets.only(top: MediaQuery.of(context).size.height*0.0037, left: MediaQuery.of(context).size.width*0.49),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
],
),
),
Padding(
padding: EdgeInsets.only(left: MediaQuery.of(context).size.width*0.1017),
child: Row(
children: <Widget>[
Padding(
padding: EdgeInsets.only(left:MediaQuery.of(context).size.width*0.337,),
child: ShowUp(
child: Text(
'Blogs',
style: TextStyle(
fontFamily: 'Montserrat',
color: Colors.white,
fontWeight: FontWeight.bold,
fontSize: width*0.059)),
delay: delayAmount,
),
),
SizedBox(width: MediaQuery.of(context).size.width*0.0130),
Expanded(
child: Padding(
padding: const EdgeInsets.all(8.0),
child: ShowUp(
child: Text('For You',
style: TextStyle(
fontFamily: 'Montserrat',
color: Colors.white,
fontSize: width*0.079)),
delay: delayAmount+200,
),
),
),
],
),
),
SizedBox(height: MediaQuery.of(context).size.height*0.041),
Row(
children: [
widget.category=='Algorithms'?SizedBox(width: MediaQuery.of(context).size.width*0.502,):SizedBox(width: MediaQuery.of(context).size.width*0.421,),
Expanded(
child: ShowUp(
child: AutoSizeText(widget.category,
maxLines: 1,
style: TextStyle(
fontFamily: 'Montserrat',
color: Colors.white,
fontSize: width*0.076)),
delay: delayAmount+=100,
),
),
],
),
SizedBox(height: MediaQuery.of(context).size.height*0.0124),
Expanded(
child: ShowUp(
child: Container(
height: MediaQuery.of(context).size.height*0.781,
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.only(topRight: Radius.circular(75.0),topLeft: Radius.circular(75.0)),
),
child: ListView(
primary: false,
padding: EdgeInsets.only(left: MediaQuery.of(context).size.width*0.0636, right: MediaQuery.of(context).size.width*0.050),
children: <Widget>[
Padding(
padding: EdgeInsets.only(top: MediaQuery.of(context).size.height*0.055),
child: RefreshIndicator(
child:isLoaded?Container(
height: MediaQuery.of(context).size.height*0.625,
child: ListView.builder(
shrinkWrap:true,
itemCount:posts.length,
itemBuilder: (context,index)
{return ShowUp(
child: _buildFoodItem(posts[index].image,posts[index].title,posts[index].content,posts[index].text,
posts[index].video1,posts[index].video2,posts[index].video3,posts[index].video4),delay: delayAmount+60,
);})):Center(
child: CircularProgressIndicator()
),onRefresh: _fetchData,)),
],
),
),
),
)
],
),
),
),
);
}
Widget _buildFoodItem(String imgPath, String foodName,String url,String text,String video1,String video2,String video3,String video4) {
return Padding(
padding: EdgeInsets.only(left: MediaQuery.of(context).size.width-372.0, right: MediaQuery.of(context).size.width-372.0, top: MediaQuery.of(context).size.height-793.0),
child: InkWell(
onTap: () {
print(MediaQuery.of(context).size.height);
print(MediaQuery.of(context).size.width);
Navigator.of(context).push(MaterialPageRoute(
builder: (context) => DetailsPage(heroTag: imgPath, foodName: foodName,url: url,text: text,video1: video1,video2: video2,video3: video3,video4: video4)));
},
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Container(
child: Row(
children: [
Hero(
tag: imgPath,
child: Container(
decoration: BoxDecoration(
shape: BoxShape.circle,
image: DecorationImage(
image: NetworkImage(imgPath),
fit: BoxFit.cover)),
height: MediaQuery.of(context).size.height-733.0,
width: MediaQuery.of(context).size.width-312.0)
),
SizedBox(width: MediaQuery.of(context).size.width*0.025),
Container(
height: MediaQuery.of(context).size.height*0.0186,
constraints: BoxConstraints(maxWidth: MediaQuery.of(context).size.width * 0.42),
child: AutoSizeText(
foodName,
maxLines: 1,
overflowReplacement: Marquee(
text: foodName,
crossAxisAlignment: CrossAxisAlignment.start,
scrollAxis: Axis.horizontal,
blankSpace: 20.0,
velocity: 30.0,
),
),
),
]
)
),
IconButton(
icon: Icon(Icons.arrow_forward_ios),
color: Colors.black,
onPressed: () {Navigator.of(context).push(MaterialPageRoute(
builder: (context) => DetailsPage(heroTag: imgPath, foodName: foodName,url: url,text: text,video1: video1,video2: video2,video3: video3,video4: video4)));}
)
],
)
));
}
}
我的自定义小部件代码为
import 'dart:async';
import 'package:flutter/material.dart';
class ShowUp extends StatefulWidget {
final Widget child;
final int delay;
ShowUp({@required this.child, this.delay});
@override
_ShowUpState createState() => _ShowUpState();
}
class _ShowUpState extends State<ShowUp> with TickerProviderStateMixin {
AnimationController _animController;
Animation<Offset> _animOffset;
@override
void initState() {
super.initState();
_animController =
AnimationController(vsync: this, duration: Duration(milliseconds: 500));
final curve =
CurvedAnimation(curve: Curves.decelerate, parent: _animController);
_animOffset =
Tween<Offset>(begin: const Offset(0.0, 0.35), end: Offset.zero)
.animate(curve);
if (widget.delay == null) {
_animController.forward();
} else {
Timer(Duration(milliseconds: widget.delay), () {
_animController.forward();
});
}
}
@override
void dispose() {
super.dispose();
_animController.dispose();
}
@override
Widget build(BuildContext context) {
return FadeTransition(
child: SlideTransition(
position: _animOffset,
child: widget.child,
),
opacity: _animController,
);
}
}
和异常或引发的错误
Reload already in progress, ignoring request
Restarted application in 4,106ms.
[38;5;248m════════ Exception caught by widgets library ═══════════════════════════════════[39;49m
[38;5;244mThe following assertion was thrown building SlideTransition(animation: AnimationController#c57cb(⏮ 0.000; paused)➩_DecelerateCurve➩Tween<Offset>(Offset(0.0, 0.3) → Offset(0.0, 0.0))➩Offset(0.0, 0.3), state: _AnimatedState#5cc4a):[39;49m
'package:flutter/src/rendering/shifted_box.dart': Failed assertion: line 107 pos 15: 'padding.isNonNegative': is not true.
[38;5;248mEither the assertion indicates an error in the framework itself, or we should provide substantially more information in this error message to help you determine and fix the underlying cause.
In either case, please report this assertion by filing a bug on GitHub:
https://github.com/flutter/flutter/issues/new?template=BUG.md
[39;49m
[38;5;244mThe relevant error-causing widget was[39;49m
[38;5;248mSlideTransition[39;49m
[38;5;244mWhen the exception was thrown, this was the stack[39;49m
[38;5;244m#2 new RenderPadding[39;49m
[38;5;244m#3 Padding.createRenderObject[39;49m
[38;5;244m#4 RenderObjectElement.mount[39;49m
[38;5;244m#5 SingleChildRenderObjectElement.mount[39;49m
[38;5;244m... Normal element mounting (55 frames)[39;49m
[38;5;244m...[39;49m
[38;5;248m════════════════════════════════════════════════════════════════════════════════[39;49m
[38;5;248m════════ Exception caught by widgets library ═══════════════════════════════════[39;49m
'package:flutter/src/rendering/shifted_box.dart': Failed assertion: line 107 pos 15: 'padding.isNonNegative': is not true.
[38;5;244mThe relevant error-causing widget was[39;49m
[38;5;248mSlideTransition[39;49m
[38;5;248m════════════════════════════════════════════════════════════════════════════════[39;49m
[38;5;248m════════ Exception caught by widgets library ═══════════════════════════════════[39;49m
'package:flutter/src/rendering/shifted_box.dart': Failed assertion: line 107 pos 15: 'padding.isNonNegative': is not true.
[38;5;244mThe relevant error-causing widget was[39;49m
[38;5;248mSlideTransition[39;49m
[38;5;248m════════════════════════════════════════════════════════════════════════════════[39;49m
[38;5;248m════════ Exception caught by widgets library ═══════════════════════════════════[39;49m
'package:flutter/src/rendering/shifted_box.dart': Failed assertion: line 107 pos 15: 'padding.isNonNegative': is not true.
[38;5;244mThe relevant error-causing widget was[39;49m
[38;5;248mSlideTransition[39;49m
[38;5;248m════════════════════════════════════════════════════════════════════════════════[39;49m
[38;5;248m════════ Exception caught by widgets library ═══════════════════════════════════[39;49m
'package:flutter/src/rendering/shifted_box.dart': Failed assertion: line 107 pos 15: 'padding.isNonNegative': is not true.
[38;5;244mThe relevant error-causing widget was[39;49m
[38;5;248mSlideTransition[39;49m
[38;5;248m════════════════════════════════════════════════════════════════════════════════[39;49m
[38;5;248m════════ Exception caught by widgets library ═══════════════════════════════════[39;49m
'package:flutter/src/rendering/shifted_box.dart': Failed assertion: line 107 pos 15: 'padding.isNonNegative': is not true.
[38;5;244mThe relevant error-causing widget was[39;49m
[38;5;248mSlideTransition[39;49m
[38;5;248m════════════════════════════════════════════════════════════════════════════════[39;49m
[38;5;248m════════ Exception caught by widgets library ═══════════════════════════════════[39;49m
'package:flutter/src/rendering/shifted_box.dart': Failed assertion: line 107 pos 15: 'padding.isNonNegative': is not true.
[38;5;244mThe relevant error-causing widget was[39;49m
[38;5;248mSlideTransition[39;49m
[38;5;248m════════════════════════════════════════════════════════════════════════════════[39;49m
[38;5;248m════════ Exception caught by widgets library ═══════════════════════════════════[39;49m
'package:flutter/src/rendering/shifted_box.dart': Failed assertion: line 107 pos 15: 'padding.isNonNegative': is not true.
[38;5;244mThe relevant error-causing widget was[39;49m
[38;5;248mSlideTransition[39;49m
[38;5;248m════════════════════════════════════════════════════════════════════════════════[39;49m
[38;5;248m════════ Exception caught by widgets library ═══════════════════════════════════[39;49m
'package:flutter/src/rendering/shifted_box.dart': Failed assertion: line 107 pos 15: 'padding.isNonNegative': is not true.
[38;5;244mThe relevant error-causing widget was[39;49m
[38;5;248mSlideTransition[39;49m
[38;5;248m════════════════════════════════════════════════════════════════════════════════[39;49m
[38;5;248m════════ Exception caught by widgets library ═══════════════════════════════════[39;49m
'package:flutter/src/rendering/shifted_box.dart': Failed assertion: line 107 pos 15: 'padding.isNonNegative': is not true.
[38;5;244mThe relevant error-causing widget was[39;49m
[38;5;248mSlideTransition[39;49m
[38;5;248m════════════════════════════════════════════════════════════════════════════════[39;49m
[38;5;248m════════ Exception caught by widgets library ═══════════════════════════════════[39;49m
'package:flutter/src/rendering/shifted_box.dart': Failed assertion: line 107 pos 15: 'padding.isNonNegative': is not true.
[38;5;244mThe relevant error-causing widget was[39;49m
[38;5;248mSlideTransition[39;49m
[38;5;248m════════════════════════════════════════════════════════════════════════════════[39;49m
[38;5;248m════════ Exception caught by widgets library ═══════════════════════════════════[39;49m
'package:flutter/src/rendering/shifted_box.dart': Failed assertion: line 107 pos 15: 'padding.isNonNegative': is not true.
[38;5;244mThe relevant error-causing widget was[39;49m
[38;5;248mSlideTransition[39;49m
[38;5;248m════════════════════════════════════════════════════════════════════════════════[39;49m
[38;5;248m════════ Exception caught by widgets library ═══════════════════════════════════[39;49m
'package:flutter/src/rendering/shifted_box.dart': Failed assertion: line 107 pos 15: 'padding.isNonNegative': is not true.
[38;5;244mThe relevant error-causing widget was[39;49m
[38;5;248mSlideTransition[39;49m
[38;5;248m════════════════════════════════════════════════════════════════════════════════[39;49m
问题出在我的自定义窗口小部件中,但它在某些设备上运行但在某些设备上失败,如何克服此错误,如果无法解决此错误,是否还有其他解决方法可以得到与自定义窗口小部件相同的结果? 预先谢谢你。