Skip to content

Instantly share code, notes, and snippets.

@boeledi
Last active January 28, 2024 22:23
Show Gist options
  • Save boeledi/3e1e6294f8ef4bd78ff33c4d42152913 to your computer and use it in GitHub Desktop.
Save boeledi/3e1e6294f8ef4bd78ff33c4d42152913 to your computer and use it in GitHub Desktop.
Sample to show a way of using the SharedPreferences in a build
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
List<String> _languages = <String>['de','en','es','fr','it','nl','pt'];
class TestPage extends StatefulWidget {
@override
_TestPageState createState() => _TestPageState();
}
class _TestPageState extends State<TestPage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Shared Preferences'),
actions: <Widget>[
FutureBuilder<String>(
// get the languageCode, saved in the preferences
future: SharedPreferencesHelper.getLanguageCode(),
initialData: 'en',
builder: (BuildContext context, AsyncSnapshot<String> snapshot) {
return snapshot.hasData
? _buildFlag(snapshot.data)
: Container();
}),
],
),
body: Container(
child: ListView.builder(
itemCount: _languages.length,
itemBuilder: (BuildContext context, int index){
return ListTile(
title: _buildFlag(_languages[index]),
trailing: IconButton(
icon: const Icon(Icons.arrow_forward),
onPressed: () async {
// Save the user preference
await SharedPreferencesHelper.setLanguageCode(_languages[index]);
// Refresh
setState((){});
},
),
);
},
),
),
);
}
// Returns the flag that corresponds to the languageCode
// Flags are assets, added to the application
Widget _buildFlag(String languageCode){
final double width = 32.0;
return Image.asset(
'images/flag-$languageCode.png',
width: width,
height: width,
);
}
}
class SharedPreferencesHelper {
///
/// Instantiation of the SharedPreferences library
///
static final String _kLanguageCode = "language";
/// ------------------------------------------------------------
/// Method that returns the user language code, 'en' if not set
/// ------------------------------------------------------------
static Future<String> getLanguageCode() async {
final SharedPreferences prefs = await SharedPreferences.getInstance();
return prefs.getString(_kLanguageCode) ?? 'en';
}
/// ----------------------------------------------------------
/// Method that saves the user language code
/// ----------------------------------------------------------
static Future<bool> setLanguageCode(String value) async {
final SharedPreferences prefs = await SharedPreferences.getInstance();
return prefs.setString(_kLanguageCode, value);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment