我想将一本书添加为书签,如果单击“收藏夹”按钮,则会添加该书,但是当我转到另一个窗口并返回时,图标按钮会更改颜色。
CupertinoButton(
child: Icon(
widget.book.starred
?
CupertinoIcons.heart_solid
: CupertinoIcons.heart,
color: Colors.red,
),
onPressed: () {
setState(() {
widget.book.starred = !widget.book.starred;
addToFavorites(this.book);
});
})//Cupertino Button
如何处理build方法来读取widget.book.starred
的值,然后打印正确的图标?
编辑 显示更多代码
class ReadBook extends StatefulWidget {
Book book;
ReadBook({Key key, this.book}) : super(key: key);
@override
State<StatefulWidget> createState() {
return new ReadBookState(this.book);
}
}
class ReadBookState extends State<ReadBook> {
// Declare a field that holds the Todo
Book book;
bool res;
final controller = new PageController(initialPage: 0, keepPage: true);
static const IconData baseball = const IconData(0xf397,
fontFamily: CupertinoIcons.iconFont,
fontPackage: CupertinoIcons.iconFontPackage);
ReadBookState(this.book);
initState() {
super.initState();
}
void addToFavorites(Book book) async {
Database.checkBookExist(book).then((value) {
if (!value) {
print("${widget.book.englishName} added successfully");
Database.addBookToFirestore(widget.book);
} else {
print("${widget.book.englishName} already added");
}
});
@override
Widget build(BuildContext context) {
Widget toRet;
bool rest = retrieveFromFavorites(book);
if (Platform.isAndroid) {
// Android-specific code
toRet = MaterialApp(
home: Scaffold(
appBar: AppBar(
leading: IconButton(
icon: Icon(Icons.arrow_back),
onPressed: () => Navigator.pop(context, false),
),
actions: <Widget>[],
backgroundColor: Color.fromRGBO(245, 205, 121, 1.0),
title: Text(book.name,
textDirection: TextDirection.rtl,
style:
TextStyle(fontSize: 35.0, fontFamily: 'SCHEHERAZADE')),
),
body: new ListView(
controller: controller,
scrollDirection: Axis.vertical,
children: <Widget>[
new Center(
child: new Text(("Title"),
textDirection: TextDirection.rtl,
style: TextStyle(
fontSize: 35.0, fontFamily: 'SCHEHERAZADE'))),
new Center(
child: new Text((t1 + t2),
textDirection: TextDirection.rtl,
style: TextStyle(
fontSize: 25.0, fontFamily: 'SCHEHERAZADE')))
],
)));
} else if (Platform.isIOS) {
// iOS-specific code
toRet = CupertinoApp(
home: CupertinoPageScaffold(
navigationBar: CupertinoNavigationBar(
backgroundColor: Color.fromRGBO(245, 205, 121, 1.0),
leading: CupertinoButton(
padding: EdgeInsets.only(right: 25.0, bottom: 8.0),
child: Icon(
CupertinoIcons.back,
color: Colors.black,
),
onPressed: () => Navigator.pop(context, false),
),
trailing: Row(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
CupertinoButton(
child: Icon(
widget.book.starred
?
CupertinoIcons.heart_solid
: CupertinoIcons.heart,
color: Colors.red,
),
onPressed: () {
// PopupMenuButton
setState(() {
widget.book.starred = !widget.book.starred;
addToFavorites(this.book);
});
}),
CupertinoButton(
child: Icon(
baseball,
color: Colors.black,
),
onPressed: () =>
// PopupMenuButton
popUpOptionsButton()),
],
),
middle: Text(book.name),
),
child: new ListView(
controller: controller,
scrollDirection: Axis.vertical,
children: <Widget>[
new Center(
child: new Text(("Title"),
textDirection: TextDirection.rtl,
style: TextStyle(
fontSize: 35.0, fontFamily: 'SCHEHERAZADE'))),
new Center(
child: new Text((t1 + t2),
textDirection: TextDirection.rtl,
style: TextStyle(
fontSize: 25.0, fontFamily: 'SCHEHERAZADE')))
],
),
),
);
}
return toRet;
}
}