Flutter bloc 01 - quick calculator

A cat that can make coffee 2021-02-23 16:09:18
flutter bloc quick calculator


In this section, the target

  • Why use bloc
  • bloc vs provider
  • Learning route recommendation
  • install bloc vscode plug-in unit
  • To configure bloc Dependency package
  • Write calculator examples

video

www.bilibili.com/video/BV1ef…

Code

github.com/ducafecat/f…

Text

Why use bloc

  • State management ( It's a must )

  • Three level separation

    • The presentation layer (Presentation)
    • Business logic (Business Logic)
    • The data layer (Data)
      • data source / library (Repository)
      • Data provider (Data Provider)
  • Standardize group development

  • convenient test 、 Record User behavior

class SimpleBlocDelegate extends BlocDelegate {
@override
void onEvent(Bloc bloc, Object event) {
super.onEvent(bloc, event);
print('${bloc.runtimeType} $event');
// be-all UI event 
// You can use umeng Such a platform Tracking 
}
@override
void onError(Bloc bloc, Object error, StackTrace stacktrace) {
super.onError(bloc, error, stacktrace);
print('${bloc.runtimeType} $error');
// All the mistakes that happened 
// use sentry Recording errors 
}
@override
void onTransition(Bloc bloc, Transition transition) {
super.onTransition(bloc, transition);
print(transition);
// be-all State change 
}
}
 Copy code 

bloc vs provider

  • bloc It's a kind of mvvm be based on event 、 state Driven

  • provider It's method based

bloc Learning route

  • Flutter Bloc Quick start -> Stream -> Cubit -> Bloc

  • Understanding is based on mvvm Component splitting

install bloc vscode plug-in unit

bloc

Create project

  • pubspec.yaml
name: ducafecat_bloc_start_example
...
dependencies:
...
bloc: ^6.1.0
flutter_bloc: ^6.1.0
equatable: ^1.2.5
 Copy code 
  • Directory structure

counter Calculator business to create bloc view Catalog , So it's separated

To write bloc

  • lib/counter/bloc/counter_bloc.dart
import 'dart:async';
import 'package:bloc/bloc.dart';
import 'package:equatable/equatable.dart';
import 'package:meta/meta.dart';
part 'counter_event.dart';
part 'counter_state.dart';
class CounterBloc extends Bloc<CounterEvent, CounterState> {
CounterBloc() : super(CounterInitial(0));
int counterNum = 0;
@override
Stream<CounterState> mapEventToState(
CounterEvent event,
) async* {
if (event is CounterIncrement) {
yield* _mapIncrementEventToState(event);
} else if (event is CounterSubduction) {
yield* _mapSubductionEventToState(event);
}
}
Stream<CounterState> _mapIncrementEventToState(
CounterIncrement event) async* {
this.counterNum += 1;
yield CounterChange(this.counterNum);
}
Stream<CounterState> _mapSubductionEventToState(
CounterSubduction event) async* {
this.counterNum -= 1;
yield CounterChange(this.counterNum);
}
}
 Copy code 
  • lib/counter/bloc/counter_event.dart
part of 'counter_bloc.dart';
@immutable
abstract class CounterEvent extends Equatable {
@override
List<Object> get props => [];
}
class CounterIncrement extends CounterEvent {}
class CounterSubduction extends CounterEvent {}
 Copy code 
  • lib/counter/bloc/counter_state.dart
part of 'counter_bloc.dart';
@immutable
abstract class CounterState extends Equatable {
final int value;
const CounterState(this.value);
@override
List<Object> get props => [value];
}
class CounterInitial extends CounterState {
CounterInitial(int value) : super(value);
}
class CounterChange extends CounterState {
CounterChange(int value) : super(value);
}
 Copy code 

To write view

  • lib/counter/view/page.dart
import 'package:ducafecat_bloc_start_example/counter/bloc/counter_bloc.dart';
import 'package:ducafecat_bloc_start_example/counter/view/view.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
class CounterPage extends StatelessWidget {
const CounterPage({Key key}) : super(key: key);
@override
Widget build(BuildContext context) {
return BlocProvider(
create: (context) => CounterBloc(),
child: CounterView(),
);
}
}
 Copy code 
  • lib/counter/view/view.dart
import 'package:ducafecat_bloc_start_example/counter/bloc/counter_bloc.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
class CounterView extends StatelessWidget {
const CounterView({Key key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('Counter')),
body: Center(
child: Column(
children: [
BlocBuilder<CounterBloc, CounterState>(
builder: (context, state) {
return Text('${state.value}');
},
),
RaisedButton(
child: Text(' Add '),
onPressed: () {
BlocProvider.of<CounterBloc>(context).add(CounterIncrement());
},
),
RaisedButton(
child: Text(' Add '),
onPressed: () {
BlocProvider.of<CounterBloc>(context).add(CounterSubduction());
},
)
],
),
),
);
}
}
 Copy code 

Replace the main program widget

  • lib/main.dart
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
...
home: CounterPage(), //MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
 Copy code 

Reference resources


Cat elder brother

ducafecat.tech

ducafecat.gitee.io

版权声明
本文为[A cat that can make coffee]所创,转载请带上原文链接,感谢
https://qdmana.com/2021/02/20210223155148579i.html

  1. RxHttp - 轻量级、可扩展、易使用、完美兼容MVVM、MVC架构的网络封装类库
  2. Rxhttp - lightweight, extensible, easy to use, perfectly compatible with MVVM, MVC architecture network encapsulation class library
  3. Rxhttp - lightweight, extensible, easy to use, perfectly compatible with MVVM, MVC architecture network encapsulation class library
  4. 前端面试常考题:JS垃圾回收机制
  5. Frequently asked questions in front end interview: JS garbage collection mechanism
  6. Rxhttp - lightweight, extensible, easy to use, perfectly compatible with MVVM, MVC architecture network encapsulation class library
  7. Java之HTTP网络编程(一):TCP/SSL网页下载
  8. HTTP network programming in Java (1): TCP / SSL web page download
  9. Java之HTTP网络编程(一):TCP/SSL网页下载
  10. HTTP network programming in Java (1): TCP / SSL web page download
  11. 使用vite搭建vue项目
  12. 在组件中展示pdf文件:vue-pdf (由于未找到方法滑动加载下一页,只能点击加载下一页pdf文件)
  13. 在 vue 中通过 express 连接数据库
  14. Using vite to build Vue project
  15. Display PDF file in component: Vue pdf
  16. Connecting database through express in Vue
  17. 2021届秋招哈啰出行前端面经(一面)
  18. vue使用sdk进行七牛云上传
  19. Javascript性能优化【内联缓存】 V8引擎特性
  20. Small true wireless smart headset evaluation: put intelligence into the ear
  21. The front end experience of the 2021 autumn recruitment
  22. Vue uses SDK to upload Qi Niu cloud
  23. 深入理解 Web 协议 (三):HTTP 2
  24. dhtmlxGantt如何重新排序任务
  25. JavaScript performance optimization [inline cache] V8 engine features
  26. 深入理解 Web 协议 (三):HTTP 2
  27. Deep understanding of Web protocol (3): http 2
  28. 深入理解 Web 协议 (三):HTTP 2
  29. How dhtmlxgantt reorders tasks
  30. 深入理解 Web 协议 (三):HTTP 2
  31. JavaScriptBOM操作
  32. JavaScriptBOM操作
  33. Deep understanding of Web protocol (3): http 2
  34. Deep understanding of Web protocol (3): http 2
  35. dhtmlxGantt甘特图重新排序任视频教程
  36. vue实现七牛云上传图片功能
  37. vue.js环境配置步骤及npm run dev报错解决方案
  38. Deep understanding of Web protocol (3): http 2
  39. JavaScript BOM operation
  40. JavaScript BOM operation
  41. Dhtmlxgantt reordering video tutorial
  42. Vue to achieve seven cattle cloud upload image function
  43. vue.js Environment configuration steps and NPM run dev error reporting solution
  44. 什么是HTTPS以及如何实施HTTPS?
  45. vue使用sdk进行七牛上传
  46. JavaScript 邮箱验证 - 正则验证
  47. Codeless development platform cloud watch sets off a "new revolution" in enterprise Digitalization
  48. Codeless development of cloud watch for enterprise digital solution
  49. What is HTTPS and how to implement it?
  50. Vue uses SDK to upload seven cows
  51. JavaScript mailbox verification - regular verification
  52. JavaScriptBOM操作
  53. JavaScript BOM operation
  54. How to create a new NPM package and publish it to the NPM community
  55. vue --tinymce 解决上传图片的方法
  56. Vue development
  57. A simple solution for vite to batch introduce SVG symbol
  58. Building 3D rendering engine from scratch with JS (2)
  59. Vue -- tinymece solution to upload pictures
  60. Common configuration of nginx