Skip to content

Instantly share code, notes, and snippets.

@chimon2000
Last active May 3, 2023 18:58
Show Gist options
  • Save chimon2000/f543fd97d233147ab10355cd3699afac to your computer and use it in GitHub Desktop.
Save chimon2000/f543fd97d233147ab10355cd3699afac to your computer and use it in GitHub Desktop.

Revisions

  1. chimon2000 revised this gist May 3, 2023. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion main.dart
    Original file line number Diff line number Diff line change
    @@ -88,7 +88,7 @@ class CounterScope extends InheritedNotifier<CounterController> {
    context.dependOnInheritedWidgetOfExactType<CounterScope>();

    assert(result != null, 'No CounterScope found in context');
    assert(result!.notifier != null, 'No CounterScope found in context');
    assert(result!.notifier != null, 'No CounterController found in context');

    return result!.notifier!;
    }
  2. chimon2000 revised this gist May 3, 2023. 1 changed file with 27 additions and 27 deletions.
    54 changes: 27 additions & 27 deletions main.dart
    Original file line number Diff line number Diff line change
    @@ -49,6 +49,33 @@ class HomePage extends StatelessWidget {
    }
    }

    class CounterDisplay extends StatelessWidget {
    const CounterDisplay({super.key});

    @override
    Widget build(BuildContext context) {
    return Text(
    '${CounterScope.of(context).value}',
    style: Theme.of(context).textTheme.headlineMedium,
    );
    }
    }

    class IncrementButton extends StatelessWidget {
    const IncrementButton({super.key});

    @override
    Widget build(BuildContext context) {
    return Builder(
    builder: (context) => FloatingActionButton(
    onPressed: CounterScope.of(context).increment,
    tooltip: 'Increment',
    child: const Icon(Icons.add),
    ),
    );
    }
    }

    class CounterScope extends InheritedNotifier<CounterController> {
    const CounterScope({
    super.key,
    @@ -78,30 +105,3 @@ class CounterController extends ValueNotifier<int> {
    value = value - 1;
    }
    }

    class CounterDisplay extends StatelessWidget {
    const CounterDisplay({super.key});

    @override
    Widget build(BuildContext context) {
    return Text(
    '${CounterScope.of(context).value}',
    style: Theme.of(context).textTheme.headlineMedium,
    );
    }
    }

    class IncrementButton extends StatelessWidget {
    const IncrementButton({super.key});

    @override
    Widget build(BuildContext context) {
    return Builder(
    builder: (context) => FloatingActionButton(
    onPressed: CounterScope.of(context).increment,
    tooltip: 'Increment',
    child: const Icon(Icons.add),
    ),
    );
    }
    }
  3. chimon2000 revised this gist May 3, 2023. 1 changed file with 22 additions and 18 deletions.
    40 changes: 22 additions & 18 deletions main.dart
    Original file line number Diff line number Diff line change
    @@ -1,12 +1,8 @@
    // Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file
    // for details. All rights reserved. Use of this source code is governed by a
    // BSD-style license that can be found in the LICENSE file.

    import 'package:flutter/material.dart';

    void main() => runApp(MyApp());
    void main() => runApp(CounterApp());

    class MyApp extends StatelessWidget {
    class CounterApp extends StatelessWidget {
    @override
    Widget build(BuildContext context) {
    return MaterialApp(
    @@ -15,13 +11,13 @@ class MyApp extends StatelessWidget {
    theme: ThemeData(
    primarySwatch: Colors.blue,
    ),
    home: const MyHomePage(title: 'Flutter Demo Home Page'),
    home: const HomePage(title: 'Flutter Demo Home Page'),
    );
    }
    }

    class MyHomePage extends StatelessWidget {
    const MyHomePage({
    class HomePage extends StatelessWidget {
    const HomePage({
    Key? key,
    required this.title,
    }) : super(key: key);
    @@ -30,7 +26,6 @@ class MyHomePage extends StatelessWidget {

    @override
    Widget build(BuildContext context) {

    return CounterScope(
    notifier: CounterController(),
    child: Scaffold(
    @@ -48,13 +43,7 @@ class MyHomePage extends StatelessWidget {
    ],
    ),
    ),
    floatingActionButton: Builder(
    builder: (context) => FloatingActionButton(
    onPressed: CounterScope.of(context).increment,
    tooltip: 'Increment',
    child: const Icon(Icons.add),
    ),
    ),
    floatingActionButton: const IncrementButton(),
    ),
    );
    }
    @@ -72,7 +61,7 @@ class CounterScope extends InheritedNotifier<CounterController> {
    context.dependOnInheritedWidgetOfExactType<CounterScope>();

    assert(result != null, 'No CounterScope found in context');
    assert(result!.notifier != null, 'No CounterController found in context');
    assert(result!.notifier != null, 'No CounterScope found in context');

    return result!.notifier!;
    }
    @@ -101,3 +90,18 @@ class CounterDisplay extends StatelessWidget {
    );
    }
    }

    class IncrementButton extends StatelessWidget {
    const IncrementButton({super.key});

    @override
    Widget build(BuildContext context) {
    return Builder(
    builder: (context) => FloatingActionButton(
    onPressed: CounterScope.of(context).increment,
    tooltip: 'Increment',
    child: const Icon(Icons.add),
    ),
    );
    }
    }
  4. chimon2000 revised this gist May 3, 2023. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion main.dart
    Original file line number Diff line number Diff line change
    @@ -72,7 +72,7 @@ class CounterScope extends InheritedNotifier<CounterController> {
    context.dependOnInheritedWidgetOfExactType<CounterScope>();

    assert(result != null, 'No CounterScope found in context');
    assert(result!.notifier != null, 'No CounterScope found in context');
    assert(result!.notifier != null, 'No CounterController found in context');

    return result!.notifier!;
    }
  5. chimon2000 created this gist May 3, 2023.
    103 changes: 103 additions & 0 deletions main.dart
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,103 @@
    // Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file
    // for details. All rights reserved. Use of this source code is governed by a
    // BSD-style license that can be found in the LICENSE file.

    import 'package:flutter/material.dart';

    void main() => runApp(MyApp());

    class MyApp extends StatelessWidget {
    @override
    Widget build(BuildContext context) {
    return MaterialApp(
    title: 'Flutter Demo',
    debugShowCheckedModeBanner: false,
    theme: ThemeData(
    primarySwatch: Colors.blue,
    ),
    home: const MyHomePage(title: 'Flutter Demo Home Page'),
    );
    }
    }

    class MyHomePage extends StatelessWidget {
    const MyHomePage({
    Key? key,
    required this.title,
    }) : super(key: key);

    final String title;

    @override
    Widget build(BuildContext context) {

    return CounterScope(
    notifier: CounterController(),
    child: Scaffold(
    appBar: AppBar(
    title: Text(title),
    ),
    body: Center(
    child: Column(
    mainAxisAlignment: MainAxisAlignment.center,
    children: const [
    Text(
    'You have pushed the button this many times:',
    ),
    CounterDisplay(),
    ],
    ),
    ),
    floatingActionButton: Builder(
    builder: (context) => FloatingActionButton(
    onPressed: CounterScope.of(context).increment,
    tooltip: 'Increment',
    child: const Icon(Icons.add),
    ),
    ),
    ),
    );
    }
    }

    class CounterScope extends InheritedNotifier<CounterController> {
    const CounterScope({
    super.key,
    required super.child,
    super.notifier,
    });

    static CounterController of(BuildContext context) {
    final CounterScope? result =
    context.dependOnInheritedWidgetOfExactType<CounterScope>();

    assert(result != null, 'No CounterScope found in context');
    assert(result!.notifier != null, 'No CounterScope found in context');

    return result!.notifier!;
    }
    }

    class CounterController extends ValueNotifier<int> {
    CounterController([int initialValue = 0]) : super(initialValue);

    void increment() {
    value = value + 1;
    }

    void decrement() {
    value = value - 1;
    }
    }

    class CounterDisplay extends StatelessWidget {
    const CounterDisplay({super.key});

    @override
    Widget build(BuildContext context) {
    return Text(
    '${CounterScope.of(context).value}',
    style: Theme.of(context).textTheme.headlineMedium,
    );
    }
    }