Skip to content

Instantly share code, notes, and snippets.

@darwin-morocho
Last active September 19, 2021 14:36
Show Gist options
  • Save darwin-morocho/79d21c50ba6c207e7ef9687156802308 to your computer and use it in GitHub Desktop.
Save darwin-morocho/79d21c50ba6c207e7ef9687156802308 to your computer and use it in GitHub Desktop.
What is flutter_meedu?
import 'package:flutter_meedu/meedu.dart';
class LoginController extends StateNotifier<LoginState> {
// you need pass an inital state using super
LoginController():super(LoginState.initialState);
void onEmailChanged(String email) {
state = state.copyWith(email: email);
}
void onPasswordChanged(String password) {
state = state.copyWith(password: password);
}
}
import 'package:flutter/material.dart';
import 'package:flutter_meedu/flutter_meedu.dart';
final loginProvider = StateProvider<LoginController, LoginState>(
(_) => LoginController(),
);
class LoginPage extends StatelessWidget {
const LoginPage({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Column(
children: [
TextField(
onChanged: loginProvider.read.onEmailChanged,
decoration: InputDecoration(
labelText: "Email",
),
),
TextField(
onChanged: loginProvider.read.onPasswordChanged,
decoration: InputDecoration(
labelText: "Password",
),
),
SizedBox(height: 30),
Consumer(
builder: (_, ref, __) {
final controller = ref.watch(loginProvider);
final state = controller.state;
final email = state.email;
final password = state.password;
final enabled = email.isNotEmpty && password.isNotEmpty;
return ElevatedButton(
onPressed: enabled
? () {
// YOUR CODE HERE
}
: null,
child: Text("SEND"),
);
},
)
],
),
),
),
);
}
}
import 'package:equatable/equatable.dart';
class LoginState extends Equatable {
final String email, password;
LoginState({
required this.email,
required this.password,
});
static LoginState get initialState => LoginState(email: '', password: '');
LoginState copyWith({
String? email,
String? password,
}) {
return LoginState(
email: email ?? this.email,
password: password ?? this.password,
);
}
@override
List<Object?> get props => [email, password];
}
import 'package:flutter_meedu/router.dart' as router;
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
navigatorKey: router.navigatorKey, // add the navigator key
home: HomePage(),
routes: {YOUR_ROUTES},
);
}
}
class CounterPage extends PageWithArgumentsWidget {
const CounterPage({Key? key}) : super(key: key);
@override
void onInit(RouteSettings settings) {
/// you can use settings to get data passed as an argument
/// using Navigator.pushName(context,'route-name', arguments: data);
counterProvider.setArguments(settings.arguments);
}
@override
Widget build(BuildContext context) {
return Scaffold(
.
.
.
);
}
}
environment:
sdk: ">=2.13.0 <3.0.0"
flutter: ">=2.0.0"
dependencies:
flutter_meedu: "^5.0.1"
import 'package:flutter_meedu/router.dart' as router;
.
.
.
router.pushNamed('detail-page', arguments: "your-arguments");
Consumer(
builder: (_, ref, __) {
final email = ref.select(
loginProvider.select((_)=>_.email),
);
return Text(email);
},
)
import 'package:flutter/material.dart';
import 'package:flutter_meedu/flutter_meedu.dart';
class CounterController extends SimpleNotifier{
int _counter = 0;
int get counter => _counter;
void increment(){
_counter++;
notify(); // notify to all listeners
}
}
final counterProvider = SimpleProvider(
(_) => CounterController(),
);
class CounterPage extends StatelessWidget {
const CounterPage({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
// The Consumer widget listen the changes in your CounterController
// and rebuild the widget when is need it
child: Consumer(builder: (_, ref, __) {
final controller = ref.watch(counterProvider);
return Text("${controller.counter}");
}),
),
floatingActionButton: FloatingActionButton(
onPressed: () {
// you can use the read property to access to your CounterController
counterProvider.read.increment();
},
),
);
}
}
import 'package:flutter/material.dart';
import 'package:flutter_meedu/flutter_meedu.dart';
class CounterController extends SimpleNotifier{
late int _counter;
int get counter => _counter;
CounterController(this._counter);
void increment(){
_counter++;
notify(); // notify to all listeners
}
}
final counterProvider = SimpleProvider(
(ref) => CounterController(ref.arguments),
);
class CounterPage extends StatelessWidget {
CounterPage({Key? key}) : super(key: key){
/// set initial value
counterProvider.setArguments(10);
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
// The Consumer widget listen the changes in your CounterController
// and rebuild the widget when is need it
child: Consumer(builder: (_, ref, __) {
final controller = ref.watch(counterProvider);
return Text("${controller.counter}");
}),
),
floatingActionButton: FloatingActionButton(
onPressed: () {
// you can use the read property to access to your CounterController
counterProvider.read.increment();
},
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment