I am retrieving variable data from shared preferences based on in-app settings, but I am having a hard time applying it to reusable BoxDecoration or a TextStyle dynamically.
For example, lets say this is my code:
import 'package:baler_bonus/notifiers_providers/settings_notifier.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
class MyPage extends StatefulWidget {
@override
_MyPageState createState() => _MyPageState();
}
class _MyPageState extends State<MyPage> {
@override
Widget build(BuildContext context) {
return Consumer<SettingsNotifier>(
builder: (context, notifier, child) {
return Container(
decoration: BoxDecoration(
color: notifier.selectedColor,
),
child: Text(
'',
style: TextStyle(
fontSize: notifier.selectedSize,
),
),
);
}
);
}
}
So instead, I want to use decoration: kDecoration
or style: kTextStyle
.
What I've tried so far: 1.
class KTextStyle {
getFontSize() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
double size = prefs.getDouble("fontSize");
return size;
}
static TextStyle mainBody(BuildContext context) {
return Theme.of(context)
.textTheme
.bodyText1
.copyWith(fontSize: getFontSize());
}
}
From this I get the error: Instance members can't be accessed from a static method.
If I remove static from TextStyle, I get the error "Instance member 'mainBody' can't be accessed using static access."
and if I add static to getFontSize function, I get the error "type 'Future' is not a subtype of type 'double'"
I have also tried:
2.
final kTextStyle = Consumer<SettingsNotifier>(
builder: (context, child, notifier) {
return TextStyle(
fontSize: notifier.selectedSize,
)
}
);
I get the error "The return type 'TextStyle' isn't a 'Widget', as required by the closure's context."
and finally I tried:
3.
void kTextStyle() {
return Consumer<SettingsNotifier>(
builder: (buildContext, notifier, child) {
return TextStyle(
fontSize: notifier.selectedSize,
),
},
)
}
I get the error "A value of type 'Consumer' can't be returned from function 'kTextStyle' because it has a return type of 'void'."
I would appreciate your time to help in fixing or re-coding to implement