我正在通过制作一个小新闻应用程序来学习学习。每个选项卡都有新闻文章窗口小部件的列表,其中包含图像,标题,描述,“了解更多”链接按钮以及用于扩展或收缩窗口小部件以包括或隐藏描述的按钮。
我现在的问题是扩展部分。当我按下按钮以扩展小部件时,会调用setState()
并切换我的 bool(boolean) _expanded
。但是,我不认为它会被重新渲染,因为小部件不会扩展。正如您将在我的代码中看到的那样,列表中有一个内联条件作为小部件,形式为_expanded ? option if its expanded : option if its not expanded
。我已经通过在内联条件前面放置!
来验证了这些小部件是否是我想要的,并且它们显示了它们应该显示的内容,所以我很困惑。
目前,我唯一的想法是,这可能与国家是一成不变的事实有关,也许我是在试图改变国家,而不是取代和重新渲染。有任何想法吗?谢谢!
我试图仅包括必要的内容,但这是一些代码:
class NewsList extends StatefulWidget {
NewsList({this.keyword});
final String keyword;
@override
createState() => NewsListState(keyword: keyword);
}
class NewsListState extends State<NewsList> {
NewsListState({this.imageURLs, this.titles, this.texts, this.keyword});
final List<String> imageURLs;
final List<String> titles;
final List<String> texts;
final String keyword;
List<NewsArticle> _newsArticles = List<NewsArticle>();
@override
void initState() {
super.initState();
_populateNewsArticles();
}
@override
void dispose() {
super.dispose();
}
void _populateNewsArticles() {
WebService().load(NewsArticle().all(keyword)).then((newsArticles) => {
setState(() {
_newsArticles = newsArticles;
})
});
}
Widget _buildNewsArticles(BuildContext build, int index) {
bool _expanded = false;
return Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.all(const Radius.circular(10)),
color: Colors.white,
boxShadow: [
BoxShadow(
color: Colors.grey[300],
blurRadius: 20,
spreadRadius: 3,
offset: Offset(5, 5))
]),
child: Column(
children: <Widget>[
Padding(
padding: const EdgeInsets.symmetric(vertical: 10, horizontal: 10),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
_newsArticles[index].urlToImage == null
? AssetImage(Constants.NEWS_PLACEHOLDER_IMAGE_ASSET_URL)
: CachedNetworkImage(
imageUrl: _newsArticles[index].urlToImage),
Padding(padding: const EdgeInsets.symmetric(vertical: 5)),
Text(
_newsArticles[index].title,
textAlign: TextAlign.left,
style: TextStyle(
fontSize: 25,
fontWeight: FontWeight.w700,
height: 0.8,
),
),
Padding(
padding: const EdgeInsets.fromLTRB(4, 15, 4, 0),
child: Platform.isIOS
? CupertinoButton.filled(
child: Text(
"Learn More",
style: TextStyle(fontSize: 17),
),
onPressed: () =>
navigateToUrl(_newsArticles[index].url),
pressedOpacity: 0.6,
)
: RaisedButton(
onPressed: () =>
navigateToUrl(_newsArticles[index].url),
color: Color.fromRGBO(0, 122, 255, 1.0),
animationDuration: Duration(milliseconds: 1000),
child: Text(
"Learn More",
style: TextStyle(fontSize: 16, color: Colors.white),
),
),
),
!_expanded ? Text("test description") : Container(),
!_expanded
? IconButton(
icon: Icon(Icons.keyboard_arrow_up),
onPressed: () => setState(() {
_expanded = !_expanded;
}),
)
: IconButton(
icon: Icon(Icons.keyboard_arrow_down),
onPressed: () {
setState(() {
debugPrint(_expanded.toString());
_expanded = !_expanded;
debugPrint(_expanded.toString());
});
},
),
],
),
),
],
),
);
}
Widget build(BuildContext context) {
return ListView.separated(
separatorBuilder: (BuildContext context, int index) =>
Padding(padding: const EdgeInsets.symmetric(vertical: 15)),
padding: const EdgeInsets.symmetric(horizontal: 30, vertical: 30),
itemBuilder: _buildNewsArticles,
itemCount: _newsArticles.length,
);
}
}
最佳答案
_expanded
是在buildNewsArticles
方法中定义和初始化的局部变量。因此,每次buildNewsArticles
运行时,_expanded
都会初始化为false。
您应该将_expanded
移出该方法,以使其成为NewsListState的成员变量。