我有一个浮动页面(用户),带有两个底部导航栏(用户信息和讨论)。我通过用户列表转到此页面,并将其传递给已选择的用户ID。然后该页面查询用户信息,然后将用户信息传递到用户信息页面,并将讨论传递到讨论页面。
class UserView extends StatefulWidget {
final String userUUID;
UserView({this.userUUID});
@override
State<StatefulWidget> createState() {
return _UserViewState();
}
}
class _UserViewState extends State<UserView> {
int _currentIndex = 0;
String _userUUID;
UserView _user;
List<Widget> _children;
@override
void initState() {
super.initState();
_userUUID = widget.userUUID;
_children = [
UserInfo(user: _user),
UserComments()
];
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(_user.userFirstName + " " + _user.userLastName),
),
body: new FutureBuilder(
future: _getUserInfo(http.Client(), _userUUID),
builder: (BuildContext context, AsyncSnapshot snapshot){
if(snapshot.hasError) print(snapshot.error);
return snapshot.hasData ?
UserInfo(user: snapshot.data):
Center(child: CircularProgressIndicator());
}
),
bottomNavigationBar: BottomNavigationBar(
currentIndex: _currentIndex, // this will be set when a new tab is tapped
items: [
BottomNavigationBarItem(
icon: new Icon(Icons.info),
title: new Text('Info'),
),
BottomNavigationBarItem(
icon: new Icon(Icons.chat),
title: new Text('Discussion'),
)
],
onTap: (int index) {
setState(() {
_currentIndex = index;
});
},
),
);
}
}
Future<User> _getUserInfo(http.Client client, String userUUID) async {
final SharedPreferences prefs = await SharedPreferences.getInstance();
Map<String, String> headersMap = {
'Authorization' : 'Bearer ' + prefs.getString("TOKEN")
};
final response = await http.get(
'https://myapi/user/view/' + userUUID,
headers: headersMap
);
if(response.statusCode == 200) {
return User.fromJson(json.decode(response.body));
} else {
throw Exception('Failed to load user data');
}
}
我正在使用FutureBuilder从API获取信息,并希望将User对象传递给User Info页面,并将Discussion列表传递给Discussion页面。
服务器的响应如下:
{
"uuid": "829998e8-6a48-4a50-a7e0-3581aeaaee87",
"first_name": "John",
"last_name": "Doe",
"age": 22,
"gender": "Male",
"phone": "",
"email": "",
"created_at": "2018-10-09T00:44:04.000Z",
"updated_at": "2018-10-09T00:44:04.000Z",
"comments": [
{
"uuid": "d2c046c8-efd4-4dfa-bcef-5ee021192a7e",
"user_uuid": "829998e8-6a48-4a50-a7e0-3581aeaaee87",
"comment": "this is comment",
"created_at": "2018-10-16T23:23:25.000Z",
"updated_at": "2018-10-16T23:23:25.000Z"
}
]
}
答案 0 :(得分:1)
您可以将FutureBuilder
提升到Scaffold
,以便您的AppBar
和BottomNavigationBar
可以访问_getUserInfo
的响应数据
@override
Widget build(BuildContext context) {
return new FutureBuilder(
future: _getUserInfo(_userUUID),
builder: (BuildContext context, AsyncSnapshot snapshot) {
if(snapshot.hasError) print(snapshot.error);
_user = snapshot.data ?? _user;
return Scaffold(
appBar: AppBar(
title: Text((_user != null) ? _user.userFirstName + " " + _user.userLastName : ""),
),
body: (_user != null) ? UserInfo(user: _user) : Center(child: CircularProgressIndicator()),
bottomNavigationBar: BottomNavigationBar(
currentIndex: _currentIndex, // this will be set when a new tab is tapped
items: [
BottomNavigationBarItem(
icon: new Icon(Icons.info),
title: new Text('Info'),
),
BottomNavigationBarItem(
icon: new Icon(Icons.chat),
title: new Text('Discussion'),
)
],
onTap: (int index) {
setState(() {
_currentIndex = index;
});
},
),
);
}
);
}