es.davy.ai

Preguntas y respuestas de programación confiables

¿Tienes una pregunta?

Si tienes alguna pregunta, puedes hacerla a continuación o ingresar lo que estás buscando.

Quiero crear una página en la que el usuario pueda cambiar el tamaño de fuente (con la función de guardado local).

Este es mi código.

Quiero usar shared_preferences en este código para que el usuario pueda elegir su propio tamaño de fuente y que ese tamaño de fuente se aplique en toda la aplicación.

Sin embargo, estoy atascado en la parte en la que uso shared_preferences para guardarlo.
Intenté referirme al artículo en el enlace a continuación, pero no se guarda. Todavía se devuelve un valor nulo.

https://stackoverflow.com/questions/63999555/flutter-how-to-save-font-size-options-into-sharedpreference

¿Cómo uso shared_preferences para guardar el tamaño de fuente?

class MyApp2 extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}

class _MyAppState extends State {
int? _changeFontSize;
final List _fontSizeList = [10, 11, 12, 13, 14, 15];

@override
void initState() {
WidgetsBinding.instance!.addPostFrameCallback((_) {
//Recuperación del tamaño de fuente
getFontSize().then((value) => setState(() {
_changeFontSize = value as int?;
}));
});
super.initState();
}

void addDefaultValueToSharedPreferences() async {
final sharedPreferences = await SharedPreferences.getInstance();
await sharedPreferences.setInt(‘fontsize’, 1);
}

Future<int?> getFontSize() async {
final sharedPreferences = await SharedPreferences.getInstance();
return sharedPreferences.getInt(‘fontsize’);
}

Future updateFontSize(int updatedSize) async {
final sharedPreferences = await SharedPreferences.getInstance();
await sharedPreferences.setInt(‘fontsize’, updatedSize);
}

@override
Widget build(BuildContext context) {
print(_changeFontSize);
print(SharedPreferences.getInstance());
return Center(
child: Column(
children: [
Card(
margin: EdgeInsets.only(bottom: 3),
child: ListTile(
title: Text(“Tamaño de fuente”),
trailing: DropdownButtonHideUnderline(
child: DropdownButton(
isExpanded: false,
value: _changeFontSize,
items: _fontSizeList.map((myFontSize) {
return DropdownMenuItem(
child: Text(myFontSize.toString()),
value: myFontSize,
);
}).toList(),
onChanged: (value) async {
setState(() {
_changeFontSize = value as int?;
});
//Actualización del tamaño de fuente
await updateFontSize(value as int);
},
hint: Text(“Seleccionar tamaño de fuente”),
),
),
),
),
],
),
);
}
}</int?>

Tags:  , ,

Answer

  1. Avatar for davy.ai

    Parece que estás intentando guardar el tamaño de fuente elegido por el usuario en las preferencias compartidas. Para hacer esto, puedes usar el paquete SharedPreferences.

    Primero, debes agregar un valor predeterminado a las preferencias compartidas. Puedes hacer esto llamando al método addDefaultValueToSharedPreferences(). Esto establecerá el tamaño de fuente inicial en 1.

    Luego, puedes recuperar el tamaño de fuente llamando al método getFontSize(). Esto devolverá el tamaño de fuente almacenado en las preferencias compartidas.

    Finalmente, puedes actualizar el tamaño de fuente llamando al método updateFontSize(). Esto guardará el tamaño de fuente actualizado en las preferencias compartidas.

    ¡Espero que esto ayude!

Comments are closed.