flutter - 如何将字符串列表发送到 TabBar 页面颤动
问题描述
我在json
主页上提出了请求。它返回一个值列表。我想将这些值发送到我的tabBar
页面之一。我不知道该怎么做伙计们。
TabOptions.dart
class Choice {
final String title;
final IconData icon;
const Choice({this.title, this.icon});
}
const List<Choice> choices = <Choice>[
Choice(title: 'Conversation', icon: Icons.comment),
Choice(title: 'Schedule', icon: Icons.schedule),
Choice(title: 'Requests', icon: Icons.subdirectory_arrow_left),];
class ChoicePage extends StatelessWidget {
const ChoicePage({Key key, this.choice}) : super(key: key);
final Choice choice;
@override
Widget build(BuildContext context) {
final TextStyle textStyle = Theme.of(context).textTheme.display1;
return Card(
color: Colors.white,
child: Center(
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Icon(
choice.icon,
size: 150.0,
color: textStyle.color,
),
Text(
choice.title,
style: textStyle,
),
],
),
),
);
}
}
主屏幕
class DashBoardPage extends StatefulWidget {
@override
_DashBoardPageState createState() => _DashBoardPageState();
}
class _DashBoardPageState extends State<DashBoardPage> {
List<MentorIndex> mentorIndexes = [];
SharedPreferences sharedPreferences;
Iterable newList;
Widget callPage(int currentIndex) {
switch (currentIndex) {
case 0:
showTabs = true;
_appBarText = "Welcome, " + _firstName;
return TabBarView(
children: [new HomePage(), new SchedulePage(), RequestsPage()]);
break;
}
@override
void initState() {
super.initState();
fetchIndex();
}
@override
Widget build(BuildContext context) {
return MaterialApp(
title: '',
debugShowCheckedModeBanner: false,
home: DefaultTabController(
length: choices.length,
child: Scaffold(
appBar: AppBar(
backgroundColor: Color(0xFFFFFFFF),
title: Text(
_appBarText,
bottom: showTabs
? TabBar(
isScrollable: true,
tabs: choices.map<Widget>((Choice choice) {
return Tab(
text: choice.title,
icon: Icon(choice.icon),
);
}).toList(),
labelColor: Color(0xFF1C2447),
)
: null,
actions: <Widget>[
],
), //AppBar
},
items: [
],
),
),
),
);
}
Future fetchIndex() async {
SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
var uri = NetworkUtils.host + AuthUtils.endPointIndex;
try {
final response = await http.get(
uri,
headers: {'Accept': 'application/json', 'Content-Type': 'application/json','Authorization': 'Bearer ' + sharedPreferences.get("token"), },
);
final responseJson = json.decode(response.body);
for (var u in responseJson["data"]) {
MentorIndex user = MentorIndex(
u["id"],
u["mentor_id"],
u["mentee_id"],
u["status"],
u["session_count"],
u["current_job"],
u["email"],
u["phone_call"],
u["video_call"],
u["face_to_face"],
u["created_at"],
u["updated_at"]);
mentorIndexes.add(user);
newList = mentorIndexes.map((MentorIndex) => MentorIndex.mentee_id);
//here i want to make this newList data available to my tabBar pages
}
return responseJson;
} catch (exception) {
print(exception);
}
}
}
解决方案
在. TabBarView
_Scaffold
body
如果只有一页需要这些值,则调用 page 中的值:
Scaffold(
body: TabBarView(
children: [HomePage(), SchedulePage(), RequestsPage()],
)
)
其他用途FutureBuilder
:
body: FutureBuilder(
future:fetchIndex(),
builder: (context, value) => TabBarView(
children: [HomePage(newList), SchedulePage(newList), RequestsPage(newList)],
),
推荐阅读
- python - 等价于 Python sklearn 中的 Matlab 的 PCA
- php - React Native 选择单个 json_encode 字符串而不是数组
- python - 检查一个数据框中的名称是否存在于另一个数据框中
- lucene - 对子类的全文搜索返回 java.lang.illegalArgumentException:给定类的全文索引过多:客户
- python - 使用 pymongo 获取带有排序和限制的请求结果
- microsoft-graph-api - Microsoft Graph:在驱动器上搜索 DriveItems 会返回所有 DriveItems
- c# - 即使对于没有帖子的标签,也可以按标签计算帖子
- excel - 保存文件时忽略斜线
- php - Vagrant 虚拟主机/共享文件夹无法访问
- java - 将 Jackson 注释添加到招摇生成的类中