我正在嘗試為我的應用程式創建一個最喜歡的按鈕。哪個作業是改變和保存顏色,而用戶按下它,所以我決定使用hive db。問題是,當我點擊按鈕時;顏色變了,但是當我移動到其他頁面或熱啟動/重新加載頁面時,顏色會自動變回原來的顏色。如何解決此問題并成功創建收藏按鈕。
class p1 extends StatefulWidget {
@override
_p1State createState() => _p1State();
}
class _p1State extends State<p1> {
Box box;
_p1State();
@override
void initstate(){
super.initState();
// Get reference to an already opened box
box = Hive.box(FAVORITES_BOX);
}
@override
void dispose() {
// Closes all Hive boxes
Hive.close();
super.dispose();
}
get_info(){
var info = box.get(_isFavorite);
}
var _isFavorite = true;
@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(' Rehman '
,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;
});
box.put(!_isFavorite, _isFavorite);
get_info();
}
)
)
)])
),
);
}
}
uj5u.com熱心網友回復:
這段代碼中有很多沒有意義的東西。
我相信流程應該是這樣的。
- 您的 Widget 首先初始化 hive 框并檢查是否有任何資料可供參考。
- 如果已經寫入了資料,則加載它,否則,設定一個默認值,我打賭是
false. 因為在你喜歡之前,一切都不是最喜歡的。 - 當您單擊“收藏夾”按鈕時,它會更新
_isFavorite值并Box使用一個鍵將其打開,該鍵必須是可以代表所選專案的唯一值
您的代碼存在問題:
- 您初始化了
BoxininitState()但您沒有考慮 中是否已經有資料Box。所以你必須檢查,如果有你可以使用的資料,你必須相應地更新_isFavorite。 Box每次處理小部件時您都會關閉,但當您回傳小部件時,我看不到您的小部件再次打開它。所以不要關閉它。看起來你會經常使用它。- 你用一個鍵來放置資料,它是資料型別為 的舊資料
boolean。它是二進制的,因此不能用作唯一鍵。
所以如果我是你,我會像下面這樣修復代碼:
import 'package:flutter/material.dart';
import 'package:hive/hive.dart';
class p1 extends StatefulWidget {
@override
_p1State createState() => _p1State();
}
class _p1State extends State<p1> {
late Box box;
late bool _isFavorite;
@override
void initState() {
super.initState();
// Get reference to an already opened box.
// This should be open before you re-enter this widget.
box = Hive.box('FAVORITES_BOX');
// Try getting data that already exists
final data = box.get(someKeyYouAlreadyUsed);
// if there is no data about favorite, assign false to _isFavorite
_isFavorite = data ?? false;
}
// Don't close the Hive Box if you're planning to come back and use it soon.
// @override
// void dispose() {
// // Closes all Hive boxes
// Hive.close();
// super.dispose();
// }
// I deleted this function. It doesn't do anything.
// get_info() {
// var info = box.get(_isFavorite);
// }
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: Stack(children: <Widget>[
const Image(
image: AssetImage("Image/Chowsun1.jpg"),
fit: BoxFit.cover,
width: double.infinity,
height: double.infinity,
),
const Align(
alignment: Alignment.center,
child: Text(' Rehman ',
style: TextStyle(
fontSize: 35.0,
color: Colors.white,
fontFamily: "Explora",
fontWeight: FontWeight.w900))),
Stack(
children: const [
Positioned(
top: 90,
right: 20,
child: 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;
});
box.put(someUniqueValueToRepresentThisItem, _isFavorite);
})))
])),
);
}
}
我希望這對你有用。
uj5u.com熱心網友回復:
您正在根據 _isFavorite 變數值而不是來自 hive 資料庫(您的獲取資訊功能)的值設定按鈕的狀態。在 hive db 中插入值后,您必須使用 hive db 值而不是 _isFavorite 變數設定按鈕的狀態。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/377558.html
