Created
May 2, 2022 02:29
-
-
Save suragch/329fd002d7976cbad4e2a2d340fe67ee to your computer and use it in GitHub Desktop.
Three buttons, change icon (with manager class)
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'; | |
import 'home_screen_manager.dart'; | |
class HomeScreen extends StatefulWidget { | |
const HomeScreen({Key? key}) : super(key: key); | |
@override | |
State<HomeScreen> createState() => _HomeScreenState(); | |
} | |
class _HomeScreenState extends State<HomeScreen> { | |
final manager = HomeScreenManager(); | |
@override | |
Widget build(BuildContext context) { | |
return Center( | |
child: Column( | |
mainAxisAlignment: MainAxisAlignment.center, | |
children: [ | |
ValueListenableBuilder<MyIcon>( | |
valueListenable: manager.iconNotifier, | |
builder: (context, myIcon, child) { | |
if (myIcon == MyIcon.sun) { | |
return Icon(Icons.sunny, size: 50); | |
} else if (myIcon == MyIcon.cloud) { | |
return Icon(Icons.cloud, size: 50); | |
} else { | |
return Icon(Icons.house, size: 50); | |
} | |
}), | |
SizedBox(height: 20), | |
Row( | |
mainAxisAlignment: MainAxisAlignment.center, | |
children: [ | |
ElevatedButton( | |
onPressed: () { | |
manager.chooseHouse(); | |
}, | |
child: Text('House'), | |
), | |
SizedBox(width: 20), | |
ElevatedButton( | |
onPressed: () { | |
manager.chooseSun(); | |
}, | |
child: Text('Sun'), | |
), | |
SizedBox(width: 20), | |
ElevatedButton( | |
onPressed: () { | |
manager.chooseCloud(); | |
}, | |
child: Text('Cloud'), | |
), | |
], | |
), | |
], | |
), | |
); | |
} | |
} |
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 HomeScreenManager { | |
final iconNotifier = ValueNotifier<MyIcon>(MyIcon.house); | |
void chooseHouse() { | |
iconNotifier.value = MyIcon.house; | |
} | |
void chooseSun() { | |
iconNotifier.value = MyIcon.sun; | |
} | |
void chooseCloud() { | |
iconNotifier.value = MyIcon.cloud; | |
} | |
} | |
enum MyIcon { | |
house, | |
sun, | |
cloud, | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment