MyExample AnimatedContainer ValueNotifier

mail@pastecode.io avatar
unknown
dart
2 years ago
2.0 kB
2
Indexable
Never
import 'package:flutter/material.dart';

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

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

  static const String _title = 'Flutter Code Sample';

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: _title,
      home: Scaffold(
        appBar: AppBar(title: const Text(_title)),
        body: const MyExample(),
      ),
    );
  }
}

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

  @override
  Widget build(BuildContext context) {
    /// [1]
    ValueNotifier<bool> selectedValueNotifier = ValueNotifier(false);

    /// [2] [ValueListenableBuilder] GestureDetector widgetini sararak
    /// [selectedValueNotifier]'in durumu her değiştiğinde [value]'yı rebuild eder.
    return ValueListenableBuilder<bool>(

        /// [3] [valueListenable] parametresine [selectedValueNotifier]
        ///  değişkenini dinlemesini istedik.
        valueListenable: selectedValueNotifier,
        builder: (context, selected, child) {
          return GestureDetector(
            onTap: () {
                ///[4] Dinlenen değişkenin içindeki değere .value ile erişerek
                /// durumunu tersine çevir.
              selectedValueNotifier.value = !selected;
            },
            child: Center(
              child: AnimatedContainer(
                width: selected ? 200.0 : 100.0,
                height: selected ? 100.0 : 200.0,
                color: selected ? Colors.red : Colors.blue,
                alignment: selected
                    ? Alignment.center
                    : AlignmentDirectional.topCenter,
                duration: const Duration(seconds: 2),
                curve: Curves.fastOutSlowIn,
                child: const FlutterLogo(size: 75),
              ),
            ),
          );
        });
  }
}