在Playstore上发布应用程序后颤振刷新小部件

时间:2020-09-19 12:07:57

标签: flutter state publish setstate provider

我是新手,在将应用发布到Playstore时遇到了问题。我正在使用提供程序来更新我的数据,包括诸如颜色或主题之类的参数。它在模拟器上可以正常工作,但对于为alpha发行版编译的捆绑软件则完全不起作用。这是代码。

这是我的主班

class _MedoState extends State<Medo> {
  final ListAlbumProvider _listAlbumProvider = ListAlbumProvider();
  final YoutubeVideoProvider _youtubeVideoProvider = YoutubeVideoProvider();
  final AudioProvider _audioProvider = AudioProvider();
  bool _loadingListAlbum = false;
  UserSettings _userSettings;
  Map<String, Color> _colorSettings;

  @override
  void initState() {
    super.initState();
    loadingListAlbum();
  }
  loadingListAlbum() async {
    _listAlbumProvider.read().then(
          (value) => setState(
            () {
              _loadingListAlbum = value;
            },
          ),
        );
  }
  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
    _listAlbumProvider.addListener(() {
      setState(() {});
    });
    _colorSettings = Settings.of(context).colorsAlbum;
  }

  @override
  Widget build(BuildContext context) {
    print('BUILD');
    _userSettings = _listAlbumProvider.albums.userSettings;

    return MultiProvider(
      providers: [
        ChangeNotifierProvider.value(
          value: _listAlbumProvider,
        ),
        ChangeNotifierProvider.value(
          value: _youtubeVideoProvider,
        ),
        ChangeNotifierProvider.value(
          value: _audioProvider,
        ),
      ],
      child: MaterialApp(
        title: 'Medo',
        debugShowCheckedModeBanner: false,
        home: !_loadingListAlbum ? Logo() : ListAlbumsView(),
        routes: {
          SettingsView.routeName: (_) => SettingsView(),
          AddAlbumView.routeName: (_) => AddAlbumView(),
          AlbumMenuView.routeName: (context) => AlbumMenuView(),
          PhotosAlbumView.routeName: (context) => PhotosAlbumView(),
          VideosAlbumView.routeName: (_) => VideosAlbumView(),
          MediaDisplayView.routeName: (context) => MediaDisplayView(),
          VideosDisplayView.routeName: (context) => VideosDisplayView(),
          ExternalLinkView.routeName: (context) => ExternalLinkView(),
          ExternalLinkDisplayView.routeName: (context) =>
              ExternalLinkDisplayView(),
          AudiosAlbumView.routeName: (context) => AudiosAlbumView(),
          AudiosDisplayView.routeName: (context) => AudiosDisplayView(),
          AlbumSettings.routeName: (context) => AlbumSettings(),
          Loading.routeName: (_) => Loading(),
        },
        onUnknownRoute: (_) => MaterialPageRoute(
          builder: (_) => NotFound(),
        ),
        theme: ThemeData(
          visualDensity: VisualDensity.adaptivePlatformDensity,
          floatingActionButtonTheme: FloatingActionButtonThemeData(
            foregroundColor: Theme.of(context).primaryColor,
          ),
          brightness: _userSettings != null ? _userSettings.themeApp : null,
          primarySwatch: _userSettings != null
              ? _colorSettings[_userSettings.colorApp]
              : null,
        ),
      ),
    );
  }
}

在这里我的小部件带有用于更改应用程序颜色或主题的选项

class _SettingsMenuState extends State<SettingsMenu> {
  bool _switchValue;
  UserSettings _userSettings;

  void settingsThemeApp() {
    _userSettings = Provider.of<ListAlbumProvider>(context, listen: false)
        .albums
        .userSettings;
    if (_userSettings.themeApp == Brightness.light)
      _switchValue = true;
    else
      _switchValue = false;
  }

  void updateThemeApp(bool value) {
    Provider.of<ListAlbumProvider>(context, listen: false)
        .updateThemeApp(value);
  }

  @override
  Widget build(BuildContext context) {
    print('BUILD');
    settingsThemeApp();

    return ListView(
      children: [
        Container(
          padding: EdgeInsets.only(left: 15),
          decoration: BoxDecoration(
            border: Border(
              bottom: BorderSide(
                width: 0.5,
                color: Colors.grey[600],
              ),
            ),
          ),
          height: 70,
          child: Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Text(
                'Thème de l\'application',
                style: TextStyle(
                  fontSize: 18,
                ),
              ),
              Container(
                padding: EdgeInsets.only(right: 15),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.end,
                  children: [
                    Text('Dark'),
                    Switch(
                      activeColor: Colors.white,
                      inactiveThumbColor: Colors.grey[600],
                      activeTrackColor: Colors.grey[400],
                      inactiveTrackColor: Colors.black54,
                      value: _switchValue,
                      onChanged: (value) {
                        updateThemeApp(value);
                      },
                    ),
                    Text('Light'),
                  ],
                ),
              ),
            ],
          ),
        ),
        InkWell(
          child: Container(
            padding: EdgeInsets.only(left: 15),
            decoration: BoxDecoration(
              border: Border(
                bottom: BorderSide(
                  width: 0.5,
                  color: Colors.grey[600],
                ),
              ),
            ),
            height: 70,
            child: Row(
              mainAxisAlignment: MainAxisAlignment.start,
              children: [
                Text(
                  'Couleurs',
                  style: TextStyle(
                    fontSize: 18,
                  ),
                ),
              ],
            ),
          ),
          onTap: () {
            widget.choose();
          },
        ),
      ],
    );
  }
}

最后是提供程序中用于设置新设置的功能

void updateThemeApp(bool value) {
    if (value)
      albums.userSettings.themeApp = Brightness.light;
    else
      albums.userSettings.themeApp = Brightness.dark;
    notifyListeners();
    String jsonFile = json.encode(_albums);
    save(jsonFile);
  }

  void updateColorApp(String key) {
    albums.userSettings.colorApp = key;
    notifyListeners();
    String jsonFile = json.encode(_albums);
    save(jsonFile);
  }

对不起,我的英语。 谢谢。

编辑:我已解决消费者问题。

0 个答案:

没有答案