我正在嘗試為我的應用程式創建一個最喜歡的按鈕。哪個作業是改變和保存顏色,而用戶按下它,所以我決定使用hive db。當圖示按鈕被點擊時;顏色發生變化,這向用戶表明它已被標記為他們的最愛。問題是當我再次點擊它時(如果用戶想要取消標記它)雖然顏色改變了,當我移動到其他頁面或熱啟動/重新加載頁面時,顏色會自動變回原來的顏色(到顏色當它第一次被按下時)。我希望通過按鈕反應顏色并保存。我該如何解決這個問題?(我在關鍵部分有點困惑。也許這就是問題發生的地方)
class p1 extends StatefulWidget {
@override
_p1State createState() => _p1State();
}
class _p1State extends State<p1> {
Box box;
bool _isFavorite = false;
_p1State();
@override
void initstate(){
super.initState();
// Get reference to an already opened box
box = Hive.box(FAVORITES_BOX);
final data = box.get(_isFavorite).containskey("1" != null ? Colors.white:Colors.red );
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body:Stack(
children:<Widget>[
Image(
image:AssetImage("Image/Chowsun1.jpg"),
fit:BoxFit.cover,
width: double.infinity,
height: double.infinity,
),
Align(alignment: Alignment.center,
child: Text(' "\n The entire world , \n is not worth \n A single Tear.\n'
' " \n -Imam Hazrat Ali (R) '
,style: TextStyle(fontSize: 35.0,
color: Colors.white,
fontFamily: "Explora",
fontWeight: FontWeight.w900 ) )
),
Stack ( children: [Positioned(
top:90,
right: 20,
child:const Text(' 1 ',
style: TextStyle(
fontSize: 25.0,
color: Colors.white,
fontFamily: "Comforter"
),
),
)], ),
Align(
alignment: Alignment.bottomCenter,
child: (
IconButton(
icon: Icon(
Icons.favorite,
color:_isFavorite ? Colors.white: Colors.red
),
onPressed: () {
setState(() {
_isFavorite= !_isFavorite;
});
if(box.containsKey(1)){
box.delete(1);
}else
box.put(1, _isFavorite);
}
)
)
)])
),
);
}
}
uj5u.com熱心網友回復:
您可以在 hive 已經打開的情況下直接對其進行初始化
Box box = Hive.box(FAVORITES_BOX);
bool _isFavorite = false;
@override
void initState() {
super.initState();
_isFavorite = box.get(0) ?? false;
}
和變化的價值
onPressed: () {
setState(() {
_isFavorite = !_isFavorite;
});
box.put(0, _isFavorite);
},
uj5u.com熱心網友回復:
我看到你正在使用 bool_isFavorite來記錄一個贊,然后你再次檢查 is favorite 的值,以了解是點贊還是洗掉贊,在你的配置單元正在作業的引擎蓋下,但基本上,你的代碼用于更新顏色不是來自蜂巢,所以當你重新更新你的狀態時,例如熱多載,一切都被重置為初始狀態,保持你最喜歡的按鈕不變。
您基本上只需要重新建模您的邏輯以使其正常作業。
import 'package:hive_flutter/hive_flutter.dart';
void main() async {
await Hive.initFlutter();
await Hive.openBox("favorites");
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key? key, required this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
Box? box = Hive.box("favorites");
bool _isFavorite = false;
@override
void initState() {
super.initState();
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: Stack(
children: <Widget>[
Align(
alignment: Alignment.center,
child: Text(
' "\n The entire world , \n is not worth \n A single Tear.\n'
' " \n -Imam Hazrat Ali (R) ',
style: TextStyle(
fontSize: 35.0,
color: Colors.white,
fontFamily: "Explora",
fontWeight: FontWeight.w900),
),
),
Stack(
children: [
Positioned(
top: 90,
right: 20,
child: const Text(
' 1 ',
style: TextStyle(
fontSize: 25.0,
color: Colors.white,
fontFamily: "Comforter"),
),
)
],
),
Align(
alignment: Alignment.bottomCenter,
child: (IconButton(
icon: Icon(Icons.favorite,
color: box!.isEmpty ? Colors.white : Colors.red),
onPressed: () {
setState(() {
_isFavorite = !_isFavorite;
});
if (box!.isEmpty)
box!.put("isFavorite", _isFavorite);
else
box!.delete("isFavorite");
},
)),
)
],
),
),
);
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/394158.html
上一篇:嘗試使用導航器時的例外
