Skip to content

Commit

Permalink
Merge pull request #1 from alekskuzmin/master
Browse files Browse the repository at this point in the history
Migrated to null safety
  • Loading branch information
alxkzmn authored Apr 9, 2021
2 parents fe03c83 + 5e60d92 commit 6b292b0
Show file tree
Hide file tree
Showing 9 changed files with 211 additions and 66 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## [0.3.0] - 2021-04-09

### Null safety
The package is now null-safe. Added a static function in a Smoothie class for convenient import.

## [0.2.0+1] - 2020-05-12

### Breaking changes
Expand Down
129 changes: 128 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

# smoothie

Create and sample a smooth bezier curve going through a given set of points
Create and sample a smooth Bézier curve going through a given set of points

## Usage

Expand Down Expand Up @@ -38,3 +38,130 @@ List<Point<num>> _sampledCurve = _originalDataSeries.smooth(_sampleCount);
See oversampling in action:

<img src="https://raw.githubusercontent.com/alekskuzmin/smoothie/master/example/example.gif" width="270" height="480">

Full example using the `charts_flutter` package:

```dart
import 'dart:math';
import 'package:charts_flutter/flutter.dart' as charts;
import 'package:flutter/material.dart';
import 'package:smoothie/smoothie.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Smoothie Demo',
theme: ThemeData(
primarySwatch: Colors.teal,
),
home: SmoothieHomePage(),
);
}
}
class SmoothieHomePage extends StatefulWidget {
SmoothieHomePage({Key? key}) : super(key: key);
@override
_SmoothieHomePageState createState() => _SmoothieHomePageState();
}
class _SmoothieHomePageState extends State<SmoothieHomePage> {
int _samplingPointCount = 0;
var _originalDataSeries = <Point>[
Point(0, 5),
Point(2, 15),
Point(3, 10),
Point(8, 6),
Point(9, 13),
];
@override
void initState() {
super.initState();
_samplingPointCount = _originalDataSeries.length;
}
void _incrementCounter() {
setState(() {
_samplingPointCount++;
});
}
void _decrementCounter() {
setState(() {
if (_samplingPointCount > _originalDataSeries.length)
_samplingPointCount--;
});
}
@override
Widget build(BuildContext context) {
var series = [
new charts.Series(
domainFn: (Point chartData, _) => chartData.x,
measureFn: (Point chartData, _) => chartData.y,
colorFn: (Point point, _) => charts.MaterialPalette.teal.shadeDefault,
id: 'Example Series',
data: _originalDataSeries.smooth(
_samplingPointCount,
),
),
];
var chart = new charts.LineChart(
series,
animate: false,
defaultRenderer: new charts.LineRendererConfig(
includeArea: true,
),
);
return Scaffold(
appBar: AppBar(
title: Text("Smoothie Demo"),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'The smooth curve now has $_samplingPointCount points.',
),
Container(
height: 200,
child: chart,
),
],
),
),
floatingActionButton: Row(
mainAxisAlignment: MainAxisAlignment.end,
children: <Widget>[
FloatingActionButton(
onPressed: _decrementCounter,
tooltip: 'Decrement',
child: Icon(Icons.remove),
),
FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: Icon(Icons.add),
),
],
));
}
}
```

If you want to run the example as is and you are using null safety, you have to pass `--no-sound-null-safety` to the build command as the current version of `charts_flutter` does not yet support null safety:
```console
cd example
flutter run --no-sound-null-safety
```

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

16 changes: 7 additions & 9 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,13 @@ class MyApp extends StatelessWidget {
theme: ThemeData(
primarySwatch: Colors.teal,
),
home: SmoothieHomePage(title: 'Smoothie Demo'),
home: SmoothieHomePage(),
);
}
}

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

final String title;
SmoothieHomePage({Key? key}) : super(key: key);

@override
_SmoothieHomePageState createState() => _SmoothieHomePageState();
Expand All @@ -31,7 +29,7 @@ class SmoothieHomePage extends StatefulWidget {
class _SmoothieHomePageState extends State<SmoothieHomePage> {
int _samplingPointCount = 0;

var originalDataSeries = <Point>[
var _originalDataSeries = <Point>[
Point(0, 5),
Point(2, 15),
Point(3, 10),
Expand All @@ -42,7 +40,7 @@ class _SmoothieHomePageState extends State<SmoothieHomePage> {
@override
void initState() {
super.initState();
_samplingPointCount = originalDataSeries.length;
_samplingPointCount = _originalDataSeries.length;
}

void _incrementCounter() {
Expand All @@ -53,7 +51,7 @@ class _SmoothieHomePageState extends State<SmoothieHomePage> {

void _decrementCounter() {
setState(() {
if (_samplingPointCount > originalDataSeries.length)
if (_samplingPointCount > _originalDataSeries.length)
_samplingPointCount--;
});
}
Expand All @@ -66,7 +64,7 @@ class _SmoothieHomePageState extends State<SmoothieHomePage> {
measureFn: (Point chartData, _) => chartData.y,
colorFn: (Point point, _) => charts.MaterialPalette.teal.shadeDefault,
id: 'Example Series',
data: originalDataSeries.smooth(
data: _originalDataSeries.smooth(
_samplingPointCount,
),
),
Expand All @@ -82,7 +80,7 @@ class _SmoothieHomePageState extends State<SmoothieHomePage> {

return Scaffold(
appBar: AppBar(
title: Text(widget.title),
title: Text("Smoothie Demo"),
),
body: Center(
child: Column(
Expand Down
Loading

0 comments on commit 6b292b0

Please sign in to comment.