0

在我的 Flutter 应用程序中,用户可以通过从颜色选择器中选择颜色来定义自定义主题。之后,他们可以将 ThemeData 保存到列表中并使用它。问题是应用程序重新启动后,用户添加的 ThemeData 消失了。即使在应用程序重新启动后,如何将这些 ThemeData-s 存储到列表中以保留?

这是我的代码:

ThemeData 列表:

我还有两个默认添加的 ThemeData。

List<ThemeData> toReturnTheme = <ThemeData>[];

List<ThemeData> getThemes() {
    ThemeData szikraTheme =  ThemeData(
      scaffoldBackgroundColor: Color(0xff5a89ba),
      backgroundColor: Color(0xff5a89b0),
      buttonColor: Color(0xfff78848),
      accentColor: Colors.white,
      floatingActionButtonTheme: FloatingActionButtonThemeData(
        backgroundColor: Color(0xff3a89ba),
      ),
      textTheme: TextTheme(
        bodyText2: TextStyle(color: Colors.white, fontFamily: 'Muli')));

    ThemeData cobotTheme =  ThemeData(
        scaffoldBackgroundColor: Color(0xff207ABB),
        backgroundColor: Color(0xff207ABB),
        buttonColor: Color(0xfff78848),
        accentColor: Colors.white,
        floatingActionButtonTheme: FloatingActionButtonThemeData(
          backgroundColor: Color(0xff3a89ba),
        ),
        textTheme: TextTheme(
            bodyText2: TextStyle(color: Colors.white, fontFamily: 'Muli')));

  toReturnTheme.add(szikraTheme);
  toReturnTheme.add(cobotTheme);

  return toReturnTheme;
}

void addToThemeList(ThemeData theme){
  toReturnTheme.add(theme);
}

将新 ThemeData 添加到列表中的 Button:

Padding(
                      padding: const EdgeInsets.all(8.0),
                      child: TextButton(
                          style: TextButton.styleFrom(
                              side: BorderSide(
                            color: Theme.of(context).accentColor,
                          )),
                          onPressed: () {
                            try {
                              ThemeData customTheme = ThemeData(
                                  scaffoldBackgroundColor:
                                      Color(bckgrndColor!.value),
                                  backgroundColor: Color(bckgrndColor!.value),
                                  buttonColor: Color(buttonColor!.value),
                                  accentColor: Color(fontColor!.value),
                                  floatingActionButtonTheme:
                                      FloatingActionButtonThemeData(
                                    backgroundColor: Color(arrowColor!.value),
                                  ),
                                  textTheme: TextTheme(
                                      bodyText2: TextStyle(
                                          color: Color(fontColor!.value),
                                          fontFamily: 'Muli'),
                                      bodyText1: TextStyle(
                                          color: Color(fontColor!.value),
                                          fontFamily:
                                              customThemeController.text)));

                              customThemeController.clear();
                              addToThemeList(customTheme);
                              saveTheme();

                              final snackBar = SnackBar(
                                  behavior: SnackBarBehavior.floating,
                                  content: Text(
                                    "themeCreated".tr().toString(),
                                  ));
                              ScaffoldMessenger.of(context)
                                  .showSnackBar(snackBar);
                            } catch (e) {
                              final snackBar = SnackBar(
                                  behavior: SnackBarBehavior.floating,
                                  backgroundColor: Colors.redAccent,
                                  content: Text(
                                    "chooseValidColors".tr().toString(),
                                  ));
                              ScaffoldMessenger.of(context)
                                  .showSnackBar(snackBar);
                            }
                            setState(() {});
                          },
                          child: Text(
                            "createNewTheme".tr().toString(),
                            style: TextStyle(
                              color: Theme.of(context).accentColor,
                              fontSize: 25.0,
                            ),
                          )),
                    ),

以及显示 ThemeData 列表的 ListView:

Container(
                      width: MediaQuery.of(context).size.width / 2,
                      height: 100,
                      child: ListView.builder(
                          shrinkWrap: true,
                          itemCount: toReturnTheme.length,
                          itemBuilder: (context, int index) {
                            if (index < 2) {
                              return Container();
                            } else {
                              return Container(
                                child: Column(
                                  children: [
                                    ListTile(
                                      title: GestureDetector(
                                        child: Text(
                                          toReturnTheme[index]
                                                      .textTheme
                                                      .bodyText1!
                                                      .fontFamily
                                                      .toString() ==
                                                  ""
                                              ? "Custom Theme"
                                              : toReturnTheme[index]
                                                  .textTheme
                                                  .bodyText1!
                                                  .fontFamily
                                                  .toString(),
                                          style: TextStyle(
                                              fontSize: 22.0,
                                              color: Theme.of(context)
                                                  .accentColor),
                                        ),
                                        onTap: () {
                                          getThemeManager(context)
                                              .selectThemeAtIndex(index);
                                        },
                                      ),
                                      trailing: GestureDetector(
                                        child: Icon(
                                          Icons.delete,
                                          color: Theme.of(context).accentColor,
                                        ),
                                        onTap: () {
                                          toReturnTheme.removeAt(index);
                                          setState(() {});
                                        },
                                      ),
                                      leading: Icon(
                                        Icons.palette_outlined,
                                        color: Theme.of(context).accentColor,
                                      ),
                                    ),
                                  ],
                                ),
                              );
                            }
                          }),
                    ),
4

0 回答 0