0

I am using ThemeData to implement dart/light theme

I have to write Theme.of(context).textTheme in each place under the build method to get access to the textTheme.

Is there a way which can solve this problem where I don't need to write this every time in each widget.

I just want to have a global variable say final theme =Theme.of(context).textTheme;

Is there a way to achieve this with the help of get_it and injectable?

I tried registering TextTheme using get_it and injectable since I do not have access to BuildContext outside the widget tree I am have trouble getting this done.

1 Answers1

0

You can easily go with extension in this case . Just write your extension once and use it everywhere

import 'package:flutter/material.dart';

extension ColorExtension on BuildContext{
  TextTheme get textTheme{
    return Theme.of(this).textTheme;
  }
}

Use it like this

@override
  Widget build(BuildContext context) {
    context.textTheme; // used here

    return (Your ui code)
  }
jayesh gurudayalani
  • 1,368
  • 1
  • 9
  • 14
  • I was so keen on finding a way using service locator, I just didnt think of using extension. Thanks for that. – As Kira May 23 '23 at 10:17