Skip to content

Commit

Permalink
feature: depends on async
Browse files Browse the repository at this point in the history
example: for depends on async
format
  • Loading branch information
ali77gh committed Mar 28, 2023
1 parent 4d073a8 commit 56a31ac
Show file tree
Hide file tree
Showing 18 changed files with 295 additions and 177 deletions.
5 changes: 2 additions & 3 deletions example/integration_test/01.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ import 'package:flutter_test/flutter_test.dart';

import 'package:app/01_simple_text_sample/main.dart' as example01;

void test(){

void test() {
testWidgets('simple text', (tester) async {
example01.main();
await tester.pumpAndSettle();
Expand All @@ -23,4 +22,4 @@ void test(){
expect(find.text('aa'), findsNWidgets(2));
expect(find.text('2'), findsOneWidget);
});
}
}
5 changes: 2 additions & 3 deletions example/integration_test/02.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ import 'package:flutter_test/flutter_test.dart';

import 'package:app/02_object_apply_sample/main.dart' as example02;

void test(){

void test() {
testWidgets('object apply', (tester) async {
example02.main();
await tester.pumpAndSettle();
Expand All @@ -15,4 +14,4 @@ void test(){

expect(find.text('Ali is 25 years old.'), findsOneWidget);
});
}
}
9 changes: 4 additions & 5 deletions example/integration_test/03.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';

import 'package:app/03_depend_observable_sample/main.dart' as example03;

void test(){
import 'package:app/03_1_depend_observable_sample/main.dart' as example03;

void test() {
testWidgets('bmi', (tester) async {
example03.main();
await tester.pumpAndSettle();
Expand All @@ -20,9 +19,9 @@ void test(){
var height = tester.allWidgets.whereType<Slider>().last.value;
var result = tester.allWidgets.whereType<Text>().last.data!;

var bmi = weight/ ((height/ 100) * (height / 100));
var bmi = weight / ((height / 100) * (height / 100));

expect(result.contains("$bmi"), true);
expect(initResult != result, true);
});
}
}
3 changes: 1 addition & 2 deletions example/integration_test/entry_point_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,4 @@ void main() {
example01.test();
example02.test();
example03.test();

}
}
1 change: 0 additions & 1 deletion example/lib/02_object_apply_sample/main.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import 'package:app/02_object_apply_sample/object_apply_sample.dart';
import 'package:flutter/material.dart';


void main() {
runApp(const MyApp());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class ObjectApplySampleLayoutState extends State<ObjectApplySampleLayout> {
)));
}
}

class Human {
String name;
int age;
Expand Down
83 changes: 83 additions & 0 deletions example/lib/03_1_depend_observable_sample/depends_on_sample.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import 'package:flutter/material.dart';
import 'package:telescope/telescope.dart';

class DependObservableSampleLayout extends StatefulWidget {
const DependObservableSampleLayout({Key? key}) : super(key: key);

@override
State<DependObservableSampleLayout> createState() =>
DependObservableSampleLayoutState();
}

class DependObservableSampleLayoutState
extends State<DependObservableSampleLayout> {
// observables
late Telescope<int> height;
late Telescope<int> weight;
late Telescope<double> bmi;
late Telescope<String> showingText;

@override
void initState() {
super.initState();

height = Telescope(186);
weight = Telescope(72);

bmi = Telescope.dependsOn([height, weight], () {
return weight.value / ((height.value / 100) * (height.value / 100));
});

showingText = Telescope.dependsOn([bmi], () {
return "weight is ${weight.value} and height is ${height.value} so bmi will be ${bmi.value.toString().substring(0, 5)}";
});
}

@override
Widget build(BuildContext context) {
var style = const TextStyle(fontSize: 40);
return Material(
type: MaterialType.transparency,
child: SafeArea(
child: Container(
color: Colors.white,
child: Column(
children: [
const SizedBox(height: 20),
Text(
"Weight(kg):",
style: style,
),
Slider(
value: weight.watch(this).toDouble(),
min: 1,
max: 200,
label: weight.watch(this).round().toString(),
onChanged: (double value) {
weight.value = value.toInt();
}),
Text(
"Height(cm):",
style: style,
),
Slider(
value: height.watch(this).toDouble(),
min: 1,
max: 200,
label: height.watch(this).round().toString(),
onChanged: (double value) {
height.value = value.toInt();
}),
Text(
"Result:",
style: style,
),
Text(
showingText.watch(this),
style: style,
),
],
),
)));
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import 'package:app/03_depend_observable_sample/depends_on_sample.dart';
import 'package:app/03_1_depend_observable_sample/depends_on_sample.dart';
import 'package:flutter/material.dart';

void main() {
Expand Down
90 changes: 90 additions & 0 deletions example/lib/03_2_depends_on_async/depends_on_async_sample.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import 'package:flutter/material.dart';
import 'package:telescope/telescope.dart';

class DependObservableAsyncSampleLayout extends StatefulWidget {
const DependObservableAsyncSampleLayout({Key? key}) : super(key: key);

@override
State<DependObservableAsyncSampleLayout> createState() =>
DependObservableAsyncSampleLayoutState();
}

class DependObservableAsyncSampleLayoutState
extends State<DependObservableAsyncSampleLayout> {
// observables
late Telescope<int> height;
late Telescope<int> weight;
late Telescope<double> bmi;
late Telescope<String> showingText;

@override
void initState() {
super.initState();

height = Telescope(186);
weight = Telescope(72);

bmi = Telescope.dependsOnAsync(0, [height, weight], () async {
return await calculateBMI(height.value, weight.value);
});

showingText = Telescope.dependsOn([height, weight, bmi], () {
var bmis = bmi.value.toString();
bmis = bmis.length > 5 ? bmis.substring(0, 5) : bmis;
return "weight is ${weight.value} and height is ${height.value} so bmi will be $bmis";
});
}

@override
Widget build(BuildContext context) {
var style = const TextStyle(fontSize: 40);
return Material(
type: MaterialType.transparency,
child: SafeArea(
child: Container(
color: Colors.white,
child: Column(
children: [
const SizedBox(height: 20),
Text(
"Weight(kg):",
style: style,
),
Slider(
value: weight.watch(this).toDouble(),
min: 1,
max: 200,
label: weight.watch(this).round().toString(),
onChanged: (double value) {
weight.value = value.toInt();
}),
Text(
"Height(cm):",
style: style,
),
Slider(
value: height.watch(this).toDouble(),
min: 1,
max: 200,
label: height.watch(this).round().toString(),
onChanged: (double value) {
height.value = value.toInt();
}),
Text(
"Result:",
style: style,
),
Text(
showingText.watch(this),
style: style,
),
],
),
)));
}
}

Future<double> calculateBMI(int w, int h) async {
await Future.delayed(const Duration(seconds: 2));
return w / ((h / 100) * (h / 100));
}
18 changes: 18 additions & 0 deletions example/lib/03_2_depends_on_async/main.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import 'package:app/03_2_depends_on_async/depends_on_async_sample.dart';
import 'package:flutter/material.dart';

void main() {
runApp(const MyApp());
}

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

@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData(fontFamily: 'IranSans'),
debugShowCheckedModeBanner: false,
home: const DependObservableAsyncSampleLayout());
}
}
83 changes: 0 additions & 83 deletions example/lib/03_depend_observable_sample/depends_on_sample.dart

This file was deleted.

3 changes: 1 addition & 2 deletions example/test/widget_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,4 @@
// gestures. You can also use WidgetTester to find child widgets in the widget
// tree, read text, and verify that the values of widget properties are correct.

void main() {
}
void main() {}
Loading

0 comments on commit 56a31ac

Please sign in to comment.