From b29d27eff086f6883b9b8194c6184beff89b72d8 Mon Sep 17 00:00:00 2001 From: Soumyadeep Sinha <43180848+Soumyadeep21@users.noreply.github.com> Date: Sat, 15 Oct 2022 23:58:20 +0530 Subject: [PATCH] Create flutter_provider.dart --- dart/flutter_provider.dart | 105 +++++++++++++++++++++++++++++++++++++ 1 file changed, 105 insertions(+) create mode 100644 dart/flutter_provider.dart diff --git a/dart/flutter_provider.dart b/dart/flutter_provider.dart new file mode 100644 index 0000000..ae795ac --- /dev/null +++ b/dart/flutter_provider.dart @@ -0,0 +1,105 @@ +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: (context) => 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('Provider example'), + ), + body: Center( + child: Column( + mainAxisSize: MainAxisSize.min, + mainAxisAlignment: MainAxisAlignment.center, + children: const [ + 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().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().count}', + key: const Key('counterState'), + style: Theme.of(context).textTheme.headline4, + ); + } +}