-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
doc: Update example code of lifecycle and hooks to show as tabs
- Loading branch information
1 parent
1e4cacb
commit b050c77
Showing
9 changed files
with
171 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import 'package:flutter/material.dart'; | ||
import 'my_custom_form.dart'; | ||
|
||
void main() { | ||
runApp(MyApp()); | ||
} | ||
|
||
class MyApp extends StatelessWidget { | ||
@override | ||
Widget build(BuildContext context) { | ||
return MaterialApp( | ||
home: MyCustomForm(), | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import 'package:flutter_reactter/flutter_reactter.dart'; | ||
import 'use_text_input.dart'; | ||
|
||
class MyController { | ||
final firstNameInput = UseTextInput(); | ||
final lastNameInput = UseTextInput(); | ||
|
||
late final fullName = Reactter.lazyState( | ||
() => UseCompute( | ||
() { | ||
final firstName = firstNameInput.value; | ||
final lastName = lastNameInput.value; | ||
|
||
return "$firstName $lastName"; | ||
}, | ||
[firstNameInput, lastNameInput], | ||
), | ||
this, | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import 'package:flutter/material.dart'; | ||
import 'package:flutter_reactter/flutter_reactter.dart'; | ||
import 'my_controller.dart'; | ||
|
||
class MyCustomForm extends StatelessWidget { | ||
const MyCustomForm({Key? key}) : super(key: key); | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return ReactterProvider<MyController>( | ||
() => MyController(), | ||
builder: (context, myController, child) { | ||
return Scaffold( | ||
appBar: AppBar( | ||
title: const Text("Retrieve Text Input"), | ||
), | ||
body: Padding( | ||
padding: const EdgeInsets.all(16), | ||
child: Column( | ||
crossAxisAlignment: CrossAxisAlignment.start, | ||
children: [ | ||
ReactterConsumer<MyController>( | ||
listenStates: (myController) => [myController.fullName], | ||
child: const Text( | ||
"Full Name:", | ||
style: TextStyle(fontWeight: FontWeight.bold), | ||
), | ||
builder: (context, myController, child) { | ||
return Row( | ||
children: [ | ||
child!, | ||
const SizedBox(width: 4), | ||
Text(myController.fullName.value), | ||
], | ||
); | ||
}, | ||
), | ||
const SizedBox(height: 8), | ||
TextField( | ||
decoration: InputDecoration( | ||
hintText: "First Name", | ||
), | ||
controller: myController.firstNameInput.controller, | ||
), | ||
TextField( | ||
decoration: InputDecoration( | ||
hintText: "Last Name", | ||
), | ||
controller: myController.lastNameInput.controller, | ||
), | ||
], | ||
), | ||
), | ||
); | ||
}, | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import 'package:reactter/reactter.dart'; | ||
import 'package:flutter/widgets.dart'; | ||
|
||
class UseTextInput extends ReactterHook { | ||
// This line is REQUIRED! | ||
final $ = ReactterHook.$register; | ||
|
||
final controller = TextEditingController(); | ||
|
||
String _value = ''; | ||
String? get value => _value; | ||
|
||
UseTextInput() { | ||
UseEffect(() { | ||
controller.addListener(() { | ||
update(() => _value = controller.text); | ||
}); | ||
|
||
return controller.dispose; | ||
}, []); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
name: MyCustomHook | ||
description: A simple counter example | ||
version: 0.1.0 | ||
|
||
environment: | ||
sdk: ">=2.19.0 <4.0.0" | ||
|
||
dependencies: | ||
flutter: | ||
sdk: flutter | ||
flutter_reactter: ^7.1.0 | ||
|
||
flutter: | ||
uses-material-design: true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters