Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove Provider package dependency #16

Merged
merged 3 commits into from Nov 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion example/lib/main.dart
Expand Up @@ -19,7 +19,7 @@ class MyApp extends StatelessWidget {
}

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

final String title;

Expand Down
15 changes: 0 additions & 15 deletions example/pubspec.lock
Expand Up @@ -81,27 +81,13 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "1.7.0"
nested:
dependency: transitive
description:
name: nested
url: "https://pub.dartlang.org"
source: hosted
version: "1.0.0"
path:
dependency: transitive
description:
name: path
url: "https://pub.dartlang.org"
source: hosted
version: "1.8.0"
provider:
dependency: transitive
description:
name: provider
url: "https://pub.dartlang.org"
source: hosted
version: "6.0.1"
sky_engine:
dependency: transitive
description: flutter
Expand Down Expand Up @@ -172,4 +158,3 @@ packages:
version: "2.1.0"
sdks:
dart: ">=2.12.0 <3.0.0"
flutter: ">=1.16.0"
3 changes: 1 addition & 2 deletions example/pubspec.yaml
Expand Up @@ -18,7 +18,7 @@ publish_to: 'none' # Remove this line if you wish to publish to pub.dev
version: 1.0.0+1

environment:
sdk: ">=2.7.0 <3.0.0"
sdk: ">=2.12.0 <3.0.0"

dependencies:
flutter:
Expand Down Expand Up @@ -117,4 +117,3 @@ flutter:
- family: VeganStyle
fonts:
- asset: fonts/VeganStyle.ttf

35 changes: 16 additions & 19 deletions lib/src/widget/color_palette.dart
@@ -1,6 +1,5 @@
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:text_editor/src/text_style_model.dart';
import 'package:text_editor/text_editor_data.dart';

class ColorPalette extends StatefulWidget {
final List<Color> colors;
Expand All @@ -14,26 +13,25 @@ class ColorPalette extends StatefulWidget {
class _ColorPaletteState extends State<ColorPalette> {
@override
Widget build(BuildContext context) {
final textStyleModel = TextEditorData.of(context).textStyleModel;
return Container(
child: SingleChildScrollView(
scrollDirection: Axis.horizontal,
child: Row(
children: [
Consumer<TextStyleModel>(
builder: (context, textStyleModel, child) => Container(
width: 40,
height: 40,
margin: EdgeInsets.only(right: 7),
decoration: BoxDecoration(
color: textStyleModel.textStyle?.color,
border: Border.all(color: Colors.white, width: 1.5),
borderRadius: BorderRadius.circular(100),
),
child: Center(
child: Icon(
Icons.colorize,
color: Colors.white,
),
Container(
width: 40,
height: 40,
margin: EdgeInsets.only(right: 7),
decoration: BoxDecoration(
color: textStyleModel.textStyle?.color,
border: Border.all(color: Colors.white, width: 1.5),
borderRadius: BorderRadius.circular(100),
),
child: Center(
child: Icon(
Icons.colorize,
color: Colors.white,
),
),
),
Expand All @@ -52,8 +50,7 @@ class _ColorPicker extends StatelessWidget {

@override
Widget build(BuildContext context) {
TextStyleModel textStyleModel =
Provider.of<TextStyleModel>(context, listen: false);
final textStyleModel = TextEditorData.read(context).textStyleModel;

return GestureDetector(
onTap: () => textStyleModel.editTextColor(color),
Expand Down
5 changes: 2 additions & 3 deletions lib/src/widget/font_family.dart
@@ -1,6 +1,6 @@
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:text_editor/src/font_option_model.dart';
import 'package:text_editor/text_editor_data.dart';

class FontFamily extends StatefulWidget {
final List<FontFamilyModel> fonts;
Expand Down Expand Up @@ -36,8 +36,7 @@ class _FontFamilyPicker extends StatelessWidget {

@override
Widget build(BuildContext context) {
FontOptionModel fontOptionModel =
Provider.of<FontOptionModel>(context, listen: false);
final fontOptionModel = TextEditorData.read(context).fontOptionModel;

return GestureDetector(
onTap: () => fontOptionModel.selectFontFamily(font),
Expand Down
23 changes: 11 additions & 12 deletions lib/src/widget/font_option_switch.dart
@@ -1,6 +1,6 @@
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:text_editor/src/font_option_model.dart';
import 'package:text_editor/text_editor_data.dart';

class FontOptionSwitch extends StatefulWidget {
final Widget? fontFamilySwitch;
Expand All @@ -15,17 +15,16 @@ class FontOptionSwitch extends StatefulWidget {
class _FontOptionSwitch extends State<FontOptionSwitch> {
@override
Widget build(BuildContext context) {
return Consumer<FontOptionModel>(
builder: (context, model, child) => GestureDetector(
onTap: () => model.changeFontOptionStatus(model.status),
child: model.status == FontOptionStatus.fontFamily
? (widget.colorPaletteSwitch == null
? _ColorOption()
: widget.colorPaletteSwitch)
: (widget.fontFamilySwitch == null
? _FontOption()
: widget.fontFamilySwitch),
),
final model = TextEditorData.of(context).fontOptionModel;
return GestureDetector(
onTap: () => model.changeFontOptionStatus(model.status),
child: model.status == FontOptionStatus.fontFamily
? (widget.colorPaletteSwitch == null
? _ColorOption()
: widget.colorPaletteSwitch)
: (widget.fontFamilySwitch == null
? _FontOption()
: widget.fontFamilySwitch),
);
}
}
Expand Down
26 changes: 12 additions & 14 deletions lib/src/widget/font_size.dart
@@ -1,6 +1,5 @@
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:text_editor/src/text_style_model.dart';
import 'package:text_editor/text_editor_data.dart';

class FontSize extends StatelessWidget {
final double minFontSize;
Expand All @@ -10,18 +9,17 @@ class FontSize extends StatelessWidget {

@override
Widget build(BuildContext context) {
return Consumer<TextStyleModel>(
builder: (context, model, child) => RotatedBox(
quarterTurns: 3,
child: Slider(
value: model.textStyle?.fontSize ?? minFontSize,
min: minFontSize,
max: maxFontSize,
divisions: ((maxFontSize - minFontSize) * 10).toInt(),
activeColor: Colors.white,
inactiveColor: Colors.white,
onChanged: (double value) => model.editFontSize(value),
),
final model = TextEditorData.of(context).textStyleModel;
return RotatedBox(
quarterTurns: 3,
child: Slider(
value: model.textStyle?.fontSize ?? minFontSize,
min: minFontSize,
max: maxFontSize,
divisions: ((maxFontSize - minFontSize) * 10).toInt(),
activeColor: Colors.white,
inactiveColor: Colors.white,
onChanged: (double value) => model.editFontSize(value),
),
);
}
Expand Down
11 changes: 5 additions & 6 deletions lib/src/widget/text_alignment.dart
@@ -1,6 +1,6 @@
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:text_editor/src/text_style_model.dart';
import 'package:text_editor/text_editor_data.dart';

class TextAlignment extends StatelessWidget {
final Widget? left;
Expand Down Expand Up @@ -35,11 +35,10 @@ class TextAlignment extends StatelessWidget {

@override
Widget build(BuildContext context) {
return Consumer<TextStyleModel>(
builder: (context, model, child) => GestureDetector(
onTapUp: (_) => _onChangeAlignment(model),
child: _mapTextAlignToWidget(model.textAlign!),
),
final model = TextEditorData.of(context).textStyleModel;
return GestureDetector(
onTapUp: (_) => _onChangeAlignment(model),
child: _mapTextAlignToWidget(model.textAlign!),
);
}
}
18 changes: 8 additions & 10 deletions lib/src/widget/text_background_color.dart
@@ -1,7 +1,6 @@
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

import '../text_style_model.dart';
import 'package:text_editor/src/text_style_model.dart';
import 'package:text_editor/text_editor_data.dart';

class TextBackgroundColor extends StatelessWidget {
final Widget? enableWidget;
Expand All @@ -11,13 +10,12 @@ class TextBackgroundColor extends StatelessWidget {

@override
Widget build(BuildContext context) {
return Consumer<TextStyleModel>(
builder: (context, model, child) => GestureDetector(
onTap: () => model.changeTextBackground(),
child: model.textBackgroundStatus != TextBackgroundStatus.disable
? enableWidget ?? _SwitchButton(enabled: true)
: disableWidget ?? _SwitchButton(enabled: false),
),
final model = TextEditorData.of(context).textStyleModel;
return GestureDetector(
onTap: () => model.changeTextBackground(),
child: model.textBackgroundStatus != TextBackgroundStatus.disable
? enableWidget ?? _SwitchButton(enabled: true)
: disableWidget ?? _SwitchButton(enabled: false),
);
}
}
Expand Down
55 changes: 28 additions & 27 deletions lib/text_editor.dart
@@ -1,14 +1,14 @@
library text_editor;

import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:text_editor/src/font_option_model.dart';
import 'package:text_editor/src/text_style_model.dart';
import 'package:text_editor/src/widget/color_palette.dart';
import 'package:text_editor/src/widget/font_family.dart';
import 'package:text_editor/src/widget/font_size.dart';
import 'package:text_editor/src/widget/font_option_switch.dart';
import 'package:text_editor/src/widget/text_alignment.dart';
import 'package:text_editor/text_editor_data.dart';

import 'src/widget/text_background_color.dart';

Expand Down Expand Up @@ -98,6 +98,16 @@ class _TextEditorState extends State<TextEditor> {
colors: widget.paletteColors,
);

// Rebuild whenever a value changes
_textStyleModel.addListener(() {
setState(() {});
});

// Rebuild whenever a value changes
_fontOptionModel.addListener(() {
setState(() {});
});

// Initialize decorator
_doneButton = widget.decoration?.doneButton ??
Text('Done', style: TextStyle(color: Colors.white));
Expand All @@ -115,11 +125,9 @@ class _TextEditorState extends State<TextEditor> {

@override
Widget build(BuildContext context) {
return MultiProvider(
providers: [
ChangeNotifierProvider(create: (context) => _textStyleModel),
ChangeNotifierProvider(create: (context) => _fontOptionModel),
],
return TextEditorData(
textStyleModel: _textStyleModel,
fontOptionModel: _fontOptionModel,
child: Container(
padding: EdgeInsets.only(right: 10, left: 10),
color: widget.backgroundColor,
Expand Down Expand Up @@ -173,21 +181,17 @@ class _TextEditorState extends State<TextEditor> {
Expanded(
child: Container(
child: Center(
child: Consumer<TextStyleModel>(
builder: (context, textStyleModel, child) {
return TextField(
controller: TextEditingController()
..text = textStyleModel.text,
onChanged: (value) => textStyleModel.text = value,
maxLines: null,
keyboardType: TextInputType.multiline,
style: textStyleModel.textStyle,
textAlign: textStyleModel.textAlign!,
autofocus: true,
cursorColor: Colors.white,
decoration: null,
);
},
child: TextField(
controller: TextEditingController()
..text = _textStyleModel.text,
onChanged: (value) => _textStyleModel.text = value,
maxLines: null,
keyboardType: TextInputType.multiline,
style: _textStyleModel.textStyle,
textAlign: _textStyleModel.textAlign!,
autofocus: true,
cursorColor: Colors.white,
decoration: null,
),
),
),
Expand All @@ -197,12 +201,9 @@ class _TextEditorState extends State<TextEditor> {
),
Container(
margin: EdgeInsets.only(bottom: 5),
child: Consumer<FontOptionModel>(
builder: (context, model, child) =>
model.status == FontOptionStatus.fontFamily
? FontFamily(model.fonts)
: ColorPalette(model.colors!),
),
child: _fontOptionModel.status == FontOptionStatus.fontFamily
? FontFamily(_fontOptionModel.fonts)
: ColorPalette(_fontOptionModel.colors!),
),
],
),
Expand Down
37 changes: 37 additions & 0 deletions lib/text_editor_data.dart
@@ -0,0 +1,37 @@
import 'package:flutter/material.dart';
import 'package:text_editor/src/font_option_model.dart';
import 'package:text_editor/src/text_style_model.dart';

class TextEditorData extends InheritedWidget {
const TextEditorData({
Key? key,
required Widget child,
required this.textStyleModel,
required this.fontOptionModel,
}) : super(key: key, child: child);

final TextStyleModel textStyleModel;
final FontOptionModel fontOptionModel;

static TextEditorData of(BuildContext context) {
final TextEditorData? result =
context.dependOnInheritedWidgetOfExactType<TextEditorData>();
assert(result != null, 'No TextEditorData found in context');
return result!;
}

static TextEditorData read(BuildContext context) {
final TextEditorData? result =
context.findAncestorWidgetOfExactType<TextEditorData>();
assert(result != null, 'No TextEditorData found in context');
return result!;
}

@override
bool updateShouldNotify(covariant InheritedWidget oldWidget) {
// Not the most efficient but we can safely assume that if we got rebuilt
// it's because a value in either textStlyeModel or fontOptionModel was changed.
// (This is the only time TextEditor rebuild itslef).
return true;
}
}