Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
C
counter
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
epx-flutter
counter
Commits
55c7a838
提交
55c7a838
authored
8月 08, 2019
作者:
王健
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
完成
上级
5b5e2b34
隐藏空白字符变更
内嵌
并排
正在显示
7 个修改的文件
包含
126 行增加
和
97 行删除
+126
-97
README.md
README.md
+5
-1
counter_bloc.dart
lib/counter/counter_bloc.dart
+21
-0
counter_event.dart
lib/counter/counter_event.dart
+8
-0
main.dart
lib/main.dart
+8
-95
counter_page.dart
lib/view/counter_page.dart
+52
-0
pubspec.lock
pubspec.lock
+29
-1
pubspec.yaml
pubspec.yaml
+3
-0
没有找到文件。
README.md
浏览文件 @
55c7a838
...
...
@@ -4,7 +4,9 @@
## Getting Started
pubspec.yaml文件
修改pubspec.yaml文件引入相关包
> 不写版本号会自动引入最新的版本,建议写版本号,防止插件更新后下载的包不一致,导致项目无法运行.
```
name: flutter_counter
description: A new Flutter project.
...
...
@@ -32,3 +34,4 @@ flutter:
flutter packages get
```
## 创建CounterBloc
\ No newline at end of file
lib/counter/counter_bloc.dart
0 → 100644
浏览文件 @
55c7a838
import
'package:bloc/bloc.dart'
;
import
'counter_event.dart'
;
// CounterBloc 就是在将CounterEvent事件转化成int状态
class
CounterBloc
extends
Bloc
<
CounterEvent
,
int
>
{
@override
int
get
initialState
=>
0
;
@override
Stream
<
int
>
mapEventToState
(
CounterEvent
event
)
async
*
{
switch
(
event
)
{
case
CounterEvent
.
decrement
:
yield
currentState
-
1
;
break
;
case
CounterEvent
.
increment
:
yield
currentState
+
1
;
break
;
}
}
}
\ No newline at end of file
lib/counter/counter_event.dart
0 → 100644
浏览文件 @
55c7a838
/// 计数器的事件类型
/// increment 增加事件
/// decrement 减少事件
enum
CounterEvent
{
increment
,
decrement
}
\ No newline at end of file
lib/main.dart
浏览文件 @
55c7a838
import
'package:flutter/material.dart'
;
import
'package:flutter_bloc/flutter_bloc.dart'
;
import
'./view/counter_page.dart'
;
import
'./counter/counter_bloc.dart'
;
void
main
(
)
=>
runApp
(
MyApp
());
class
MyApp
extends
StatelessWidget
{
// This widget is the root of your application.
@override
Widget
build
(
BuildContext
context
)
{
return
MaterialApp
(
title:
'Flutter Demo'
,
theme:
ThemeData
(
// This is the theme of your application.
//
// Try running your application with "flutter run". You'll see the
// application has a blue toolbar. Then, without quitting the app, try
// changing the primarySwatch below to Colors.green and then invoke
// "hot reload" (press "r" in the console where you ran "flutter run",
// or simply save your changes to "hot reload" in a Flutter IDE).
// Notice that the counter didn't reset back to zero; the application
// is not restarted.
primarySwatch:
Colors
.
blue
,
),
home:
MyHomePage
(
title:
'Flutter Demo Home Page'
),
);
}
}
class
MyHomePage
extends
StatefulWidget
{
MyHomePage
({
Key
key
,
this
.
title
})
:
super
(
key:
key
);
// This widget is the home page of your application. It is stateful, meaning
// that it has a State object (defined below) that contains fields that affect
// how it looks.
// This class is the configuration for the state. It holds the values (in this
// case the title) provided by the parent (in this case the App widget) and
// used by the build method of the State. Fields in a Widget subclass are
// always marked "final".
final
String
title
;
@override
_MyHomePageState
createState
()
=>
_MyHomePageState
();
}
class
_MyHomePageState
extends
State
<
MyHomePage
>
{
int
_counter
=
0
;
void
_incrementCounter
()
{
setState
(()
{
// This call to setState tells the Flutter framework that something has
// changed in this State, which causes it to rerun the build method below
// so that the display can reflect the updated values. If we changed
// _counter without calling setState(), then the build method would not be
// called again, and so nothing would appear to happen.
_counter
++;
});
}
@override
Widget
build
(
BuildContext
context
)
{
// This method is rerun every time setState is called, for instance as done
// by the _incrementCounter method above.
//
// The Flutter framework has been optimized to make rerunning build methods
// fast, so that you can just rebuild anything that needs updating rather
// than having to individually change instances of widgets.
return
Scaffold
(
appBar:
AppBar
(
// Here we take the value from the MyHomePage object that was created by
// the App.build method, and use it to set our appbar title.
title:
Text
(
widget
.
title
),
),
body:
Center
(
// Center is a layout widget. It takes a single child and positions it
// in the middle of the parent.
child:
Column
(
// Column is also a layout widget. It takes a list of children and
// arranges them vertically. By default, it sizes itself to fit its
// children horizontally, and tries to be as tall as its parent.
//
// Invoke "debug painting" (press "p" in the console, choose the
// "Toggle Debug Paint" action from the Flutter Inspector in Android
// Studio, or the "Toggle Debug Paint" command in Visual Studio Code)
// to see the wireframe for each widget.
//
// Column has various properties to control how it sizes itself and
// how it positions its children. Here we use mainAxisAlignment to
// center the children vertically; the main axis here is the vertical
// axis because Columns are vertical (the cross axis would be
// horizontal).
mainAxisAlignment:
MainAxisAlignment
.
center
,
children:
<
Widget
>[
Text
(
'You have pushed the button this many times:'
,
),
Text
(
'
$_counter
'
,
style:
Theme
.
of
(
context
).
textTheme
.
display1
,
),
],
),
home:
BlocProvider
<
CounterBloc
>(
builder:
(
context
)
=>
CounterBloc
(),
child:
CounterPage
(),
),
floatingActionButton:
FloatingActionButton
(
onPressed:
_incrementCounter
,
tooltip:
'Increment'
,
child:
Icon
(
Icons
.
add
),
),
// This trailing comma makes auto-formatting nicer for build methods.
);
}
}
lib/view/counter_page.dart
0 → 100644
浏览文件 @
55c7a838
import
'package:counter/counter/counter_bloc.dart'
;
import
'package:counter/counter/counter_event.dart'
;
import
'package:flutter/material.dart'
;
import
'package:flutter_bloc/flutter_bloc.dart'
;
class
CounterPage
extends
StatelessWidget
{
@override
Widget
build
(
BuildContext
context
)
{
final
CounterBloc
_counterBloc
=
BlocProvider
.
of
<
CounterBloc
>(
context
);
return
Scaffold
(
appBar:
AppBar
(
backgroundColor:
Colors
.
black26
,
elevation:
0
,
title:
Text
(
'Bloc计数器'
),
),
body:
BlocBuilder
<
CounterBloc
,
int
>(
bloc:
_counterBloc
,
builder:
(
BuildContext
context
,
int
count
)
{
return
Center
(
child:
Text
(
'
$count
'
,
style:
TextStyle
(
fontSize:
24.0
),
),
);
},
),
floatingActionButton:
Column
(
mainAxisAlignment:
MainAxisAlignment
.
end
,
crossAxisAlignment:
CrossAxisAlignment
.
end
,
children:
<
Widget
>[
FloatingActionButton
(
onPressed:
()
{
_counterBloc
.
dispatch
(
CounterEvent
.
increment
);
},
backgroundColor:
Colors
.
green
,
child:
Icon
(
Icons
.
add
),
),
SizedBox
(
height:
10.0
,
),
FloatingActionButton
(
onPressed:
()
{
_counterBloc
.
dispatch
(
CounterEvent
.
decrement
);
},
child:
Icon
(
Icons
.
remove
),
backgroundColor:
Colors
.
pink
,
)
],
));
}
}
pubspec.lock
浏览文件 @
55c7a838
...
...
@@ -8,6 +8,13 @@ packages:
url: "https://pub.flutter-io.cn"
source: hosted
version: "2.2.0"
bloc:
dependency: transitive
description:
name: bloc
url: "https://pub.flutter-io.cn"
source: hosted
version: "0.14.4"
boolean_selector:
dependency: transitive
description:
...
...
@@ -41,6 +48,13 @@ packages:
description: flutter
source: sdk
version: "0.0.0"
flutter_bloc:
dependency: "direct main"
description:
name: flutter_bloc
url: "https://pub.flutter-io.cn"
source: hosted
version: "0.20.0"
flutter_test:
dependency: "direct dev"
description: flutter
...
...
@@ -54,7 +68,7 @@ packages:
source: hosted
version: "0.12.5"
meta:
dependency:
transitive
dependency:
"direct main"
description:
name: meta
url: "https://pub.flutter-io.cn"
...
...
@@ -74,6 +88,13 @@ packages:
url: "https://pub.flutter-io.cn"
source: hosted
version: "1.8.0+1"
provider:
dependency: transitive
description:
name: provider
url: "https://pub.flutter-io.cn"
source: hosted
version: "3.0.0+1"
quiver:
dependency: transitive
description:
...
...
@@ -81,6 +102,13 @@ packages:
url: "https://pub.flutter-io.cn"
source: hosted
version: "2.0.3"
rxdart:
dependency: transitive
description:
name: rxdart
url: "https://pub.flutter-io.cn"
source: hosted
version: "0.22.1+1"
sky_engine:
dependency: transitive
description: flutter
...
...
pubspec.yaml
浏览文件 @
55c7a838
...
...
@@ -23,6 +23,9 @@ dependencies:
# The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons.
cupertino_icons
:
^0.1.2
flutter_bloc
:
meta
:
dev_dependencies
:
flutter_test
:
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论