Guest User

Untitled

a guest
Mar 27th, 2020
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 2.39 KB | None | 0 0
  1. import 'package:flutter/material.dart';
  2. import 'package:flutter_bloc/flutter_bloc.dart';
  3.  
  4.  
  5.  
  6. class CounterState {
  7.   int counter;
  8.   static final CounterState _state=CounterState._();
  9.   CounterState._(){
  10.     counter=0;
  11.   }
  12.  
  13.   factory CounterState() {
  14.     return _state;
  15.   }
  16. }
  17.  
  18. abstract class CounterEvent {}
  19.  
  20. class IncrementEvent extends CounterEvent {}
  21.  
  22. class DecrementEvent extends CounterEvent {}
  23.  
  24. class CounterBloc extends Bloc<CounterEvent, CounterState> {
  25.   @override
  26.   CounterState get initialState => CounterState();
  27.  
  28.   @override
  29.   Stream<CounterState> mapEventToState(CounterEvent event) async* {
  30.     if (event is IncrementEvent) {
  31.       yield state..counter +=1;
  32.     } else if (event.runtimeType == DecrementEvent) {
  33.       yield state..counter -= 1;
  34.     }
  35.   }
  36. }
  37.  
  38.  
  39. void main() => runApp(MyApp());
  40.  
  41. class MyApp extends StatelessWidget {
  42.   @override
  43.   Widget build(BuildContext context) {
  44.     return MaterialApp(
  45.       title: 'Flutter Demo',
  46.       home: BlocProvider<CounterBloc>(
  47.         create: (context) => CounterBloc(),
  48.         child: CounterPage(),
  49.       ),
  50.     );
  51.   }
  52. }
  53.  
  54. class CounterPage extends StatelessWidget {
  55.   @override
  56.   Widget build(BuildContext context) {
  57.     final CounterBloc counterBloc = BlocProvider.of<CounterBloc>(context);
  58.  
  59.     return Scaffold(
  60.       appBar: AppBar(title: Text('Counter')),
  61.       body: Center(
  62.         child: BlocBuilder<CounterBloc, CounterState>(
  63.           builder: (context, count) {
  64.             return Text(
  65.               '${count.counter}',
  66.               style: TextStyle(fontSize: 24.0),
  67.             );
  68.           },
  69.         ),
  70.       ),
  71.       floatingActionButton: Column(
  72.         crossAxisAlignment: CrossAxisAlignment.end,
  73.         mainAxisAlignment: MainAxisAlignment.end,
  74.         children: <Widget>[
  75.           Padding(
  76.             padding: EdgeInsets.symmetric(vertical: 5.0),
  77.             child: FloatingActionButton(
  78.               child: Icon(Icons.add),
  79.               onPressed: () {
  80.                 counterBloc.add(IncrementEvent());
  81.               },
  82.             ),
  83.           ),
  84.           Padding(
  85.             padding: EdgeInsets.symmetric(vertical: 5.0),
  86.             child: FloatingActionButton(
  87.               child: Icon(Icons.remove),
  88.               onPressed: () {
  89.                 counterBloc.add(DecrementEvent());
  90.               },
  91.             ),
  92.           ),
  93.         ],
  94.       ),
  95.     );
  96.   }
  97. }
Advertisement
Add Comment
Please, Sign In to add comment