基于从sharedpreference数据设置背景颜色

问题描述 投票:0回答:1

在下面的类,我想_isFavorited设置为我从SharedPreference获得的价值。不过,我想小部件已经被时间我从Sharedpreference价值构建。如何设置从sharedpreference值,然后显示我的窗口小部件?

 import 'package:shared_preferences/shared_preferences.dart';
    import 'package:flutter/material.dart';

    class FavoriteWidget extends StatefulWidget {
      final dish_name;
      final dish_pic;

      @override
      _FavoriteWidgetState createState() => _FavoriteWidgetState();
      FavoriteWidget(this.dish_name, this.dish_pic,{Key key})
          : super(key: key);
    }

    class _FavoriteWidgetState extends State<FavoriteWidget> {
      bool _isFavorited = false;

      @override
      void initState() {
        // TODO: implement initState
        super.initState();
       isDishFavorited(this.context);
      }

      isDishFavorited(BuildContext context) async{
        SharedPreferences prefs = await SharedPreferences.getInstance();
        if(prefs.getString(widget.dish_name) != null){
//rebuilding the context to display Icons.favorite         
build(context);
          _isFavorited = true;  <== the widget is built before this is set to true. 
        }
        debugPrint("isfavorite inside method is" + _isFavorited.toString());
      }
      // #docregion _toggleFavorite
      void _toggleFavorite() {
        setState(() {
          if (_isFavorited) {
            //Remove from favorites
            _removeFromFavorites();
            _isFavorited = false;
            debugPrint("Removed from favorites");

          } else {
            //Add to favorites
            _addToFavorites();
            _isFavorited = true;
            debugPrint("Added to favorites");
          }
        });
      }


      @override
      Widget build(BuildContext context) {
        return Column(
          mainAxisSize: MainAxisSize.min,
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            IconButton(
              icon: (_isFavorited
                  ? Icon(Icons.favorite, size: 35, color: Colors.green)
                  : Icon(Icons.favorite_border, size: 35, color: Colors.green)),
              color: Colors.red[500],
              onPressed: _toggleFavorite,
            ),
            Container(
              margin: const EdgeInsets.only(top: 8.0),
              child: Text(
                "FAVORITE",
                style: TextStyle(
                  fontSize: 12.0,
                  fontWeight: FontWeight.w400,
                  color: Colors.white,
                ),
              ),
            ),
          ],
        );
      }
    }

我试图做的是is_Favorited设置为true后重建的小部件,但没有任何工作。所以,我不知道怎么回事,使这项工作。有任何想法吗?

dart flutter
1个回答
0
投票

你需要修复的isDishFavorited()功能在你的代码

isDishFavorited(BuildContext context) async{
        SharedPreferences prefs = await SharedPreferences.getInstance();
        if(prefs.getString(widget.dish_name) != null){
            setState(() {
               _isFavorited = true; 
            });
        }
        debugPrint("isfavorite inside method is" + _isFavorited.toString());
}

qazxsw POI将重建你的widget你从共享偏好获取数据后,

© www.soinside.com 2019 - 2024. All rights reserved.