Created with <3 with dartpad.dev.
Last active
April 14, 2023 14:32
-
-
Save aliyazdi75/ae816915e62542d85caa5c08b51460e4 to your computer and use it in GitHub Desktop.
Better Structure
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'; | |
void main() => runApp(MyApp()); | |
class MyApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
title: 'Flutter Demo', | |
debugShowCheckedModeBanner: false, | |
theme: ThemeData( | |
primarySwatch: Colors.blue, | |
), | |
home: const MyHomePage(title: 'Flutter Demo Home Page'), | |
); | |
} | |
} | |
class MyHomePage extends StatefulWidget { | |
final String title; | |
const MyHomePage({ | |
Key? key, | |
required this.title, | |
}) : super(key: key); | |
@override | |
State<MyHomePage> createState() => _MyHomePageState(); | |
} | |
class _MyHomePageState extends State<MyHomePage> { | |
final counterGreenNotifier = ValueNotifier<int>(0); | |
final counterRedNotifier = ValueNotifier<int>(0); | |
void _incrementCounterGreen() { | |
counterGreenNotifier.value += 1; | |
} | |
void _incrementCounterRed() { | |
counterRedNotifier.value += 1; | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
title: Text(widget.title), | |
), | |
body: Center( | |
child: Column( | |
mainAxisAlignment: MainAxisAlignment.center, | |
children: [ | |
const Text( | |
'You have pushed the Green button this many times:', | |
), | |
ValueListenableBuilder( | |
valueListenable: counterGreenNotifier, | |
builder: (context, counterGreen, child) { | |
return Text( | |
'$counterGreen', | |
style: Theme.of(context) | |
.textTheme | |
.headlineMedium! | |
.copyWith(color: Colors.green), | |
); | |
}, | |
), | |
const Text( | |
'You have pushed the Red button this many times:', | |
), | |
ValueListenableBuilder( | |
valueListenable: counterRedNotifier, | |
builder: (context, counterRed, child) { | |
return Text( | |
'$counterRed', | |
style: Theme.of(context) | |
.textTheme | |
.headlineMedium! | |
.copyWith(color: Colors.red), | |
); | |
}, | |
), | |
], | |
), | |
), | |
floatingActionButton: Column( | |
mainAxisSize: MainAxisSize.min, | |
children: [ | |
FloatingActionButton( | |
onPressed: _incrementCounterGreen, | |
tooltip: 'Increment Green', | |
backgroundColor: Colors.green, | |
child: const Icon(Icons.add), | |
), | |
FloatingActionButton( | |
onPressed: _incrementCounterRed, | |
tooltip: 'Increment Red', | |
backgroundColor: Colors.red, | |
child: const Icon(Icons.add), | |
), | |
], | |
), | |
); | |
} | |
} |
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'; | |
void main() => runApp(MyApp()); | |
class MyApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
title: 'Flutter Demo', | |
debugShowCheckedModeBanner: false, | |
theme: ThemeData( | |
primarySwatch: Colors.blue, | |
), | |
home: const MyHomePage(title: 'Flutter Demo Home Page'), | |
); | |
} | |
} | |
class MyHomePage extends StatefulWidget { | |
final String title; | |
const MyHomePage({ | |
Key? key, | |
required this.title, | |
}) : super(key: key); | |
@override | |
State<MyHomePage> createState() => _MyHomePageState(); | |
} | |
class _MyHomePageState extends State<MyHomePage> { | |
final counterGreenNotifier = ValueNotifier<int>(0); | |
final counterRedNotifier = ValueNotifier<int>(0); | |
void _incrementCounterGreen() { | |
counterGreenNotifier.value += 1; | |
} | |
void _incrementCounterRed() { | |
counterRedNotifier.value += 1; | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
title: Text(widget.title), | |
), | |
body: Center( | |
child: Column( | |
mainAxisAlignment: MainAxisAlignment.center, | |
children: [ | |
const Text( | |
'You have pushed the Green button this many times:', | |
), | |
ValueListenableBuilder( | |
valueListenable: counterGreenNotifier, | |
builder: (context, counterGreen, child) { | |
return Text( | |
'$counterGreen', | |
style: Theme.of(context) | |
.textTheme | |
.headlineMedium! | |
.copyWith(color: Colors.green), | |
); | |
}, | |
), | |
const Text( | |
'You have pushed the Red button this many times:', | |
), | |
ValueListenableBuilder( | |
valueListenable: counterRedNotifier, | |
builder: (context, counterRed, child) { | |
return Text( | |
'$counterRed', | |
style: Theme.of(context) | |
.textTheme | |
.headlineMedium! | |
.copyWith(color: Colors.red), | |
); | |
}, | |
), | |
], | |
), | |
), | |
floatingActionButton: Column( | |
mainAxisSize: MainAxisSize.min, | |
children: [ | |
FloatingActionButton( | |
onPressed: _incrementCounterGreen, | |
tooltip: 'Increment Green', | |
backgroundColor: Colors.green, | |
child: const Icon(Icons.add), | |
), | |
FloatingActionButton( | |
onPressed: _incrementCounterRed, | |
tooltip: 'Increment Red', | |
backgroundColor: Colors.red, | |
child: const Icon(Icons.add), | |
), | |
], | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment