Here is a code that creates 2 clickable containers.
Each container changes the background color.
I’d like to save the background color when I relaunch the application.
I know I need to use shared_preference but I don’t know where to put the ‘themeColor’ variable.
If someone could help me, that would be great.
Thank you
class _ColorSaveState extends State<ColorSave> {
//VARIABLE
var themeColor = Colors.red[100];
//METHOD CHANGE COLOR
colorBackground(String color){
setState(() {
if(color == "themeBlue"){
themeColor = Colors.blue;
}else{
themeColor = Colors.green;
}
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
centerTitle: true,
title: Text('ColorSave'),
backgroundColor: Colors.blue,
),
//I would like to save the state of 'themeColor'
backgroundColor: themeColor,
body: Center(
child: Column(
children:<Widget>[
//Two Container Clickable
InkWell(
child: Container(
width: 100,
height: 100,
color: Colors.blue,
),
onTap: (){
colorBackground("themeBlue");
},
),
InkWell(
child: Container(
width: 100,
height: 100,
color: Colors.green,
),
onTap: (){
colorBackground("themeGreen");
},
),
]
)
)
);
}
}
New contributor
julien tacru is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.