Created
July 26, 2020 02:24
-
-
Save frazras/c8278be91bd86b8a2ee660edde42731b to your computer and use it in GitHub Desktop.
An example of my broken shared preference flutter slider
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
import 'package:flutter_html/style.dart'; | |
import 'package:flutter_html/flutter_html.dart'; | |
import 'package:shared_preferences/shared_preferences.dart'; | |
class FontSizer extends StatefulWidget { | |
@override | |
_FontSizerState createState() => new _FontSizerState(); | |
} | |
class _FontSizerState extends State<FontSizer> { | |
Future<SharedPreferences> _prefs = SharedPreferences.getInstance(); | |
Future<double> fontSizeValue; | |
@override | |
void initState() { | |
super.initState(); | |
_loadFontSize(); | |
} | |
_loadFontSize() async { | |
final prefs = await _prefs; | |
final double fs = prefs.getDouble('fontSize') ?? 18.0; | |
setState(() { | |
fontSizeValue = prefs.setDouble("fontSize", fs).then((bool success) { | |
return fs; | |
}); | |
}); | |
print('fontsize: $fs'); | |
} | |
_saveFontSize(newFontSize) async { | |
// obtain shared preferences | |
final prefs = await _prefs; | |
// set value | |
prefs.setDouble('fontSize', newFontSize); | |
print("new fontsize set to $newFontSize"); | |
} | |
Widget _buildFontSizer() { | |
return FutureBuilder( | |
future: fontSizeValue, | |
builder: (BuildContext context, AsyncSnapshot snapshot) { | |
switch (snapshot.connectionState) { | |
case ConnectionState.waiting: | |
return const CircularProgressIndicator(); | |
default: | |
if (snapshot.hasError) { | |
return Text('Error: ${snapshot.error}'); | |
} else { | |
print('Snapshot fontsize ${snapshot.data}'); | |
double x = snapshot.data; | |
return Column( | |
children: <Widget>[ Padding( | |
padding: const EdgeInsets.only(top: 48.0), | |
child: SliderTheme( | |
data: SliderTheme.of(context).copyWith( | |
activeTrackColor: Colors.red[700], | |
inactiveTrackColor: Colors.red[100], | |
trackShape: RoundedRectSliderTrackShape(), | |
trackHeight: 4.0, | |
thumbShape: RoundSliderThumbShape( | |
enabledThumbRadius: 12.0), | |
thumbColor: Colors.redAccent, | |
overlayColor: Colors.red.withAlpha(32), | |
overlayShape: RoundSliderOverlayShape( | |
overlayRadius: 28.0), | |
tickMarkShape: RoundSliderTickMarkShape(), | |
activeTickMarkColor: Colors.red[700], | |
inactiveTickMarkColor: Colors.red[100], | |
valueIndicatorShape: PaddleSliderValueIndicatorShape(), | |
valueIndicatorColor: Colors.redAccent, | |
valueIndicatorTextStyle: TextStyle( | |
color: Colors.white, | |
), | |
), | |
child: Slider( | |
value: x, | |
min: 16, | |
max: 30, | |
divisions: 14, | |
label: '$x', | |
onChanged: (value) { | |
setState(() { | |
x = value; | |
}, | |
); | |
print(value); | |
}, | |
onChangeEnd: (value) { | |
setState(() { | |
(value); | |
}, | |
); | |
print(value); | |
}, | |
), | |
), | |
), | |
Html( | |
data: "<font color=\"#0B6138\"><b>1</b></font><br>\nAll creatures of our God and King,<br>\nLift up your voice with us and sing:<br>\nAlleluia! Alleluia!<br>\nO burning sun with golden beam<br>\nAnd silver moon with softer gleam:<br>\n<br>\n<i><b><font color=\"#CD9B1D\">CHORUS:</font></b><br>\nOh, praise Him! Oh, praise Him!<br>\nAlleluia, alleluia, alleluia!<br>\n</i><br>\n<font color=\"#0B6138\"><b>2</b></font><br>\nO rushing wind and breezes soft,<br>\nO clouds that ride the winds aloft:<br>\nOh, praise Him! Alleluia!<br>\nO rising morn, in praise rejoice,<br>\nO lights of evening, find a voice.<br>\n<br>\n", | |
style: { | |
"html": Style(color: Colors.black, | |
fontSize: FontSize(20.0)) | |
} | |
) | |
], | |
); | |
} | |
} | |
}, | |
); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return new Scaffold( | |
appBar:AppBar( | |
title: Text("Font Sizer") | |
), | |
body: Padding( | |
padding: const EdgeInsets.all(10.0), | |
child: SingleChildScrollView( | |
child: _buildFontSizer() | |
), | |
), | |
); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment