main.dart
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'settings.dart';
import 'login.dart';
import 'package:fluttertoast/fluttertoast.dart';
void main() => runApp(MyApp());
class MyApp extends StatefulWidget {
@override
State<StatefulWidget> createState() {
return _MyAppState();
}
}
class _MyAppState extends State<MyApp> {
late String _title = 'Please Login';
late Widget _screen;
late Login _login;
late Settings _settings;
late bool _authenticated;
_MyAppState() {
_login = new Login(onSubmit);
_settings = new Settings();
_screen = _login;
_authenticated = false;
}
onSubmit() {
if (_login.username == 'user' && _login.password == 'password') {
_setAuthenticated(true);
showToast("Login : " + _login.username + " " + _login.password + "\nAuthenticated!!!");
} else {
showToast("Login : " + _login.username + " " + _login.password + "\nNot authenticated...");
}
}
void _goHome() {
print('_goHome()');
setState(() {
if (_authenticated == true) {
_screen = _settings;
showToast("This is Setting Page.");
} else {
_screen = _login;
showToast("This is Login Page.");
}
});
}
void logout() {
print('logout()');
_setAuthenticated(false);
showToast("Logout");
}
void _setAuthenticated(bool auth) {
setState(() {
if (auth == true) {
_screen = _settings;
_title = 'Welcome';
} else {
_screen = _login;
_title = 'Please Login';
}
});
}
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Login App',
home: Scaffold(
appBar: AppBar(
title: Text(_title),
actions: <Widget>[
IconButton(
icon: Icon(Icons.home),
onPressed: _goHome,
),
IconButton(
icon: Icon(Icons.exit_to_app),
onPressed: logout,
)
],
),
body: _screen));
}
showToast(String msg) {
Fluttertoast.showToast(msg: msg,
backgroundColor: Colors.white,
toastLength: Toast.LENGTH_LONG,
gravity: ToastGravity.BOTTOM_RIGHT);
}
}
login.dart
import 'package:flutter/material.dart';
class Login extends StatelessWidget{
const Login(this.onSubmit);
final VoidCallback onSubmit;
static final TextEditingController _user = new TextEditingController();
static final TextEditingController _pass = new TextEditingController();
String get username => _user.text;
String get password => _pass.text;
@override
Widget build(BuildContext context) {
return Column(
children: <Widget>[
TextField(controller: _user, decoration: InputDecoration(hintText: 'Enter a username'),),
TextField(controller: _pass, decoration: InputDecoration(hintText: 'Enter a password'), obscureText: true,),
RaisedButton(child: Text('Submit'), onPressed: onSubmit,)
],
);
}
}
settings.dart
import 'package:flutter/material.dart';
class Settings extends StatelessWidget{
@override
Widget build(BuildContext context) {
return Center(
child: Text('settings here'),
);
}
}
결과
https://cording-cossk3.tistory.com/112
320x100
'프로그래밍 > Flutter-Dart' 카테고리의 다른 글
[Flutter] DatePicker (iOS style) (0) | 2021.03.23 |
---|---|
[Flutter] Image Picker (2) | 2021.03.23 |
[Flutter] wifi connect 정보 (0) | 2021.03.22 |
[Flutter] Webview JavaScript Handler 등록 (0) | 2021.03.22 |
[Flutter] WebView JS 통신 (2) | 2021.03.19 |
댓글