Flutter – Validate() kullanımı

import 'package:flutter/material.dart';
class FormveTextFormField extends StatefulWidget {
  @override
  _FormveTextFormFieldState createState() => _FormveTextFormFieldState();
}
class _FormveTextFormFieldState extends State<FormveTextFormField> {
  String _adsoyad, _mailadres, _password;
 bool otomatikkontrol = true;
  final formkey = GlobalKey<FormState>(); //tüm formlarda geçerli bir key oluşturuyoruz
  //bu key sadece aşağıdaki formun keyi olarak düşünebilirsiniz...
  
  @override
  Widget build(BuildContext context) {
    return Theme(
      data: Theme.of(context).copyWith(
          accentColor: Colors.green,
          errorColor: Colors.green,
          
           primaryColor: Colors.red.shade400),
        
      child: Scaffold(
        floatingActionButton: FloatingActionButton(
          onPressed: () {},
          child: Icon(Icons.save),
        ),
        appBar: AppBar(
          title: Text("Form ve Text Form İşlemleri"),
        ),
        body: Padding(
            padding: const EdgeInsets.all(20.0),
            child: Form(
              key: formkey,
              autovalidate: otomatikkontrol,
              child: ListView(
                children: <Widget>[
                  TextFormField(
                    decoration: InputDecoration(
                      prefixIcon: Icon(Icons.account_circle),
                      labelText: "Ad Soyad :",
                      labelStyle: TextStyle(
                        fontSize: 22,
                      ),
                      border: OutlineInputBorder(),
                      hintText: "Adınız Soyadınız...",
                    ),
                    validator: (String girilenveri) {
                      if (girilenveri.length < 5) {
                        return "Girilen veri 5 ten küçük";
                      }
                    },
                    onSaved: (deger) => _adsoyad=deger,
                  ),
                  SizedBox(
                    height: 10,
                  ),
                  TextFormField(
                    keyboardType: TextInputType.emailAddress,
                    decoration: InputDecoration(
                      prefixIcon: Icon(Icons.email),
                      labelText: "Email Adresiniz :",
                      labelStyle: TextStyle(
                        fontSize: 22,
                      ),
                      border: OutlineInputBorder(),
                      enabledBorder: OutlineInputBorder(
                          borderSide: BorderSide(color: Colors.green)),
                      focusedBorder: OutlineInputBorder(
                          borderSide: BorderSide(color: Colors.amber)),
                      hintText: "Email Adresini...",
                    ),
                    validator: (String girilenveri) => "",
                    onSaved: (deger) => _mailadres=deger,
                  ),
                  SizedBox(
                    height: 10,
                  ),
                  TextFormField(
                    keyboardType: TextInputType.visiblePassword,
                    obscureText: true,
                    decoration: InputDecoration(
                      prefixIcon: Icon(Icons.lock),
                      labelText: "Şifreniz :",
                      labelStyle: TextStyle(
                        fontSize: 22,
                      ),
                      border: OutlineInputBorder(),
                      enabledBorder: OutlineInputBorder(
                          borderSide: BorderSide(color: Colors.green)),
                      focusedBorder: OutlineInputBorder(
                          borderSide: BorderSide(color: Colors.amber)),
                      hintText: "Şifreniz...",
                    ),
                    validator: (String girilenveri) => "",
                    onSaved: (deger) => _password=deger,
                  ),
                  SizedBox(
                    height: 10,
                  ),
                  RaisedButton(
                    color: Colors.greenAccent,
                    disabledColor: Colors.redAccent,
                    child: Text("Butonum benim"),
                    onPressed: () {
                      debugPrint("Butona Basıldı :");
                      _girilenVerileriOnayla();
                    },
                  ),
                ],
              ),
            )),
      ),
    );
  }
  void _girilenVerileriOnayla() {
    if(formkey.currentState.validate()){
      formkey.currentState.save();
      debugPrint("ad :$_adsoyad mail $_mailadres Şifre : $_password");
    } else
    {
      setState(() {
      otomatikkontrol=true;
        
      });
      debugPrint(formkey.currentState.validate().toString());
    }
  }
}

Bir cevap yazın

E-posta hesabınız yayımlanmayacak. Gerekli alanlar * ile işaretlenmişlerdir