Skip to content

Software Development at Program Tom LTD

Place for coding, programming, development and software in general.

Menu
  • Blog
  • PDF Booklets
  • Dev Utils & Content
  • Java Spring Boot Or Web Apps
  • English
    • български
    • English
    • Español
    • Português
    • हिन्दी
    • Русский
    • Deutsch
    • Français
    • Italiano
    • العربية
  • About Us
Menu
Flutter Stateful Widget Builder Pattern

Flutter Stateful Widget Builder Pattern

Posted on December 17, 2025December 17, 2025 by Toma Velev

Creating a StatefulWidget that passes data down using a Builder pattern is a common and effective way to manage local state without passing values to widget contructors. Thos way you provide values to children without manually passing parameters through every constructor (prop-drilling).

The “Provider-lite” Pattern

In Flutter, this is typically achieved by creating a custom widget that contains a builder function as a property. This function takes the current state/value and returns a Widget.

Step-by-Step Implementation

Here is how you build a counter widget that passes its value down to whatever UI you define in the builder.

1. Define the Widget

The widget needs a builder property. This is a function that usually looks like: Widget Function(BuildContext context, T value).

class MyDataRoot extends StatefulWidget {
  // The builder allows the parent to define the UI while receiving the value
  final Widget Function(BuildContext context, int value, VoidCallback increment) builder;

  const MyDataRoot({super.key, required this.builder});

  @override
  State<MyDataRoot> createState() => _MyDataRootState();
}

2. Define the State

The state holds the actual logic and data. Inside the build method, you simply call the widget.builder function.

class _MyDataRootState extends State<MyDataRoot> {
  int _counter = 0;

  void _incrementCounter() {
    setState(() {
      _counter++;
    });
  }

  @override
  Widget build(BuildContext context) {
    // We pass the state and the logic into the builder
    return widget.builder(context, _counter, _incrementCounter);
  }
}

3. Use the Widget

Now, anywhere in your app, you can wrap a section of your UI with MyDataRoot. The children inside the builder now have access to the value without it being a global variable.

@override
Widget build(BuildContext context) {
  return MyDataRoot(
    builder: (context, value, increment) {
      return Column(
        children: [
          Text('The current value is: $value'),
          ElevatedButton(
            onPressed: increment,
            child: const Text('Add 1'),
          ),
        ],
      );
    },
  );
}


Why use this instead of a constructor?

Feature Builder Pattern Standard Constructor
Decoupling The logic-heavy widget doesn’t need to know what the UI looks like. The parent must know exactly which children to instantiate.
Reusability You can wrap any UI in MyDataRoot to give it “counter powers.” You have to rewrite the logic for every new UI screen.
Rebuilds Only the code inside the builder is swapped/updated. The entire parent widget usually rebuilds.

When to move to InheritedWidget?

If you find yourself nesting multiple builders or needing to access that value deep down (10+ layers) in the widget tree, you should switch from this Builder pattern to an InheritedWidget (or a package like Provider or Riverpod).

  • Join iOS Beta Testing Explained
  • Firebase App Distribution Setup
  • iOS App Lifetime Unverified
  • Flutter Bottom Border
  • Get Flutter current time zone

Categories

  • Apps (25)
  • ChatGPT (24)
  • Choosing a Framework (38)
  • Flutter (279)
  • Graphical User Interface (14)
  • Marketing (118)
  • Software Development (288)
  • Spring (45)
  • StartUp (22)
  • Uncategorized (4)
  • Uncategorized (14)
  • Vaadin (15)

Tags

Algorithms (9) crypto (29) flutterdev (39) General (86) Java (7) QR & Bar Codes (3) Software Dev Choices (33) Spring Boot (1) standards (1) Theme (3) User Authentication & Authorization (9) User Experience (10) Utilities (19) WordPress (11)

Product categories

  • All Technologies (86)
    • Flutter Apps (26)
    • GPT (4)
    • Java (38)
    • Native Android (3)
    • PHP (9)
    • Spring (Boot) / Quarkus (35)
    • Utils (15)
    • Vaadin 24+ (27)
    • Vaadin 8 (1)
  • Apps (18)
    • Employees DB (1)
    • Notes (6)
    • Personal Budget (1)
    • Recipes Book (1)
    • Stuff Organizer (1)
    • To-Do (2)
  • PDF Books (3)
  • Source Code Generators (8)

Recent Posts

  • Join iOS Beta Testing Explained
  • Firebase App Distribution Setup
  • iOS App Lifetime Unverified
  • Flutter Bottom Border
  • Get Flutter current time zone

Post Categories

  • Apps (25)
  • ChatGPT (24)
  • Choosing a Framework (38)
  • Flutter (279)
  • Graphical User Interface (14)
  • Marketing (118)
  • Software Development (288)
  • Spring (45)
  • StartUp (22)
  • Uncategorized (4)
  • Uncategorized (14)
  • Vaadin (15)