根据来自sharedpreference的数据设置背景色

时间:2019-02-05 12:55:13

标签: dart flutter

在下面的类中,我试图将_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之后重新构建窗口小部件,但这也不起作用。因此,我不确定还有其他方法可以使这项工作成功进行。有任何想法吗?

1 个答案:

答案 0 :(得分:0)

您需要在代码中修复.item.inactive { opacity: 0.5; }函数

<div class="item">Item 1</div>
<div class="item">Item 2</div>
<div class="item">Item 3</div>
<div class="item">Item 4</div>
<div class="item">Item 5</div>
<div class="item">Item 6</div>
<div class="item">Item 7</div>

isDishFavorited()将在您从共享首选项中获取数据后重新构建窗口小部件