Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
542 views
in Technique[技术] by (71.8m points)

flutter - Re-usable TextStyle or BoxDecoration with a Future function in it - SharedPrefs - How To?

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I dont understand why you are storing this type of data in shared-Pref because it is async function and takes time to get back the data. But you can store it in the class like this

class ChagneItemValues {
  static const double fontSize = 1.0;
 static const double padding = 10.0;
  
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...