Search
 
SCRIPT & CODE EXAMPLE
 

DART

flutter counter app with block library

import 'package:equatable/equatable.dart';
import 'package:flutter/material.dart';
import 'dart:async';

import 'package:bloc/bloc.dart';

import 'package:flutter/foundation.dart';
import 'package:flutter_bloc/flutter_bloc.dart';

abstract class CounterEvent extends Equatable {
  const CounterEvent();

  @override
  List<Object> get props => [];
}

class IncrementEvent extends CounterEvent {}

class DecrementEvent extends CounterEvent {}

class CounterState extends Equatable {
  final int counter;
  const CounterState({@required this.counter});

  @override
  List<Object> get props => [counter];
}

class CounterBloc extends Bloc<CounterEvent, CounterState> {
  CounterBloc() : super(CounterState(counter: 0));

  @override
  Stream<CounterState> mapEventToState(CounterEvent event) async* {
    print(event.toString());
    if (event is IncrementEvent) {
      print("inc");
      yield CounterState(counter: state.counter + 1);
    } else if (state is DecrementEvent) {
      yield CounterState(counter: state.counter - 1);
    }
  }
}

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      home: BlocProvider<CounterBloc>(
        create: (context) => CounterBloc(),
        child: Home(
          title: "test",
        ),
      ),
    );
  }
}

class Home extends StatefulWidget {
  Home({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _HomeState createState() => _HomeState();
}

class _HomeState extends State<Home> {
  @override
  Widget build(BuildContext context) {
    final CounterBloc counterBloc = BlocProvider.of<CounterBloc>(context);
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: BlocBuilder<CounterBloc, CounterState>(
        //cubit: counterBloc,
        builder: (context, state) {
          return Center(
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                Text(
                  'You have pushed the button this many times:',
                ),
                Text(
                  state.counter.toString(),
                  style: Theme.of(context).textTheme.headline4,
                ),
              ],
            ),
          );
        },
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          print("click");
          counterBloc.add(IncrementEvent());
        },
        tooltip: 'Increment',
        child: Icon(Icons.add),
      ),
    );
  }
}
Comment

PREVIOUS NEXT
Code Example
Dart :: add fullscreen modal on a page in flutter app 
Dart :: dart contains method 
Dart :: flutter conver string to inr 
Dart :: lifecycle methods flutter 
Dart :: get avarae image from like flutter 
Dart :: http dart 
Dart :: flutter custom error widget 
Dart :: Array of colors in dart 
Dart :: dart if else 
Dart :: dart remove from list 
Dart :: Avoid `print` calls in production code 
Dart :: flutter variables 
Dart :: how to hide the keyboard in flutter 
Dart :: create a row with two child in flutter 
Dart :: difference between hot reload and hot restart in flutter 
Dart :: flutter showmodal initstate 
Dart :: onpressed flutter calculate 
Dart :: how to mesure execution time of method in dart 
Dart :: proportion in flutter 
Dart :: ordered/numbered list in flutter 
Dart :: dart get href attribute 
Dart :: flutter column width 
Swift :: time formats swift 
Swift :: swift array to string 
Swift :: unrecognized font family Helvetica-Regular 
Swift :: get item filter count swift 
Swift :: iOS 10.3.1 Simulator manually 
Swift :: prevent iphone to sleep swift 
Swift :: append new element to dictionary in swift 
Swift :: swift date difference in days 
ADD CONTENT
Topic
Content
Source link
Name
9+2 =