Skip to content

Instantly share code, notes, and snippets.

@diegoveloper
Created May 30, 2021 21:43
Show Gist options
  • Save diegoveloper/e37a6f7c7485cbda01ed0ee189162d16 to your computer and use it in GitHub Desktop.
Save diegoveloper/e37a6f7c7485cbda01ed0ee189162d16 to your computer and use it in GitHub Desktop.
import 'package:flutter/material.dart';
class MainNotifier {
final notifierCounter = ValueNotifier(0);
final notifierSlider = ValueNotifier(0.0);
final notifierCheck = ValueNotifier(false);
void incrementCounter() {
notifierCounter.value = notifierCounter.value + 1;
}
void sliderChanged(double val) {
notifierSlider.value = val;
}
void checkChanged(bool val) {
notifierCheck.value = val;
}
}
class MainBloc extends ChangeNotifier {
int counter = 0;
void incrementCounter() {
counter++;
notifyListeners();
}
}
class MainBlocSlider extends ChangeNotifier {
double sliderValue = 0;
void sliderChanged(double val) {
sliderValue = val;
notifyListeners();
}
}
import 'package:sample/main_bloc.dart';
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData(
primarySwatch: Colors.red,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
const style = TextStyle(
fontSize: 80,
fontWeight: FontWeight.bold,
);
class _MyHomePageState extends State<MyHomePage> {
final myNotifier = MainNotifier();
@override
Widget build(BuildContext context) {
print('rebuild...');
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
const Text(
'You have pushed the button this many times:',
),
ValueListenableBuilder(
valueListenable: myNotifier.notifierCounter,
builder: (context, snapshot, _) {
print('rebuild text');
return Text(
'$snapshot',
style: style,
);
},
),
const SizedBox(height: 30),
Container(
height: 1,
color: Colors.grey,
),
ValueListenableBuilder(
valueListenable: myNotifier.notifierSlider,
child: MyLogo(),
builder: (context, snapshot, child334324) {
print('rebuild slider');
return Column(
children: [
Text(
'${snapshot.toStringAsFixed(2)}',
style: style,
),
child334324,
Slider(
value: snapshot,
onChanged: myNotifier.sliderChanged,
),
],
);
}),
ValueListenableBuilder(
valueListenable: myNotifier.notifierCheck,
builder: (context, snapshot, _) {
print('rebuild checkbox');
return Checkbox(
value: snapshot,
onChanged: myNotifier.checkChanged,
);
}),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: myNotifier.incrementCounter,
child: Icon(Icons.add),
),
);
}
}
class MyLogo extends StatelessWidget {
@override
Widget build(BuildContext context) {
print('rebuild logo');
return FlutterLogo(
size: 100,
);
}
}
class HorizontalListWidget extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Container();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment