Flutter State Yönetimine Giriş 2: Signal​

Flutter State Yönetimine Giriş 2: Signal

Bugün Stream/Akış tabalı çoklu state yönetim paketi Signal / Sinyal i yayınladım. Belki daha sonra video gelebilir. Aşağıda örneği mevcut.. iyi kodlamalar..

 
import 'dart:math';
import 'dart:ui';
import 'package:flutter/material.dart';
import 'package:signal/signal.dart';

abstract class MyChannelSignal extends ChannelSignal{}

class MyChannel extends StateChannel<MyChannelSignal>{

  MyChannel() {
    _counterState = CounterState(() => add(CounterStateSignal()));
    _notificationState = NotificationState(() => add(NotificationStateSignal()));
    _colorState = ColorState(() => add(ColorStateSignal()));
  }

//signal: CounterStateSignal
  CounterState _counterState;
  CounterState get counterState => _counterState;

//signal: NotificationStateSignal
  NotificationState _notificationState;
  NotificationState get notificationState => _notificationState;

//signal: ColorStateSignal
  ColorState _colorState;
  ColorState get colorState => _colorState;

}

 


class CounterStateSignal extends MyChannelSignal{}

class CounterState extends BaseState{
  CounterState(void Function() onStateChanged) : super(onStateChanged);
  
 int _count =0;
 int get count => _count;

  void increment() {
    _count = _count + 1;
    doneSucces();
  }

  void decrement() {
    _count = _count - 1;
     doneSucces();
  }

   incrementFuture() async {
    try {
       wait();

      await Future<void>.delayed(Duration(milliseconds: 800));
      _count = _count + 1;

     doneSucces();
    } catch (e) {
      doneError(e.toString());
    }
  }

    decrementFuture() async {
    try {
     wait();

      await Future<void>.delayed(Duration(seconds: 2));
      _count = _count - 1;

      doneSucces();
    } catch (e) {
      doneError(e.toString());
    }
  }
}



class NotificationStateSignal extends MyChannelSignal{}

class NotificationState extends BaseState{
  NotificationState(void Function() onStateChanged) : super(onStateChanged);
  
 bool _isOpen =false;
 bool get isOpen => _isOpen;

   change() {
    _isOpen = !_isOpen;
    doneSucces();
  }

   changeFuture() async {
    try {
       wait();

      await Future<void>.delayed(Duration(milliseconds: 500));
       _isOpen = !_isOpen;

     doneSucces();
    } catch (e) {
      doneError(e.toString());
    }
  }

}



class ColorStateSignal extends MyChannelSignal{}

class ColorState extends BaseState{
  ColorState(void Function() onStateChanged) : super(onStateChanged);
  
 Color _color =Color(0xFFFFFFFF);
 Color get color => _color;
 
   void changeColor() {
   _change();
    doneSucces();
  }

   Future changeColorFuture() async {
    try {
       wait();

      await Future<void>.delayed(Duration(milliseconds: 2000)).then((value) 
      
      
      {
     _change();

     doneSucces();

      });

    } catch (e) {
      doneError(e.toString());
    }
  }


  void _change() {
      Random _random = Random();
      _color = Color.fromARGB(
        _random.nextInt(256),
        _random.nextInt(256),
        _random.nextInt(256),
        _random.nextInt(256),
      );
  }

}
 
import 'package:flutter/material.dart';
import 'package:signal/signal.dart';
import 'MyChannel.dart';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return AncestorChannelProvider<MyChannel>(
      channel: MyChannel(),
      child: MaterialApp(
        title: 'Signal State Management',
        home: MyHomePage(),
      ),
    );
  }
}

class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  MyChannel availableMychannel;

  @override
  void initState() {
    super.initState();
    availableMychannel = MyChannel();
  }

  @override
  void dispose() {
    availableMychannel.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    final mychannel = AncestorChannelProvider.of<MyChannel>(context);

    return Scaffold(
      appBar: AppBar(
        title: Text("Signal State Management"),
      ),
      body: ListView(shrinkWrap: true, children: <Widget>[
        AncestorChannelBuilder<MyChannel, MyChannelSignal>(
          condition: (channel, signal) =>
              signal is NotificationStateSignal || signal is ColorStateSignal,
          builder: (context, channel) => SwitchListTile(
            subtitle: Text('subtitle Text color from ColorState',
                style:
                    TextStyle(color: channel.colorState.color.withAlpha(255))),
            secondary: Icon(channel.notificationState.isOpen
                ? Icons.notifications_active
                : Icons.notifications_off),
            title: channel.notificationState.busy
                ? LinearProgressIndicator()
                : !channel.notificationState.success
                    ? Text(channel.notificationState.error)
                    : Text(channel.notificationState.isOpen
                        ? 'Notification Open'
                        : 'Notification Closed'),
            value: channel.notificationState.isOpen,
            onChanged: (bool value) => channel.notificationState.change(),
          ),
        ),
        Container(
          alignment: Alignment.center,
          height: 50,
          child: AncestorChannelBuilder<MyChannel, MyChannelSignal>(
            condition: (channel, signal) => signal is CounterStateSignal,
            builder: (context, channel) => channel.counterState.busy
                ? Container(
                    width: 50,
                    height: 50,
                    child: CircularProgressIndicator(
                      strokeWidth: 2,
                    ))
                : !channel.counterState.success
                    ? Text(channel.notificationState.error)
                    : Text(channel.counterState.count.toString(),
                        style: TextStyle(fontSize: 25)),
          ),
        ),
        AncestorChannelBuilder<MyChannel, MyChannelSignal>(
          condition: (channel, signal) => signal is ColorStateSignal,
          builder: (context, channel) => Container(
            height: 20,
            alignment: Alignment.center,
            child: channel.colorState.busy
                ? LinearProgressIndicator()
                : !channel.colorState.success
                    ? Text(channel.notificationState.error)
                    : Text(channel.colorState.color.toString(),
                        style: TextStyle(fontSize: 20)),
          ),
        ),
        AvailableChannelBuilder<MyChannel, MyChannelSignal>(
          channel: availableMychannel,
          condition: (channel, signal) => signal is CounterStateSignal,
          builder: (context, channel) => Column(
            mainAxisAlignment: MainAxisAlignment.center,
            mainAxisSize: MainAxisSize.max,
            children: <Widget>[
              Container(
                width: 50,
                height: 50,
                alignment: Alignment.center,
                child: channel.counterState.busy
                    ? Container(
                        width: 20,
                        height: 20,
                        child: CircularProgressIndicator(
                          strokeWidth: 2,
                        ))
                    : !channel.counterState.success
                        ? Text(channel.notificationState.error)
                        : Text(channel.counterState.count.toString(),
                            style: TextStyle(fontSize: 25)),
              ),
              RaisedButton(
                  child: Text(
                      'AvailableChannelBuilder : CounterState : increment'),
                  onPressed: () => channel.counterState.increment()),
              RaisedButton(
                  child: Text(
                      'AvailableChannelBuilder : CounterState : decrementFuture'),
                  onPressed: () => channel.counterState.decrementFuture()),
            ],
          ),
        ),
        SizedBox(height: 10),
        OwnChannelBuilder<MyChannel, MyChannelSignal>(
          channel: MyChannel(),
          condition: (channel, signal) => signal is CounterStateSignal,
          builder: (context, channel) => Column(
            mainAxisAlignment: MainAxisAlignment.center,
            mainAxisSize: MainAxisSize.max,
            children: <Widget>[
              Container(
                width: 50,
                height: 50,
                alignment: Alignment.center,
                child: channel.counterState.busy
                    ? Container(
                        width: 20,
                        height: 20,
                        child: CircularProgressIndicator(
                          strokeWidth: 2,
                        ))
                    : !channel.counterState.success
                        ? Text(channel.counterState.error)
                        : Text(channel.counterState.count.toString(),
                            style: TextStyle(fontSize: 25)),
              ),
              RaisedButton(
                  child: Text('OwnChannelBuilder : CounterState : increment'),
                  onPressed: () => channel.counterState.increment()),
              RaisedButton(
                  child: Text(
                      'OwnChannelBuilder : CounterState : decrementFuture'),
                  onPressed: () => channel.counterState.decrementFuture()),
            ],
          ),
        ),
        RaisedButton(
            child: Text('NotificationState : change '),
            onPressed: () => mychannel.notificationState.change()),
        RaisedButton(
            child: Text('NotificationState : changeFuture '),
            onPressed: () => mychannel.notificationState.changeFuture()),
        RaisedButton(
            child: Text('CounterState : increment'),
            onPressed: () => mychannel.counterState.increment()),
        RaisedButton(
            child: Text('CounterState : decrementFuture'),
            onPressed: () => mychannel.counterState.decrementFuture()),
        RaisedButton(
            child: Text('ColorState : changeColor'),
            onPressed: () => mychannel.colorState.changeColor()),
        RaisedButton(
            child: Text('ColorState : changeColorFuture'),
            onPressed: () => mychannel.colorState.changeColorFuture()),
      ]),
    );
  }
} 

This Post Has 3 Comments

  1. hasan

    hocam mail istemiştiniz.

  2. flutter_admin

    ok. Entegrasyonları hazırlıyorum yayında olunca mail atacağım. İlginize teşekkürler.

Bir cevap yazın