Spaces:
Paused
Paused
File size: 4,582 Bytes
4547589 38048d1 f21ac8b 4547589 a8cb3e9 4547589 a8cb3e9 4547589 a8cb3e9 4547589 a8cb3e9 4547589 a8cb3e9 4547589 a8cb3e9 4547589 f21ac8b 4547589 a8cb3e9 4547589 38048d1 4547589 a8cb3e9 4547589 a8cb3e9 4547589 38048d1 4547589 f21ac8b 4547589 a8cb3e9 4547589 a8cb3e9 38048d1 a8cb3e9 38048d1 a8cb3e9 38048d1 a8cb3e9 38048d1 a8cb3e9 38048d1 a8cb3e9 4547589 a8cb3e9 38048d1 a8cb3e9 4547589 38048d1 4547589 38048d1 a8cb3e9 4547589 a8cb3e9 4547589 f21ac8b 4547589 a8cb3e9 4547589 a8cb3e9 4547589 a8cb3e9 4547589 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 |
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:flutter_colorpicker/flutter_colorpicker.dart';
import 'custom_color_picker_widget.dart';
import 'custom_button.dart';
import 'home_screen.dart';
class SetupScreen extends StatefulWidget{
const SetupScreen({super.key});
@override
State<SetupScreen> createState(){
return _SetupScreenState();
}
}
class _SetupScreenState extends State<SetupScreen>{
late TextEditingController textcontroller; // for reading name
late SharedPreferences prefs; // for reading local storage
late Color profileColor; // to store the profile color
@override
void initState(){
super.initState();
textcontroller=TextEditingController();
profileColor=Colors.red.shade200; // default profile color
loadPreferences();
}
Future loadPreferences() async{
prefs = await SharedPreferences.getInstance();
}
@override
void dispose(){ // every StatefulWidget has an dispose method which is called when we move to another screen
// it is used to free up resources
textcontroller.dispose();
super.dispose();
}
void changeColor(color){
setState((){
profileColor=color;
});
}
Widget header(){
return Column(
children: [
// Main Title
const Text(
"Haptic Link",
style: TextStyle(
fontSize: 40,
fontWeight: FontWeight.w500,
),
),
// Sub title
Container(
margin:const EdgeInsets.only(top:20), // adding margin between main title and sub title
child:const Text(
"Linking people with haptics",
style: TextStyle(
fontSize: 20,
fontWeight: FontWeight.w200,
),
),
),
],
);
}
Widget footer(){
/*
Consists of :-
Textfield (for entering name)
choose Color section
Join button
*/
return Column(
children: [
// Input fields
Container(
margin:const EdgeInsets.only(bottom:20),
child:TextField(
decoration:const InputDecoration(
hintText: "Enter your first name",
border:OutlineInputBorder(),
),
style:const TextStyle(
fontSize: 20,
fontWeight: FontWeight.w300,
),
controller: textcontroller, // linking the textcontroller with the textfield
),
),
// Text and Color Picker in a row
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
// Text
Container(
margin:const EdgeInsets.only(right:20),
child:const Text("Choose your Color"),
),
// Color Picker Widget
CustomColorPickerWidget(
pickerColor: profileColor,
onSubmit: changeColor,
size:40,
),
]),
// margin
const SizedBox(height: 20,), // just an transparent box taking some space
// Join button
CustomButton(
onPressed: submit, // trigger submit function on press
padding:const EdgeInsets.symmetric(vertical:15,horizontal: 20),
shape: const RoundedRectangleBorder(
borderRadius: BorderRadius.all(Radius.circular(50)),
),
child:const Text(
"Join",
style:TextStyle(
fontSize:20,
),
),
),
]);
}
void submit(){
if(textcontroller.text.isNotEmpty){
// save username & profileColor to Shared Preferences
prefs.setBool("setup_done", true);
prefs.setString("username", textcontroller.text);
prefs.setString("profile_color", colorToHex(profileColor));
// go to Lobby
Navigator.pushReplacement(
context,
MaterialPageRoute(
builder: (context)=>const HomeScreen() // HomeScreen
),
);
}
}
@override
Widget build(BuildContext context){
return Scaffold(
body:Container(
padding:const EdgeInsets.all(20),
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
header(), // header section consists of a Main Title and a Sub Title
footer(),
],
),
),
);
}
} |