Skip to content

Instantly share code, notes, and snippets.

@prasadsunny1
Last active May 17, 2020 09:59
Show Gist options
  • Save prasadsunny1/66b2f2f18c2ea703b2cc650833c28719 to your computer and use it in GitHub Desktop.
Save prasadsunny1/66b2f2f18c2ea703b2cc650833c28719 to your computer and use it in GitHub Desktop.
Validate the input
import 'dart:async';
import 'package:flutter/material.dart';
void main() {
runApp(MaterialApp(
home: Page1(),
));
}
class Page1 extends StatefulWidget {
@override
_Page1State createState() => _Page1State();
}
class _Page1State extends State<Page1> {
TextEditingController usernameController = TextEditingController();
TextEditingController passwordController = TextEditingController();
bool isBusy = false;
bool hasError = false;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Page 1"),
),
body: Center(
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
TextField(
controller: usernameController,
decoration: InputDecoration(
labelText: "Username",
hintText: "Your Username",
),
),
TextField(
controller: passwordController,
obscureText: true,
obscuringCharacter: '*',
decoration: InputDecoration(
labelText: "Password",
hintText: "Your password",
),
),
RaisedButton(
child: Text("Go to page 2"),
onPressed: () {
setState(() {
isBusy = true;
hasError = false;
});
Timer(Duration(seconds: 2), () {
var username = usernameController.text;
var password = passwordController.text;
if (username.isEmpty || password.isEmpty) {
setState(() {
hasError = true;
});
}
setState(() {
isBusy = false;
});
if (!hasError) {
Navigator.of(context).push(
MaterialPageRoute(
builder: (context) {
return Page2();
},
),
);
}
});
},
),
if (isBusy) CircularProgressIndicator(),
if (hasError) Text('There is an error'),
],
),
),
),
);
}
}
class Page2 extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Page 2"),
),
body: Center(
child: Container(
child: Column(
children: [
RaisedButton(
child: Text("Go Back"),
onPressed: () {
Navigator.of(context).pop();
},
)
],
),
),
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment