我有一个项目管理应用程序,在按下卡片上的“更多”图标按钮后,该应用程序会显示项目的详细信息。要显示的详细信息包括从数据库中提取的项目名称和截止日期,然后使用共享首选项存储在本地。“setString”和“getString”都运行良好,但在加载项目详细信息页面时,详细信息一开始并不显示。2只有在应用程序热重加载后,当页面处于活动状态时,才会显示。3下面是项目详细信息应用程序的代码:
import 'package:flutter/material.dart';
import 'package:mne/Actual%20Tasks/activity_widget.dart';
import 'package:mne/UserTasks/task_widget.dart';
import 'package:shared_preferences/shared_preferences.dart';
class ProjectTask extends StatefulWidget {
const ProjectTask({Key key}) : super(key: key);
@override
State<ProjectTask> createState() => _ProjectTaskState();
}
class _ProjectTaskState extends State<ProjectTask> {
String pname;
String pdesc;
String pdue;
@override
void initState() {
super.initState();
_fetchData();
}
Future<Null> _fetchData() async {
WidgetsFlutterBinding.ensureInitialized();
SharedPreferences localStorage = await SharedPreferences.getInstance();
pname = localStorage.getString('project_name');
pdesc = localStorage.getString('project_desc');
pdue = localStorage.getString('project_due');
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
iconTheme: const IconThemeData(color: Colors.black),
backgroundColor: Colors.white,
automaticallyImplyLeading: true,
centerTitle: true,
title: const Text('Project Details',
style: TextStyle(color: Colors.black))),
body: SingleChildScrollView(
child: Column(children: [
Container(height: 10, color: Colors.transparent),
// for image
Container(
width: 330,
child: Image.asset('assets/images/projectbanner.png'),
),
//for project name
Container(
padding: const EdgeInsets.only(bottom: 25, top: 15),
child: Row(children: [
Container(
padding: const EdgeInsets.only(left: 20, right: 145),
child: Text(pname ?? '',
style: const TextStyle(
color: Colors.black,
fontWeight: FontWeight.bold,
fontSize: 16)),
),
Container(
padding: const EdgeInsets.only(right: 10, top: 8),
child: const Icon(Icons.calendar_month_outlined)),
RichText(
text: TextSpan(children: [
const TextSpan(
text: 'Due: ',
style: TextStyle(
fontSize: 12,
fontWeight: FontWeight.bold,
color: Colors.black)),
TextSpan(
text: pdue ?? '',
style: const TextStyle(fontSize: 12, color: Colors.black))
])),
])),
// for description title
Container(
alignment: Alignment.centerLeft,
padding: const EdgeInsets.only(left: 20, bottom: 20),
child: const Text('Description',
style: TextStyle(fontWeight: FontWeight.bold, fontSize: 16))),
// for actual desc
Container(
padding: const EdgeInsets.only(left: 20),
alignment: Alignment.centerLeft,
child: Text(
pdesc ?? '',
style: const TextStyle(color: Colors.grey),
)),
// for task title
Container(
padding: const EdgeInsets.only(left: 20, top: 20, bottom: 20),
alignment: Alignment.centerLeft,
child: const Text('Tasks',
style: TextStyle(fontWeight: FontWeight.bold, fontSize: 16))),
// for task widget
Container(height: 630, child: const ActivityWidget()),
]),
),
);
}
}
此图像显示了第一次加载时的外观:
这是热重新加载后的外观:
我怎样才能使它马上显示信息呢?任何帮助都是感激的。
2条答案
按热度按时间ozxc1zmp1#
将fetchData函数更改为:
ftf50wuq2#
在主.dart文件中执行以下操作: