Skip to content

Instantly share code, notes, and snippets.

@loic-sharma
Last active December 27, 2025 21:14
Show Gist options
  • Select an option

  • Save loic-sharma/953b98d1cf1ac503052320121755b8e5 to your computer and use it in GitHub Desktop.

Select an option

Save loic-sharma/953b98d1cf1ac503052320121755b8e5 to your computer and use it in GitHub Desktop.
Inherited value

InheritedValue

Before After
import 'package:flutter/material.dart';

void main() {
  runApp(MaterialApp(home: MyScreen()));
}

class AppColors extends InheritedWidget {
  const AppColors({
    required this.buttonColor,
    required super.child,
  });

  final Color buttonColor;

  static AppColors? maybeOf(BuildContext context) {
    return context.dependOnInheritedWidgetOfExactType<AppColors>();
  }

  static AppColors of(BuildContext context) {
    final AppColors? model = maybeOf(context);
    assert(model != null);
    return model!;
  }

  @override
  bool updateShouldNotify(AppColors oldWidget) {
    return oldWidget.buttonColor != buttonColor;
  }
}

class MyScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return AppColors(
      buttonColor: Colors.blue,
      child: Scaffold(
        body: Center(child: MyButton()),
      ),
    );
  }
}

class MyButton extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return TextButton(
      style: TextButton.styleFrom(
        foregroundColor: AppColors.of(context).buttonColor,
      ),
      onPressed: () {},
      child: const Text('Hello world'),
    );
  }
}
import 'package:flutter/material.dart';

void main() {
  runApp(MaterialApp(home: MyScreen()));
}

class AppColors({required final Color buttonColor});

class MyScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return InheritedValue(
      create: () => AppColors(buttonColor: Colors.blue),
      child: Scaffold(
        body: Center(child: MyButton()),
      ),
    );
  }
}

class MyButton extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final AppColors colors = InheritedValue<AppColors>.of(context);

    return TextButton(
      style: TextButton.styleFrom(foregroundColor: colors.buttonColor),
      onPressed: () {},
      child: const Text('Hello world'),
    );
  }
}

InheritedValue.value

Before After
import 'package:flutter/material.dart';

void main() {
  runApp(MaterialApp(home: MyScreen()));
}

class AppColors extends InheritedWidget {
  const AppColors({
    required this.buttonColor,
    required super.child,
  });

  final Color buttonColor;

  static AppColors? maybeOf(BuildContext context) {
    return context.dependOnInheritedWidgetOfExactType<AppColors>();
  }

  static AppColors of(BuildContext context) {
    final AppColors? model = maybeOf(context);
    assert(model != null);
    return model!;
  }

  @override
  bool updateShouldNotify(AppColors oldWidget) {
    return oldWidget.buttonColor != buttonColor;
  }
}

class MyScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return AppColors(
      buttonColor: Colors.blue,
      child: Scaffold(
        body: Center(child: MyButton()),
      ),
    );
  }
}

class MyButton extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return TextButton(
      style: TextButton.styleFrom(
        foregroundColor: AppColors.of(context).buttonColor,
      ),
      onPressed: () {},
      child: const Text('Hello world'),
    );
  }
}
import 'package:flutter/material.dart';

void main() {
  runApp(MaterialApp(home: MyScreen()));
}

class AppColors({required final Color buttonColor});

class MyScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return InheritedValue.value(
      value: AppColors(buttonColor: Colors.blue),
      updateShouldNotify: (oldValue, newValue) {
        return oldValue.buttonColor != newValue.buttonColor;
      },
      child: Scaffold(
        body: Center(child: MyButton()),
      ),
    );
  }
}

class MyButton extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final AppColors colors = InheritedValue<AppColors>.of(context);

    return TextButton(
      style: TextButton.styleFrom(foregroundColor: colors.buttonColor),
      onPressed: () {},
      child: const Text('Hello world'),
    );
  }
}

InheritedValue.multiple

import 'package:flutter/material.dart';

void main() {
  runApp(MaterialApp(home: CounterScreen()));
}

class AppColors({final Color buttonColor});
class CounterModel({final VoidCallback increment});

class CounterScreen extends StatefulWidget {
  @override
  State<CounterScreen> createState() => _CounterScreenState();
}

class _CounterScreenState extends State<CounterScreen> {
  int counter = 0;

  @override
  Widget build(BuildContext context) {
    return InheritedValue.multiple(
      create: () => [
        AppColors(buttonColor: Colors.blue),
        CounterModel(increment: () => setState(() => counter++)),
      ],
      child: Scaffold(
        body: Center(child: Text('Count: $counter')),
        floatingActionButton: IncrementButton(),
      ),
    );
  }
}

class IncrementButton extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final colors = InheritedValue<AppColors>.of(context);
    final counter = InheritedValue<CounterModel>.of(context);
 
    return FloatingActionButton(
      foregroundColor: colors.buttonColor,
      onPressed: counter.increment,
      child: const Icon(Icons.add),
    );
  }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment