[英]I am having trouble loadding CatDetailHeader
I hope all is well.我希望一切都好。 I have been following a video tutorial and I am having trouble with loading this second screen.
我一直在关注视频教程,但在加载第二个屏幕时遇到了问题。 I am able to see the screen with the list of cats, but I can not load the detail page.
我可以看到带有猫列表的屏幕,但无法加载详细信息页面。 I have copied the example code into my project and that did not work.
我已将示例代码复制到我的项目中,但没有奏效。 I even tried loading the example project and that did not work either.
我什至尝试加载示例项目,但也没有用。 I need a second set of eyes on this situation.
我需要重新审视这种情况。
════════ Exception caught by widgets library ═══════════════════════════════════════════════════════
The following assertion was thrown building CatDetailHeader(dirty, dependencies: [_LocalizationsScope-[GlobalKey#5f942], _InheritedTheme], state: _CatDetailHeaderState#2ac20):
'package:flutter/src/widgets/heroes.dart': Failed assertion: line 164 pos 15: 'tag != null': is not true.
The relevant error-causing widget was:
CatDetailHeader file:///C:/Users/13363/AndroidStudioProjects/catbox/lib/ui/cat_details/details_page.dart:40:19
When the exception was thrown, this was the stack:
#2 new Hero (package:flutter/src/widgets/heroes.dart:164:15)
#3 _CatDetailHeaderState.build (package:catbox/ui/cat_details/header/details_header.dart:24:22)
#4 StatefulElement.build (package:flutter/src/widgets/framework.dart:4334:27)
#5 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4223:15)
#6 Element.rebuild (package:flutter/src/widgets/framework.dart:3947:5)
...
════════════════════════════════════════════════════════════════════════════════════════════════
//Code from details_page.dart
import 'package:catbox/ui/cat_details/header/details_header.dart';
import 'package:catbox/models/cat.dart';
import 'package:flutter/material.dart';
import 'package:meta/meta.dart';
class CatDetailsPage extends StatefulWidget {
final Cat cat;
final Object avatarTag;
CatDetailsPage(
this.cat, {
@required this.avatarTag,
});
@override
_CatDetailsPageState createState() => new _CatDetailsPageState();
}
class _CatDetailsPageState extends State<CatDetailsPage> {
@override
Widget build(BuildContext context) {
var linearGradient = new BoxDecoration(
gradient: new LinearGradient(
begin: FractionalOffset.centerRight,
end: FractionalOffset.bottomLeft,
colors: [
Colors.blue,
Colors.blue,
],
),
);
return new Scaffold(
body: new SingleChildScrollView(
child: new Container(
decoration: linearGradient,
child: new Column(
crossAxisAlignment: CrossAxisAlignment.start,
children:[
new CatDetailHeader(
widget.cat,
avatarTag: widget.avatarTag,
),
//TODO Details Body
//TODO Cat Showcase
],
),
),
),
);
}
}
//Code from detail_header.dart
import 'package:catbox/models/cat.dart';
import 'package:flutter/material.dart';
import 'package:meta/meta.dart';
class CatDetailHeader extends StatefulWidget {
final Cat cat;
final Object avatarTag;
CatDetailHeader(
this.cat, {
@required this.avatarTag,
});
@override
_CatDetailHeaderState createState() => new _CatDetailHeaderState();
}
class _CatDetailHeaderState extends State<CatDetailHeader> {
@override
Widget build(BuildContext context) {
var theme = Theme.of(context);
var textTheme = theme.textTheme;
var avatar = new Hero(
tag: widget.avatarTag,
child: new CircleAvatar(
backgroundImage: new NetworkImage(widget.cat.avatarUrl),
radius: 75.0,
),
);
var likeInfo = new Padding(
padding: const EdgeInsets.only(top: 16.0),
child: new Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
new Icon(
Icons.thumb_up,
color: Colors.white,
size: 16.0,
),
new Padding(
padding: const EdgeInsets.only(left: 8.0),
child: new Text(
widget.cat.likeCounter.toString(),
style: textTheme.subhead.copyWith(color: Colors.white),
)
)
],
),
);
var actionButtons = new Padding(
padding: const EdgeInsets.only(
top: 16.0,
left: 16.0,
right: 16.0,
),
child: new Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
new ClipRRect(
borderRadius: new BorderRadius.circular(30.0),
child: new MaterialButton(
minWidth: 140.0,
color: theme.accentColor,
textColor: Colors.white,
onPressed: () async {
//TODO Handle Adopt
},
child: new Text('ADOPT ME'),
),
),
new ClipRRect(
borderRadius: new BorderRadius.circular(30.0),
child: new RaisedButton(
color: Colors.lightGreen,
disabledColor: Colors.grey,
textColor: Colors.white,
onPressed: () async {
//TODO Handle Like
},
child: new Text('LIKE'),
),
),
],
),
);
return new Stack(
children: [
//TODO Background Image
new Align(
alignment: FractionalOffset.bottomCenter,
heightFactor: 1.4,
child: new Column(
children: [
avatar,
likeInfo,
actionButtons,
],
),
),
new Positioned(
top: 26.0,
left: 4.0,
child: new BackButton(color: Colors.white),
),
],
);
}
}
You can copy paste run full code below您可以在下面复制粘贴运行完整代码
The error message means Hero tag is incorrect错误信息表示英雄标签不正确
In your code, you need to provide a value to avatarTag
when call CatDetailsPage
在您的代码中,您需要在调用
CatDetailsPage
时为avatarTag
提供一个值
In ListView
you can use index
as part of tag name like below so it is unique在
ListView
您可以使用index
作为标签名称的一部分,如下所示,因此它是唯一的
CatDetailsPage(cat, avatarTag: "tag${index}")
code snippet of demo code演示代码的代码片段
Expanded(flex: 1,child: CatDetailsPage(cat, avatarTag: "tag")),
working demo工作演示
full code完整代码
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
Cat cat = CatImplementation();
void _incrementCounter() {
setState(() {
_counter++;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Expanded(flex: 1,child: CatDetailsPage(cat, avatarTag: "tag")),
Text(
'You have pushed the button this many times:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.display1,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: Icon(Icons.add),
),
);
}
}
class CatDetailsPage extends StatefulWidget {
final Cat cat;
final Object avatarTag;
CatDetailsPage(
this.cat, {
@required this.avatarTag,
});
@override
_CatDetailsPageState createState() => new _CatDetailsPageState();
}
class _CatDetailsPageState extends State<CatDetailsPage> {
@override
Widget build(BuildContext context) {
var linearGradient = new BoxDecoration(
gradient: new LinearGradient(
begin: FractionalOffset.centerRight,
end: FractionalOffset.bottomLeft,
colors: [
Colors.blue,
Colors.blue,
],
),
);
return new Scaffold(
body: new SingleChildScrollView(
child: new Container(
decoration: linearGradient,
child: new Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
new CatDetailHeader(
widget.cat,
avatarTag: widget.avatarTag,
),
//TODO Details Body
//TODO Cat Showcase
],
),
),
),
);
}
}
class CatDetailHeader extends StatefulWidget {
final Cat cat;
final Object avatarTag;
CatDetailHeader(
this.cat, {
@required this.avatarTag,
});
@override
_CatDetailHeaderState createState() => new _CatDetailHeaderState();
}
class _CatDetailHeaderState extends State<CatDetailHeader> {
@override
Widget build(BuildContext context) {
var theme = Theme.of(context);
var textTheme = theme.textTheme;
var avatar = new Hero(
tag: widget.avatarTag,
child: new CircleAvatar(
backgroundImage: new NetworkImage(widget.cat.avatarUrl),
radius: 75.0,
),
);
var likeInfo = new Padding(
padding: const EdgeInsets.only(top: 16.0),
child: new Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
new Icon(
Icons.thumb_up,
color: Colors.white,
size: 16.0,
),
new Padding(
padding: const EdgeInsets.only(left: 8.0),
child: new Text(
widget.cat.likeCounter.toString(),
style: textTheme.subhead.copyWith(color: Colors.white),
))
],
),
);
var actionButtons = new Padding(
padding: const EdgeInsets.only(
top: 16.0,
left: 16.0,
right: 16.0,
),
child: new Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
new ClipRRect(
borderRadius: new BorderRadius.circular(30.0),
child: new MaterialButton(
minWidth: 140.0,
color: theme.accentColor,
textColor: Colors.white,
onPressed: () async {
//TODO Handle Adopt
},
child: new Text('ADOPT ME'),
),
),
new ClipRRect(
borderRadius: new BorderRadius.circular(30.0),
child: new RaisedButton(
color: Colors.lightGreen,
disabledColor: Colors.grey,
textColor: Colors.white,
onPressed: () async {
//TODO Handle Like
},
child: new Text('LIKE'),
),
),
],
),
);
return new Stack(
children: [
//TODO Background Image
new Align(
alignment: FractionalOffset.bottomCenter,
heightFactor: 1.4,
child: new Column(
children: [
avatar,
likeInfo,
actionButtons,
],
),
),
new Positioned(
top: 26.0,
left: 4.0,
child: new BackButton(color: Colors.white),
),
],
);
}
}
abstract class Cat {
String get avatarUrl;
int get likeCounter;
}
class CatImplementation extends Cat {
String avatarUrl = "https://i.pravatar.cc/150?img=1";
int likeCounter = 0;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.