Shr*_*thi 5 state dart flutter flutter-layout scoped-model
我正在尝试在我的 flutter 项目中创建一个范围模型,但我似乎无法弄清楚为什么会出现错误。这个作用域模型实现有什么问题?我有一个带有底部导航器的主页。在个人资料选项卡中,我在树深处的小部件中获取了我需要的关注者列表,因此我尝试使用scopedmodel。
型号代码是
class RelationshipsModel extends Model {
List<Relationship> relations;
RelationshipsModel(this.relations);
void add(String name) {
relations.add(new Relationship("", "", name, name));
notifyListeners();
}
}
Run Code Online (Sandbox Code Playgroud)
创建作用域模型的配置文件页面如下。在这里,我从该州的 Firebase 获取关注者列表,并使用它来创建作用域模型的模型。
class ProfileWidget extends StatefulWidget {
@override
_ProfileWidgetState createState() => _ProfileWidgetState();
}
class _ProfileWidgetState extends State<ProfileWidget> {
@override
initState() {
super.initState();
getCurrentUserDetails();
}
getCurrentUserDetails() async {
_name = await CacheService.getCurrentUser();
var following = await service.getFollowingList(_name);
setState(() {
this._following = following;
});
}
@override
Widget build(BuildContext context) {
if (this._name == null) {
return new Container(
child: const CupertinoActivityIndicator(),
);
}
return ScopedModel<RelationshipsModel>(
model: RelationshipsModel(this._following),
child: Scaffold(
//more stuff
background: new Stack(children: <Widget>[
new CarouselWidget(),
new Align(
alignment: FractionalOffset.topCenter,
heightFactor: 6.0,
child: new Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
//Here I am creating the a widget that shows the list of followers and following
new FollowerInfoWidget(followers: this._followers,
following: this._following),
],
),
),
])),
)
);
}
}
Run Code Online (Sandbox Code Playgroud)
下面是 FollowerInfoWidget,它根据列表中单击的用户调用 ProfileWidget 或 UserWidget
class _FollowerState extends State<FollowerWidget> {
Widget buildListTile(BuildContext context, Relationship item) {
return new MergeSemantics(
child: new ListTile(
title: new Text(item.followerId),
onTap: () async {
if (item.followerId == await CacheService.getCurrentUser()) {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => ProfileWidget()),
);
}
else {
Navigator.push(
context,
MaterialPageRoute(builder: (context) =>
UserWidget(name: item.followerId)),
);
}
},
trailing: new Icon(Icons.info, color: Theme.of(context).disabledColor),
),
);
}
Run Code Online (Sandbox Code Playgroud)
FollowUnFollowWidget 按钮是 UserWidget 的一部分
class UserWidget extends StatefulWidget {
UserWidget(
{Key key})
: super(key: key);
@override
_UserWidgetState createState() => _UserWidgetState();
}
class _UserWidgetState extends State<UserWidget> {
@override
Widget build(BuildContext context) {
return Scaffold(
body: DefaultTabController(
//more stuff
new FollowerInfoWidget(
followers: this._followers,
following: this._following,
),
new FollowUnFollowWidget ()
Run Code Online (Sandbox Code Playgroud)
具有 ScopedModelDescendant 的小部件如下
class FollowUnFollowWidget extends StatelessWidget {
@override
Widget build(BuildContext context) {
return ScopedModelDescendant<RelationshipsModel>(
builder: (context, child, model) =>
FloatingActionButton(
onPressed: () {}
//more stuff
)
);
}
}
Run Code Online (Sandbox Code Playgroud)
这里的代码将推送一个视图,它是 aProfileWidget或 aUserWidget
onTap: () async {
if (item.followerId == await CacheService.getCurrentUser()) {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => ProfileWidget()),
);
}
else {
Navigator.push(
context,
MaterialPageRoute(builder: (context) =>
UserWidget(name: item.followerId)),
);
}
},
Run Code Online (Sandbox Code Playgroud)
由于没有上面的内容,所以使用UserWidget是行不通的。这仅由以下定义FollowUnFollowWidgetScopedModelDescendantScopedModelScopedModelProfileWidget
看起来您需要将其添加ScopedModel到构建方法的顶部UserWidget或作为其一部分buildListTile
| 归档时间: |
|
| 查看次数: |
5394 次 |
| 最近记录: |