【发布时间】:2020-11-10 08:24:41
【问题描述】:
好的,我已经搜索了很长时间,但无济于事。这个问题似乎是构建缓存的问题,但我找不到有关清除构建缓存的资源,也找不到有关如何解决此特定问题的任何资源。
我的代码如下:
part of finance_app;
class InvestmentDetail extends StatefulWidget {
final String userEmail;
final String userName;
final String profilePic;
final String providerID;
final String uid;
InvestmentDetail(
{@required this.userEmail,
@required this.userName,
@required this.profilePic,
@required this.providerID,
@required this.uid});
@override
_InvestmentDetailState createState() => _InvestmentDetailState();
}
final FirebaseAuth _auth = FirebaseAuth.instance;
class _InvestmentDetailState extends State<InvestmentDetail>
with SingleTickerProviderStateMixin {
bool firstRun = true;
bool showMoney = true;
bool readyToShowChart = false;
var refreshkey = GlobalKey<RefreshIndicatorState>();
LineChart chart;
bool isTapped = false;
Future<String> displayName() async {
FirebaseUser _user = await FirebaseAuth.instance.currentUser();
return _user.displayName;
}
// various other functions go here
void createLine2() {
print("working");
Firestore.instance
.collection("user/" + widget.userEmail + "/investmentHistory")
.snapshots()
.listen(
(data) => {
print(data),
line1 = {},
data.documents.forEach(
(doc) => {
print(doc["percentage"]),
line1[DateTime.fromMillisecondsSinceEpoch(
int.parse(doc.documentID).round())] = doc["percentage"],
},
),
chart = !f.format(percentageChangeTotal).contains("-")
? LineChart.fromDateTimeMaps(
[line1], [Colors.red], ['%'],
)
: LineChart.fromDateTimeMaps(
[line1], [Colors.green], ['%'],
),
},
);
}
@override
void dispose() {
super.dispose();
}
@override
void initState() {
createLine2();
super.initState();
}
@override
Widget build(BuildContext context) {
createLine2();
return WillPopScope(
onWillPop: () async => false,
child: Scaffold(
body: SafeArea(
child: Column(
children: <Widget>[
Expanded(
child: StreamBuilder<QuerySnapshot>(
stream: Firestore.instance
.collection(
'user/' + widget.userEmail + '/positionLabels')
.orderBy("Date", descending: true)
.orderBy("Time", descending: true)
.snapshots(),
builder: (BuildContext context,
AsyncSnapshot<QuerySnapshot> snapshot) {
if (snapshot.hasError) {
return new Text('Error: ${snapshot.error}');
}
if (!snapshot.hasData) {
return Padding(
padding: const EdgeInsets.all(50.0),
child: Center(
child: Text(
"Add your first transaction!\n\nTap your profile picture in the top right to add your first order",
style: TextStyle(color: Colors.white),
textAlign: TextAlign.center,
),
),
);
} else {
switch (snapshot.connectionState) {
case ConnectionState.none:
print("ConnectionState: NONE");
return Text(
'Select',
style: TextStyle(color: Colors.white),
);
case ConnectionState.waiting:
print("ConnectionState: WAITING");
return LinearProgressIndicator();
case ConnectionState.done:
print("ConnectionState: DONE");
return Text(
'\$${snapshot.data} (closed)',
style: TextStyle(color: Colors.white),
);
default:
return Column(
children: <Widget>[
SearchBar(
widget: widget,
nodeOne: nodeOne,
controller: controller),
GestureDetector(
onTap: () {
setState(() {
readyToShowChart = true;
});
},
child: Column(
children: <Widget>[
Container(
padding: EdgeInsets.only(
top: 20,
right: 10,
left: 10,
bottom: 5),
height: 200,
width: MediaQuery.of(context).size.width,
child: AnimatedOpacity(
duration: Duration(seconds: 2),
opacity: readyToShowChart ? 1 : 0,
child: AnimatedLineChart(
chart,
key: UniqueKey(),
),
),
),
TotalPositionsWidget(
f.format(
getTotalMoneySpent(snapshot.data)),
getTotalAccountValue(snapshot.data)),
PositionsChangeWidget(
workWithNumbers(snapshot.data),
f.format(percentageChangeTotal)),
],
),
),
Expanded(
child: RefreshIndicator(
color: Colors.white,
onRefresh: () => refreshMain(snapshot.data),
key: refreshkey,
child: ListView(
children: snapshot.data.documents.map(
(DocumentSnapshot document) {
return new AnimatedOpacity(
opacity: 1.0,
duration:
Duration(milliseconds: 1000),
child: StockListTile(
document, widget.userEmail),
);
},
).toList(),
),
),
),
],
);
}
}
},
),
),
],
),
),
),
);
}
}
为了简化:我有一个 StreamBuilder,它有一个列作为它的子列。该列包含:
- 搜索栏
- “图形小部件”
- 包裹在 GestureDetector 中的文本小部件
- 可刷新流视图中的数据库条目列表
当我点击文本小部件并触发“setState”时,屏幕会在执行命令之前闪烁浅蓝色:
tapping the Text widget three times
看起来 Flutter 正在重建整个布局。
一个较小的问题(尽管没有闪烁的蓝屏那么重要)是 AnimatedOpacity 和 AnimatedCrossFade 从未在此小部件中实际设置动画,但在其他小部件中执行(以 SearchBar 为例)
我看过this的例子,和我遇到的差不多,不过我这里不处理图片,所以不知道去哪里。
我也尝试过 Dart DevTools,我所能得到的只是(使用时间线中的自下而上视图)它卡在“performLayout --> layout --> performLayout -- > 布局”循环
有人可以指点我正确的方向吗?
【问题讨论】:
-
你设置改变背景颜色了吗?
-
请创建一个minimal, complete and verifiable example 并将其直接发布到您的问题中。
-
@ParthPitroda 这会影响它吗?我将画布颜色设置为 Colors.black。
标签: flutter dart flutter-layout flutter-animation