我有一个CircularProgressIndicator,是这样定义的。
const corpLoading = CircularProgressIndicator(
valueColor: AlwaysStoppedAnimation<Color> (corpColorPrimary),
);
但是当我在FutureBuilder中使用它时,它没有正确地呈现出来(见下图)。这是一种......切割。
我的代码。
class ScreenTextViewer extends StatelessWidget {
final String _title;
final String _text;
final String _resourceFileToLoad;
ScreenTextViewer({
@required String title,
String text,
String resourceFileToLoad,
Key key
}) : assert(title != null && title != "" && ((text != null && text != "") || (resourceFileToLoad != null && resourceFileToLoad != ""))),
_title = title,
_text = text,
_resourceFileToLoad = resourceFileToLoad,
super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("$_title"),
),
body: Center(
child: Scrollbar (
child: SingleChildScrollView(
child: _text == null
? FutureBuilder<String>(
future: rootBundle.loadString(_resourceFileToLoad),
builder: (BuildContext context, AsyncSnapshot<String> snapshot) {
// return corpLoading; // same error here
if (snapshot.connectionState != ConnectionState.done) {
return corpLoading;
}
if (snapshot.hasError) {
return Text('Error: ${snapshot.error}');
}
if (!snapshot.hasData){
return const Text('Error: Nothing to show');
}
// here is all good
return RichText(
text: TextSpan(
text: snapshot.data.toString(), // text: "$snapshot.data",
style: DefaultTextStyle.of(context).style,
),
);
})
: RichText(
text: TextSpan(
text: "$_text",
style: DefaultTextStyle.of(context).style,
),
),
),
),
),
);
}
}
问题出在哪里? 我怎么才能解决这个问题?我从来没有遇到过这样的问题......
我想你应该只在usecase snapshot.hasData中使用SingleChildScrollView。
你可以试着把你的 CircularProgressIndicator
用一个固定大小的容器,或者在容器上加一个填充物。