Flutter State Yönetimine Giriş 3: Signal​ Paketi Nasıl Kullanılır Video Anlatım

Flutter State Yönetimine Giriş 3: Signal​ Paketi Nasıl Kullanılır Video Anlatım

Flutter State Yönetimine Giriş 3: Signal Paketi Nasıl Kullanılır Video Anlatım

Video için tıklayın…

GitHub : https://github.com/XPersPective/flutterdersleri.com

Signal Paket Sayfası

//globalKanal.dart
import 'package:signal/signal.dart';
abstract class GlobalKanalSinyal extends ChannelSignal {}
class GlobalKanal extends StateChannel<GlobalKanalSinyal> {
GlobalKanal(){
  _counterState = CounterState(()=>add(CounterStateSinyal()));
  _bildirimState = BildirimState(()=> add(BildirimStateSinyal()));
}
//CounterStateSinyal
CounterState _counterState;
CounterState get counterState =>_counterState;
//BildirimStateSinyal
BildirimState _bildirimState;
BildirimState get bildirimState => _bildirimState;
@override
  initState() {
 _counterState.initState();
 _bildirimState.initState();
  }
@override
dispose(){
_counterState.dispose();
_bildirimState.dispose();
  super.dispose();
}
}
class CounterStateSinyal extends GlobalKanalSinyal {}
class CounterState extends BaseState {
  CounterState(void Function() onStateChanged) : super(onStateChanged);
  int _count;
  int get count => _count;
  artir() {
    _count = _count + 1;
    doneSucces();
  }
  azaltFuture() async {
    wait();
    try {
      await Future.delayed(Duration(seconds: 2));
      _count = _count - 1;
      doneSucces();
    } catch (e) {
      doneError(e.toString());
    }
  }
  @override
  initState() {
    _count = 0;
  }
  @override
  dispose() {}
}
class BildirimStateSinyal extends GlobalKanalSinyal {}
class BildirimState extends BaseState {
  BildirimState(void Function() onStateChanged) : super(onStateChanged);
  bool _acikmi;
  bool get acikmi => _acikmi;
  degistir() {
    _acikmi = !_acikmi;
    doneSucces();
  }
 Future<void> degistirFuture() async {
    wait();
    try {
      await Future.delayed(Duration(seconds: 2));
      _acikmi = !_acikmi;
      doneSucces();
    } catch (e) {
      doneError(e.toString());
    }
  }
@override
  dispose() {
      // super.dispose();
  }
  @override
  initState() {
_acikmi =false;
  }
} 
//main.dart
import 'package:flutter/material.dart';
import 'package:signal/signal.dart';
import 'package:signal_flutterdersleri_com/globalKanal.dart';
void main() {
  runApp(MyApp());
}
class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return AncestorChannelProvider<GlobalKanal>(
        channel: GlobalKanal()..initState(),
        child: MaterialApp(
          home: HomePage(),
        ));
  }
}
class HomePage extends StatefulWidget {
  @override
  _HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('Signal State Yönetim Paketi Örnek')),
      body: ListView(
        children: <Widget>[
          SizedBox(
            height: 50,
          ),
          Center(
            child: AncestorChannelBuilder<GlobalKanal, GlobalKanalSinyal>(
                condition: (channel, signal) => signal is CounterStateSinyal,
                builder: (context, channel) {
                  final state = channel.counterState;
                  return state.busy
                      ? CircularProgressIndicator()
                      : !state.success
                          ? Text(state.error)
                          : Text(state.count.toString(),
                              style: TextStyle(fontSize: 25));
                }),
          ),
          SizedBox(
            height: 50,
          ),
          AncestorChannelBuilder<GlobalKanal, GlobalKanalSinyal>(
              condition: (channel, signal) => signal is BildirimStateSinyal,
              builder: (context, channel) {
                final state = channel.bildirimState;
                return Stack(
                  alignment: Alignment.topCenter,
                  children: <Widget>[
                    RaisedButton(
                      child: Text(state.acikmi
                          ? 'Bildirim Durumu: Açık'
                          : 'Bildirim Durumu: Kapalı'),
                      onPressed: state.busy ? null : () => state.degistir(),
                    ),
                    if (state.busy) CircularProgressIndicator(),
                  ],
                );
              }),
          SizedBox(
            height: 50,
          ),
          RaisedButton(
            child: Text('CounterState: artır'),
            onPressed: () => AncestorChannelProvider.of<GlobalKanal>(context)
                .counterState
                .artir(),
          ),
          RaisedButton(
            child: Text('CounterState: azaltFuture'),
            onPressed: () => AncestorChannelProvider.of<GlobalKanal>(context)
                .counterState
                .azaltFuture(),
          ),
          RaisedButton(
            child: Text('BildirimState: degistir'),
            onPressed: () => AncestorChannelProvider.of<GlobalKanal>(context)
                .bildirimState
                .degistir(),
          ),
          RaisedButton(
            child: Text('BildirimState: degistirFuture'),
            onPressed: () => AncestorChannelProvider.of<GlobalKanal>(context)
                .bildirimState
                .degistirFuture(),
          ),
             RaisedButton(
            child: Text('Diğer Sayfaya git'),
            onPressed: () => Navigator.of(context).push(MaterialPageRoute(builder: (context)=>DigerSayfa())),
          ),
        ],
      ),
    );
  }
}
class DigerSayfa extends StatefulWidget {
  @override
  _DigerSayfaState createState() => _DigerSayfaState();
}
class _DigerSayfaState extends State<DigerSayfa> {
GlobalKanal _globalKanal;
GlobalKanal get globalKanal =>_globalKanal;
 @override
  void initState() {
    super.initState();
    _globalKanal = GlobalKanal();
    _globalKanal.initState();
  }
@override
  void dispose() {
 _globalKanal.dispose();
    super.dispose();
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('Signal State Yönetim Paketi Örnek: DigerSayfa')),
      body: ListView(
        children: <Widget>[
          SizedBox(
            height: 50,
          ),
          Center(
            child: AvailableChannelBuilder<GlobalKanal, GlobalKanalSinyal>(
              channel: _globalKanal,
                condition: (channel, signal) => signal is CounterStateSinyal,
                builder: (context, channel) {
                  final state = channel.counterState;
                  return state.busy
                      ? CircularProgressIndicator()
                      : !state.success
                          ? Text(state.error)
                          : Text(state.count.toString(),
                              style: TextStyle(fontSize: 25));
                }),
          ),
          SizedBox(
            height: 50,
          ),
          AvailableChannelBuilder<GlobalKanal, GlobalKanalSinyal>(
             channel: _globalKanal,
              condition: (channel, signal) => signal is BildirimStateSinyal,
              builder: (context, channel) {
                final state = channel.bildirimState;
                return Stack(
                  alignment: Alignment.topCenter,
                  children: <Widget>[
                    RaisedButton(
                      child: Text(state.acikmi
                          ? 'Bildirim Durumu: Açık'
                          : 'Bildirim Durumu: Kapalı'),
                      onPressed: state.busy ? null : () => state.degistir(),
                    ),
                    if (state.busy) CircularProgressIndicator(),
                  ],
                );
              }),
          SizedBox(
            height: 50,
          ),
          RaisedButton(
            child: Text('CounterState: artır'),
            onPressed: () =>_globalKanal
                .counterState
                .artir(),
          ),
          RaisedButton(
            child: Text('CounterState: azaltFuture'),
            onPressed: () => _globalKanal
                .counterState
                .azaltFuture(),
          ),
          RaisedButton(
            child: Text('BildirimState: degistir'),
            onPressed: () =>_globalKanal
                .bildirimState
                .degistir(),
          ),
          RaisedButton(
            child: Text('BildirimState: degistirFuture'),
            onPressed: ()  =>_globalKanal
                .bildirimState
                .degistirFuture(),
          ),
        ],
      ),
    );
  }
} 

This Post Has One Comment

  1. flutterAdmin

    versiyon güncellendi. Yeni versiyona uygun kullanımı görmek için lütfen signal paket sayfasına bakın.

Bir cevap yazın