Skip to content

Commit

Permalink
Merge pull request #5 from CopyPaste-Orbital2022/toolbar
Browse files Browse the repository at this point in the history
added basic drawing features
  • Loading branch information
wxxedu authored May 16, 2022
2 parents 0bb59aa + 924e6f5 commit c2910fe
Show file tree
Hide file tree
Showing 13 changed files with 94 additions and 118 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "plugins/scribble"]
path = plugins/scribble
url = https://github.com/CopyPaste-Orbital2022/scribble.git
29 changes: 18 additions & 11 deletions lib/core/injections/injection.config.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/core/injections/injection.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import 'injection.config.dart';

final getIt = GetIt.instance;

@injectableInit
@InjectableInit()
Future<void> configureInjection(String env) async {
await $initGetIt(getIt, environment: env);
}
8 changes: 8 additions & 0 deletions lib/core/injections/scribble_injection.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import 'package:injectable/injectable.dart';
import 'package:scribble/scribble.dart';

@module
abstract class ScribbleInjectionModule {
@lazySingleton
ScribbleNotifier get notifier => ScribbleNotifier();
}
2 changes: 1 addition & 1 deletion lib/features/drawing/data/models/pen_state_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class PenStateModel {
final int currentColorIdx;
static const int defaultCurrentColorIdx = 0;
final List<String> colors;
static const List<String> defaultColors = ["0x0000000000"];
static const List<String> defaultColors = ["0xFF00000000"];
final bool useStylus;
static const bool defaultUseStylus = false;
final double width;
Expand Down
6 changes: 6 additions & 0 deletions lib/features/drawing/domain/entities/pen_state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,9 @@ class PenState with _$PenState {
required double width,
}) = _PenState;
}

extension PenStateX on PenState {
Color getCurrentColor() {
return colors[currentColorIdx];
}
}
12 changes: 11 additions & 1 deletion lib/features/drawing/presentation/bloc/drawing_bloc.dart
Original file line number Diff line number Diff line change
@@ -1,17 +1,27 @@
import 'package:bloc/bloc.dart';
import 'package:copypaste/features/drawing/domain/entities/pen_state.dart';
import 'package:copypaste/features/drawing/domain/entities/selectable_tools.dart';
import 'package:copypaste/features/drawing/domain/repositories/i_drawing_toolbar_repository.dart';
import 'package:copypaste/features/drawing/presentation/bloc/index.dart';
import 'package:injectable/injectable.dart';
import 'package:scribble/scribble.dart';

@LazySingleton()
class DrawingBloc extends Bloc<DrawingEvent, DrawingState> {
final IDrawingToolBarRepository _repository;
DrawingBloc(this._repository) : super(DrawingStateX.initialState()) {
final ScribbleNotifier _notifier;
DrawingBloc(this._repository, this._notifier)
: super(DrawingStateX.initialState()) {
on<ChangeSelectedDrawingButtonEvent>((event, emit) {
_repository.saveCurrentTool(event.tool);
emit(
state.copyWith(currentTool: event.tool),
);
if (event.tool == DrawingTool.eraser) {
_notifier.setEraser();
} else if (event.tool == DrawingTool.pen) {
_notifier.setPen();
} else if (event.tool == DrawingTool.hand) {}
});
}
}
1 change: 1 addition & 0 deletions lib/features/drawing/presentation/bloc/drawing_state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import 'package:copypaste/features/drawing/domain/entities/pen_state.dart';
import 'package:copypaste/features/drawing/domain/entities/selectable_tools.dart';
import 'package:copypaste/features/drawing/domain/repositories/i_drawing_toolbar_repository.dart';
import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:scribble/scribble.dart';

part 'drawing_state.freezed.dart';

Expand Down
35 changes: 35 additions & 0 deletions lib/features/drawing/presentation/pages/drawing_page.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import 'package:copypaste/core/injections/injection.dart';
import 'package:copypaste/features/drawing/domain/entities/selectable_tools.dart';
import 'package:copypaste/features/drawing/presentation/bloc/index.dart';
import 'package:copypaste/features/drawing/presentation/widgets/drawing_button_menu.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:scribble/scribble.dart';

class DrawingPage extends StatelessWidget {
const DrawingPage({Key? key}) : super(key: key);

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
actions: const [
DrawingButtonMenu(),
],
),
body: BlocProvider(
create: (context) => getIt<DrawingBloc>(),
child: BlocConsumer<DrawingBloc, DrawingState>(
builder: (context, state) {
return Scribble(
notifier: getIt<ScribbleNotifier>(),
drawPen: state.currentTool == DrawingTool.pen,
drawEraser: state.currentTool == DrawingTool.eraser,
);
},
listener: (context, state) {},
),
),
);
}
}
102 changes: 3 additions & 99 deletions lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:copypaste/core/injections/injection.dart';
import 'package:copypaste/features/drawing/presentation/pages/drawing_page.dart';
import 'package:copypaste/features/drawing/presentation/widgets/drawing_button_menu.dart';
import 'package:flutter/material.dart';
import 'package:injectable/injectable.dart';
Expand All @@ -16,108 +17,11 @@ class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
title: 'CopyPaste',
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: const MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}

class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key, required 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
State<MyHomePage> 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),
actions: const [
DrawingButtonMenu(),
],
),
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>[
const Text(
'You have pushed the button this many times:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.headline4,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: const Icon(Icons.add),
), // This trailing comma makes auto-formatting nicer for build methods.
home: const DrawingPage(),
);
}
}
1 change: 1 addition & 0 deletions plugins/scribble
Submodule scribble added at 210f85
8 changes: 4 additions & 4 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -488,10 +488,10 @@ packages:
scribble:
dependency: "direct main"
description:
name: scribble
url: "https://pub.dartlang.org"
source: hosted
version: "0.4.0"
path: "plugins/scribble"
relative: true
source: path
version: "0.3.1"
shared_preferences:
dependency: "direct main"
description:
Expand Down
3 changes: 2 additions & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ dependencies:
freezed_annotation: ^2.0.3
get_it: ^7.2.0
injectable: ^1.5.3
scribble: ^0.4.0
scribble:
path: plugins/scribble
shared_preferences: ^2.0.13

dev_dependencies:
Expand Down

0 comments on commit c2910fe

Please sign in to comment.