Flutter - Best way to aggregate data from child widgets in an IndexedStack

aeviou

I have an IndexedStack in a Scaffold that I use to manage my registration. The Registration widget itself is Stateful, but the widgets that compose it are Stateless. The parent widget looks like this:

class Registration extends StatefulWidget {
  @override
  _RegistrationState createState() => _RegistrationState();
}

class _RegistrationState extends State<Registration> {
  int _index = 0;

  void _nextPage() {
    setState(() {
      _index++;
    });
  }

  void _prevPage() {
    setState(() {
      _index--;
    });
  }

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      backgroundColor: Colors.white,
      appBar: new AppBar(
        backgroundColor: Colors.white,
        automaticallyImplyLeading: false,
        leading: new IconButton(
            icon: new Icon(Icons.arrow_back,
                color: Theme.of(context).primaryColor),
            onPressed: () {
              if (_index == 0) {
                Navigator.pop(context);
              } else {
                _prevPage();
              }
            }),
        elevation: 0.0,
      ),
      body: IndexedStack(
        children: <Widget>[
          RegistrationPhone(_nextPage),
          RegistrationName(_nextPage),
          RegistrationBirthday(_nextPage),],
        index: _index,
      ),
    );
  }
}

What is the best way to take data from these child widgets?

Should I pass in a callback function and hold the data in the parent? Should I pass the information down the line from widget to widget until it's submitted? I don't know what the practices are for sharing data across multiple screens.

Tasnuva oshin

Use Provider

Add Dependency :

dependencies:
  provider: ^4.3.3

here is the Example :

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

/// This is a reimplementation of the default Flutter application using provider + [ChangeNotifier].

void main() {
  runApp(
    /// Providers are above [MyApp] instead of inside it, so that tests
    /// can use [MyApp] while mocking the providers
    MultiProvider(
      providers: [
        ChangeNotifierProvider(create: (_) => Counter()),
      ],
      child: const MyApp(),
    ),
  );
}

/// Mix-in [DiagnosticableTreeMixin] to have access to [debugFillProperties] for the devtool
// ignore: prefer_mixin
class Counter with ChangeNotifier, DiagnosticableTreeMixin {
  int _count = 0;

  int get count => _count;

  void increment() {
    _count++;
    notifyListeners();
  }

  /// Makes `Counter` readable inside the devtools by listing all of its properties
  @override
  void debugFillProperties(DiagnosticPropertiesBuilder properties) {
    super.debugFillProperties(properties);
    properties.add(IntProperty('count', count));
  }
}

class MyApp extends StatelessWidget {
  const MyApp({Key key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatelessWidget {
  const MyHomePage({Key key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Example'),
      ),
      body: Center(
        child: Column(
          mainAxisSize: MainAxisSize.min,
          mainAxisAlignment: MainAxisAlignment.center,
          children: const <Widget>[
            Text('You have pushed the button this many times:'),

            /// Extracted as a separate widget for performance optimization.
            /// As a separate widget, it will rebuild independently from [MyHomePage].
            ///
            /// This is totally optional (and rarely needed).
            /// Similarly, we could also use [Consumer] or [Selector].
            Count(),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        key: const Key('increment_floatingActionButton'),

        /// Calls `context.read` instead of `context.watch` so that it does not rebuild
        /// when [Counter] changes.
        onPressed: () => context.read<Counter>().increment(),
        tooltip: 'Increment',
        child: const Icon(Icons.add),
      ),
    );
  }
}

class Count extends StatelessWidget {
  const Count({Key key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Text(

        /// Calls `context.watch` to make [Count] rebuild when [Counter] changes.
        '${context.watch<Counter>().count}',
        key: const Key('counterState'),
        style: Theme.of(context).textTheme.headline4);
  }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

What's the best way of scraping data from a website?

From Dev

Best way to create instance of child object from parent object

From Dev

AngularJS: Best way to handle data from socket with ng-repeat

From Dev

Better way to aggregate timestamped data?

From Dev

Best way to sort data

From Dev

Best way to compare data from file to data in array in Matlab

From Dev

Aggregate data from child into parent for Binding

From Dev

Best way to extract data from string

From Dev

Best way to send data from DB to Android

From Dev

Best way to encode data

From Dev

Best way to read data from text files stored in your app

From Dev

How do I fill a child entity with data from DB from inside an aggregate root?

From Dev

Best way to request data from server

From Dev

Best way to aggregate a simple dict in Python

From Dev

Best way to show data from multiple tables

From Dev

Flutter: Get data back from StatefulWidget child class to parent

From Dev

Best way to access parent properties from a child class

From Dev

Best way to create child class from a abstract Base class

From Dev

best way to import data from a php into tableview

From Dev

Best way to periodically fetch data from server

From Dev

Best way to request data from server

From Dev

Best way to aggregate a simple dict in Python

From Dev

Best Way to call Child method from Parent in c++ inheritance

From Dev

Best way to concatenate column values in aggregate

From Dev

Best way to aggregate logging data

From Dev

Best way to transform data

From Dev

What is the best way to index aggregate data on ElasticSearch

From Dev

Best way to persist data?

From Dev

Angular 4 - Best way to populate child components data

Related Related

HotTag

Archive