Skip to content

Instantly share code, notes, and snippets.

@kika
Created July 29, 2019 04:34
Show Gist options
  • Save kika/01b7461d261b433d47f9596ecff0c4a6 to your computer and use it in GitHub Desktop.
Save kika/01b7461d261b433d47f9596ecff0c4a6 to your computer and use it in GitHub Desktop.

Revisions

  1. kika created this gist Jul 29, 2019.
    134 changes: 134 additions & 0 deletions main.dart
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,134 @@
    import 'package:flutter/material.dart';

    class MyKeys {
    static final GlobalKey navKey = GlobalKey<NavigatorState>(debugLabel: 'navKey');
    }

    void main() => runApp(MyApp());

    class MyApp extends StatelessWidget {
    // This widget is the root of your application.
    @override
    Widget build(BuildContext context) {
    return MaterialApp(
    title: 'Flutter Demo',
    theme: ThemeData(
    primarySwatch: Colors.blue,
    ),
    routes: {
    "/": (_) => MyHomePage(key: MyKeys.navKey, title: 'Home Page'),
    '/auth': (_) => MyAuth(),
    },
    );
    }
    }

    class MyHomePage extends StatefulWidget {
    MyHomePage({Key key, this.title}) : super(key: key);

    final String title;

    @override
    _MyHomePageState createState() => _MyHomePageState();
    }

    class _MyHomePageState extends State<MyHomePage> {
    int _counter = 0;

    void _incrementCounter() {
    setState(() {
    _counter++;
    });
    }

    @override
    Widget build(BuildContext context) {
    return Scaffold(
    appBar: AppBar(
    title: Text(widget.title),
    ),
    body: Center(
    child: Column(
    mainAxisAlignment: MainAxisAlignment.center,
    children: <Widget>[
    FlatButton(
    child: Text('Login'),
    onPressed: () => Navigator.of(context).pushNamed('/auth'),
    ),
    Text(
    'You have pushed the button this many times:',
    ),
    Text(
    '$_counter',
    style: Theme.of(context).textTheme.display1,
    ),
    ],
    ),
    ),
    floatingActionButton: FloatingActionButton(
    onPressed: _incrementCounter,
    tooltip: 'Increment',
    child: Icon(Icons.add),
    ),
    );
    }
    }

    class MyAuth extends StatefulWidget {
    @override
    _MyAuthState createState() => _MyAuthState();
    }

    class _MyAuthState extends State<MyAuth> {
    String login;
    String password;
    final _formKey = GlobalKey<FormState>();

    void _doLogin() {
    final form = _formKey.currentState;
    if(form.validate()) {
    form.save();
    Navigator.of(context).pushNamed('/');
    }
    }

    @override
    Widget build(BuildContext context) {
    return Scaffold(
    appBar: AppBar(title: Text('Login')),
    body: Padding(
    padding: const EdgeInsets.all(24.0),
    child: Form(
    key: _formKey,
    child: Column(
    children: <Widget>[
    TextFormField(
    decoration: InputDecoration(
    labelText: 'Enter login',
    ),
    autocorrect: false,
    autofocus: true,
    validator: (v) => v.trim().length < 3 ? 'Enter more than 3 chars': null,
    onSaved: (v) => login = v.trim(),
    ),
    TextFormField(
    decoration: InputDecoration(
    labelText: 'Enter password',
    ),
    obscureText: true,
    autocorrect: false,
    validator: (v) => v.trim().length < 3 ? 'Password should be no less than 3 chars': null,
    onSaved: (v) => password = v.trim(),
    ),
    FlatButton(
    child: Text('Login'),
    onPressed: () => _doLogin(),
    )
    ],
    ),

    ),
    ),
    );
    }
    }