Created
May 30, 2021 21:43
-
-
Save diegoveloper/e37a6f7c7485cbda01ed0ee189162d16 to your computer and use it in GitHub Desktop.
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
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(); | |
} | |
} |
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
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