last63

Untitled

Aug 26th, 2024 (edited)
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.97 KB | None | 0 0
  1. import 'package:flutter/material.dart';
  2. import 'package:project_api/ui/login_screen.dart';
  3. import 'package:project_api/ui/main_screen.dart';
  4.  
  5. import 'ui/register_screen.dart';
  6.  
  7. void main() {
  8. runApp(const MyApp());
  9. }
  10.  
  11. class MyApp extends StatelessWidget {
  12. const MyApp({super.key});
  13.  
  14. @override
  15. Widget build(BuildContext context) {
  16. return MaterialApp(
  17. title: 'Flutter Demo',
  18. theme: ThemeData(
  19. colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
  20. useMaterial3: true,
  21. ),
  22. home: RegisterScreen(),
  23. debugShowCheckedModeBanner: false,
  24. routes:
  25. {
  26. '/register' : (context) => RegisterScreen(),
  27. '/login' : (context) => LoginScreen(),
  28. }
  29. );
  30. }
  31. }
  32.  
  33.  
  34.  
  35. =================================Login Screen =======================================
  36.  
  37. import 'package:flutter/material.dart';
  38.  
  39. import 'main_screen.dart';
  40.  
  41. class LoginScreen extends StatefulWidget {
  42. const LoginScreen({super.key});
  43.  
  44. @override
  45. State<LoginScreen> createState() => _LoginScreenState();
  46. }
  47.  
  48. class _LoginScreenState extends State<LoginScreen> {
  49. final usernameController = TextEditingController();
  50. final passwordController = TextEditingController();
  51. var isSignedIn = false;
  52.  
  53. @override
  54. Widget build(BuildContext context) {
  55. return Scaffold(
  56. body: Center(
  57. child: Column(
  58. mainAxisAlignment: MainAxisAlignment.center,
  59. children: [
  60. Image.asset(
  61. 'assets/images/logo.png',
  62. fit: BoxFit.cover,
  63. width: 100.0,
  64. ),
  65. SizedBox(
  66. height: 10.0,
  67. ),
  68. Text(
  69. 'Login to your account',
  70. style: TextStyle(
  71. fontFamily: "Oswald",
  72. fontSize: 24.0,
  73. ),
  74. ),
  75. SizedBox(
  76. height: 10.0,
  77. ),
  78. Padding(
  79. padding: const EdgeInsets.symmetric(
  80. horizontal: 16.0,
  81. ),
  82. child: TextFormField(
  83. keyboardType: TextInputType.emailAddress,
  84. controller: usernameController,
  85. decoration: InputDecoration(
  86. labelText: 'Username',
  87. labelStyle: TextStyle(
  88. fontFamily: "Oswald",
  89. ),
  90. border: OutlineInputBorder(),
  91. ),
  92. ),
  93. ),
  94. SizedBox(
  95. height: 10.0,
  96. ),
  97. Padding(
  98. padding: const EdgeInsets.symmetric(
  99. horizontal: 16.0,
  100. ),
  101. child: TextFormField(
  102. keyboardType: TextInputType.visiblePassword,
  103. controller: passwordController,
  104. enableSuggestions: false,
  105. autocorrect: false,
  106. obscureText: true,
  107. decoration: InputDecoration(
  108. labelText: 'Password',
  109. labelStyle: TextStyle(
  110. fontFamily: "Oswald",
  111. ),
  112. border: OutlineInputBorder(),
  113. ),
  114. ),
  115. ),
  116. SizedBox(
  117. height: 10.0,
  118. ),
  119. Padding(
  120. padding: const EdgeInsets.symmetric(
  121. horizontal: 16.0,
  122. ),
  123. child: SizedBox(
  124. width: double.infinity,
  125. child: ElevatedButton(
  126. onPressed: () {
  127. setState(
  128. () {
  129. //kondisi bila username null
  130. if (usernameController.text.isEmpty) {
  131. ScaffoldMessenger.of(context).showSnackBar(
  132. SnackBar(
  133. content: Text('Username cannot be empty'),
  134. ),
  135. );
  136. } else {
  137. isSignedIn = true;
  138. }
  139.  
  140. if (passwordController.text.isEmpty) {
  141. ScaffoldMessenger.of(context).showSnackBar(
  142. SnackBar(
  143. content: Text('Password cannot be empty'),
  144. ),
  145. );
  146. } else {
  147. isSignedIn = true;
  148. }
  149.  
  150. if (usernameController.text.isNotEmpty &&
  151. passwordController.text.isNotEmpty) {
  152. Navigator.push(
  153. context,
  154. MaterialPageRoute(
  155. builder: (context) => MainScreen(
  156. username: usernameController.text,
  157. password: passwordController.text,
  158. ),
  159. ),
  160. );
  161. }
  162. },
  163. );
  164. },
  165. style: ButtonStyle(
  166. elevation: WidgetStateProperty.all(5.0),
  167. backgroundColor: WidgetStateProperty.all(
  168. Colors.blue,
  169. ),
  170. ),
  171. child: Text(
  172. 'Login',
  173. style: TextStyle(fontFamily: "Oswald", color: Colors.white),
  174. ),
  175. ),
  176. ),
  177. ),
  178. SizedBox(
  179. height: 10.0,
  180. ),
  181. ElevatedButton(
  182. onPressed: () {
  183. Navigator.pushNamed(context, '/register');
  184. },
  185. style: ButtonStyle(
  186. elevation: WidgetStatePropertyAll(
  187. 5.0,
  188. ),
  189. backgroundColor: WidgetStatePropertyAll(
  190. Colors.red,
  191. ),
  192. ),
  193. child: Text(
  194. 'Register',
  195. style: TextStyle(
  196. color: Colors.white,
  197. fontFamily: "Oswald",
  198. ),
  199. ),
  200. ),
  201. ],
  202. )),
  203. );
  204. }
  205. }
  206.  
  207. =============================================================== MAIN SCREEN ===============================================================
  208. import 'package:flutter/material.dart';
  209.  
  210. class MainScreen extends StatelessWidget {
  211. const MainScreen({super.key, this.username, this.password});
  212.  
  213. final String? username;
  214. final String? password;
  215.  
  216. @override
  217. Widget build(BuildContext context) {
  218. return Scaffold(
  219. body: Center(
  220. child: Column(
  221. mainAxisAlignment: MainAxisAlignment.center,
  222. children: <Widget>[
  223. Icon(
  224. Icons.person,
  225. size: 100.0,
  226. ),
  227. Text(
  228. 'Welcome $username!',
  229. style: TextStyle(
  230. fontFamily: 'Oswald',
  231. fontSize: 18.0,
  232. letterSpacing: 2.0,
  233. ),
  234. ),
  235. Text(
  236. 'Your password is $password',
  237. style: TextStyle(
  238. fontFamily: 'Oswald',
  239. fontSize: 18.0,
  240. letterSpacing: 2.0,
  241. ),
  242. ),
  243. ],
  244. ),
  245. ),
  246. );
  247. }
  248. }
  249.  
  250.  
  251.  
  252.  
  253.  
  254.  
  255. =============================================================== REGISTER SCREEN ===============================================================
  256. import 'package:flutter/material.dart';
  257.  
  258. class RegisterScreen extends StatefulWidget {
  259. const RegisterScreen({super.key});
  260.  
  261. @override
  262. State<RegisterScreen> createState() => _RegisterScreenState();
  263. }
  264.  
  265. class _RegisterScreenState extends State<RegisterScreen> {
  266. @override
  267. Widget build(BuildContext context) {
  268. return Scaffold(
  269. body: Center(
  270. child: Column(
  271. mainAxisAlignment: MainAxisAlignment.center,
  272. children: <Widget>[
  273. Image.asset(
  274. 'assets/images/logo.png',
  275. fit: BoxFit.cover,
  276. width: 100.0,
  277. ),
  278. Text(
  279. 'Register to create an account',
  280. style: TextStyle(
  281. fontFamily: "Oswald",
  282. fontSize: 18.0,
  283. ),
  284. ),
  285. SizedBox(
  286. height: 10.0,
  287. ),
  288. Padding(
  289. padding: const EdgeInsets.symmetric(
  290. horizontal: 16.0,
  291. ),
  292. child: TextFormField(
  293. keyboardType: TextInputType.emailAddress,
  294. decoration: InputDecoration(
  295. labelText: 'Email',
  296. labelStyle: TextStyle(
  297. fontFamily: "Oswald",
  298. ),
  299. border: OutlineInputBorder(
  300. borderRadius: BorderRadius.circular(25.0),
  301. ),
  302. ),
  303. style: TextStyle(
  304. fontFamily: "Oswald",
  305. ),
  306. ),
  307. ),
  308. SizedBox(
  309. height: 10.0,
  310. ),
  311. Padding(
  312. padding: const EdgeInsets.symmetric(
  313. horizontal: 16.0,
  314. ),
  315. child: Row(
  316. children: <Widget>[
  317. Expanded(
  318. flex: 1,
  319. child: TextFormField(
  320. keyboardType: TextInputType.text,
  321. decoration: InputDecoration(
  322. labelText: 'First Name',
  323. labelStyle: TextStyle(
  324. fontFamily: "Oswald",
  325. ),
  326. border: OutlineInputBorder(
  327. borderRadius: BorderRadius.circular(25.0),
  328. ),
  329. ),
  330. ),
  331. ),
  332. SizedBox(
  333. width: 10.0,
  334. ),
  335. Expanded(
  336. flex: 1,
  337. child: TextFormField(
  338. keyboardType: TextInputType.text,
  339. decoration: InputDecoration(
  340. labelText: 'Last Name',
  341. labelStyle: TextStyle(
  342. fontFamily: "Oswald",
  343. ),
  344. border: OutlineInputBorder(
  345. borderRadius: BorderRadius.circular(25.0),
  346. ),
  347. ),
  348. ),
  349. ),
  350. ],
  351. ),
  352. ),
  353. SizedBox(
  354. height: 10.0,
  355. ),
  356. Padding(
  357. padding: EdgeInsets.symmetric(
  358. horizontal: 16.0,
  359. ),
  360. child: TextFormField(
  361. keyboardType: TextInputType.visiblePassword,
  362. obscureText: true,
  363. autocorrect: false,
  364. enableSuggestions: false,
  365. decoration: InputDecoration(
  366. labelText: 'Password',
  367. labelStyle: TextStyle(
  368. fontFamily: "Oswald",
  369. ),
  370. border: OutlineInputBorder(
  371. borderRadius: BorderRadius.circular(25.0),
  372. ),
  373. ),
  374. ),
  375. ),
  376. SizedBox(
  377. height: 10.0,
  378. ),
  379. Padding(
  380. padding: const EdgeInsets.symmetric(
  381. horizontal: 16.0,
  382. ),
  383. child: Row(
  384. mainAxisAlignment: MainAxisAlignment.center,
  385. children: <Widget>[
  386. Expanded(
  387. flex: 1,
  388. child: ElevatedButton(
  389. onPressed: () {
  390. Navigator.pushNamed(context, '/login');
  391. },
  392. style: ElevatedButton.styleFrom(
  393. backgroundColor: Colors.blue,
  394. ),
  395. child: Text(
  396. 'Login',
  397. style: TextStyle(
  398. color: Colors.white,
  399. fontFamily: 'Oswald',
  400. ),
  401. ),
  402. ),
  403. ),
  404. SizedBox(
  405. width: 10.0,
  406. ),
  407. Expanded(
  408. flex: 2,
  409. child: ElevatedButton(
  410. onPressed: () {},
  411. style: ElevatedButton.styleFrom(
  412. backgroundColor: Colors.red,
  413. ),
  414. child: Text(
  415. 'Register',
  416. style: TextStyle(
  417. color: Colors.white,
  418. fontFamily: 'Oswald',
  419. ),
  420. ),
  421. ),
  422. ),
  423. ],
  424. ),
  425. )
  426. ],
  427. ),
  428. ),
  429. );
  430. }
  431. }
  432.  
Advertisement
Add Comment
Please, Sign In to add comment