Skip to content

Commit 0c80891

Browse files
authored
Merge pull request #18 from FlutterOpen/dev
feat(): 命令模式的完成
2 parents b347089 + 58640bc commit 0c80891

5 files changed

Lines changed: 179 additions & 0 deletions

File tree

lib/constant/string_const.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,7 @@ class StringConst {
4343

4444
//享元模式
4545
static const String DUTY_ = "责任链模式";
46+
47+
//命令模式
48+
static const String COMMAND_ = "责任链模式";
4649
}

lib/main.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import 'package:flutter_design/constant/string_const.dart';
33
import 'package:flutter_design/page/adapter/adapter_page.dart';
44
import 'package:flutter_design/page/bridge/bridge_page.dart';
55
import 'package:flutter_design/page/combination/combination_page.dart';
6+
import 'package:flutter_design/page/command/command_page.dart';
67
import 'package:flutter_design/page/decorator/decorator_page.dart';
78
import 'package:flutter_design/page/duty/duty_page.dart';
89
import 'package:flutter_design/page/facade/facade_page.dart';
@@ -41,6 +42,7 @@ class MyApp extends StatelessWidget {
4142
PageConst.FLYWEIGHT_PAGE: (context) => FlyweightPage(),
4243
PageConst.PROXY_PAGE: (context) => ProxyPage(),
4344
PageConst.DUTY_PAGE: (context) => DutyPage(),
45+
PageConst.COMMAND_PAGE: (context) => CommandPage(),
4446
},
4547
);
4648
}

lib/page/command/command_mode.dart

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import 'package:flutter/cupertino.dart';
2+
3+
/// Created by NieBin on 2020-03-31
4+
/// Github: https://github.com/nb312
5+
/// Email: niebin312@gmail.com
6+
//接收角色
7+
class AudioPlayer {
8+
void play() {
9+
debugPrint("播放音乐");
10+
}
11+
12+
void stop() {
13+
debugPrint("停止播放音乐");
14+
}
15+
16+
void pause() {
17+
debugPrint("暂停播放音乐");
18+
}
19+
20+
void nextPlay() {
21+
debugPrint("播放下一首");
22+
}
23+
}
24+
25+
//命令角色
26+
abstract class Command {
27+
void execute();
28+
}
29+
30+
class PlayCommand implements Command {
31+
AudioPlayer player;
32+
33+
PlayCommand(this.player);
34+
35+
@override
36+
void execute() {
37+
player?.play();
38+
}
39+
}
40+
41+
class StopCommand implements Command {
42+
AudioPlayer player;
43+
44+
StopCommand(this.player);
45+
46+
@override
47+
void execute() {
48+
player?.stop();
49+
}
50+
}
51+
52+
class PauseCommand implements Command {
53+
AudioPlayer player;
54+
55+
PauseCommand(this.player);
56+
57+
@override
58+
void execute() {
59+
player?.pause();
60+
}
61+
}
62+
63+
class NextCommand implements Command {
64+
AudioPlayer player;
65+
66+
NextCommand(this.player);
67+
68+
@override
69+
void execute() {
70+
player?.nextPlay();
71+
}
72+
}
73+
74+
//手势调用命令
75+
class PointerInvoker {
76+
Command _command;
77+
78+
set command(Command command) => _command = command;
79+
80+
void execute() {
81+
_command.execute();
82+
}
83+
}

lib/page/command/command_page.dart

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
import 'package:flutter/material.dart';
2+
import 'package:flutter_design/constant/string_const.dart';
3+
import 'package:flutter_design/page/command/command_mode.dart';
4+
5+
/// Created by NieBin on 2020-03-31
6+
/// Github: https://github.com/nb312
7+
/// Email: niebin312@gmail.com
8+
9+
class CommandPage extends StatefulWidget {
10+
@override
11+
_CommandPageState createState() => _CommandPageState();
12+
}
13+
14+
class _CommandPageState extends State<CommandPage> {
15+
AudioPlayer _player;
16+
17+
@override
18+
initState() {
19+
super.initState();
20+
_player = AudioPlayer();
21+
}
22+
23+
@override
24+
Widget build(BuildContext context) {
25+
return Scaffold(
26+
appBar: AppBar(
27+
title: Text(StringConst.COMMAND_),
28+
),
29+
body: Container(
30+
alignment: Alignment.center,
31+
child: Row(
32+
crossAxisAlignment: CrossAxisAlignment.center,
33+
mainAxisAlignment: MainAxisAlignment.center,
34+
children: <Widget>[
35+
_button(
36+
icon: Icons.pause,
37+
callback: () {
38+
PointerInvoker invoker = PointerInvoker();
39+
invoker.command = PauseCommand(_player);
40+
invoker.execute();
41+
},
42+
color: Colors.red),
43+
_button(
44+
icon: Icons.stop,
45+
callback: () {
46+
PointerInvoker invoker = PointerInvoker();
47+
invoker.command = StopCommand(_player);
48+
invoker.execute();
49+
},
50+
color: Colors.red),
51+
_button(
52+
icon: Icons.play_arrow,
53+
callback: () {
54+
PointerInvoker invoker = PointerInvoker();
55+
invoker.command = PlayCommand(_player);
56+
invoker.execute();
57+
},
58+
color: Colors.red),
59+
_button(
60+
icon: Icons.skip_next,
61+
callback: () {
62+
PointerInvoker invoker = PointerInvoker();
63+
invoker.command = NextCommand(_player);
64+
invoker.execute();
65+
},
66+
color: Colors.green),
67+
],
68+
),
69+
),
70+
);
71+
}
72+
73+
Widget _button({
74+
IconData icon,
75+
VoidCallback callback,
76+
Color color = Colors.white,
77+
}) =>
78+
FlatButton(
79+
color: color,
80+
child: Padding(
81+
padding: const EdgeInsets.all(10.0),
82+
child: Icon(
83+
icon,
84+
size: 30,
85+
),
86+
),
87+
onPressed: callback,
88+
shape: CircleBorder(),
89+
);
90+
}

lib/page/home_page.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ Map<String, String> map = {
2828
PageConst.FLYWEIGHT_PAGE: StringConst.FLYWEIGHT_,
2929
PageConst.PROXY_PAGE: StringConst.PROXY_,
3030
PageConst.DUTY_PAGE: StringConst.DUTY_,
31+
PageConst.COMMAND_PAGE: StringConst.COMMAND_,
3132
};
3233

3334
class _HomePageState extends State<HomePage> {

0 commit comments

Comments
 (0)