How To Add Checked Enter Value Number Or Not In Flutter

admin_img Posted By Bajarangi soft , Posted On 10-09-2020

The _isNumeric() function would return true if the value is numbered and return false if the entered string contains non numeric numbers. So in this tutorial we would Flutter Dart Check Entered String Value is Number or Not in Android iOS Example Tutorial in Text Input TextField widget.

How To Add Checked Enter Value Number Or Not In Flutter

Complete Code FOr Numaric Value In Flutter
main.dart

import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
        home: Scaffold(
            appBar: AppBar(
              centerTitle: true,
                backgroundColor: Colors.amber,
                title: Text('Check Value Is Number Or Not',style: TextStyle(color: Colors.black),
                )),
            body: Center(child: TextFieldClass())));
  }
}

class TextFieldClass extends StatefulWidget {
  TextFieldClassState createState() => TextFieldClassState();
}

class TextFieldClassState extends State<TextFieldClass> {
  final textFieldHolder = TextEditingController();

  String value = '';
  String output = 'Not Checked';

  checkTextInputData() {
    setState(() {
      value = textFieldHolder.text;
    });

    print(_isNumeric(value));

    if (_isNumeric(value) == true) {
      setState(() {
        output = 'Value is Number';
      });
    } else {
      setState(() {
        output = 'Value is Not Number';
      });
    }
  }

  bool _isNumeric(String result) {
    if (result == null) {
      return false;
    }
    return double.tryParse(result) != null;
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        body: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              Container(
                  width: 280,
                  padding: EdgeInsets.all(10.0),
                  child: TextField(
                    controller: textFieldHolder,
                    autocorrect: true,
                    decoration: InputDecoration(
                        hintText: 'Enter Some Text'),
                  )),
              RaisedButton(
                onPressed: checkTextInputData,
                color: Colors.amber,
                textColor: Colors.black,
                padding: EdgeInsets.fromLTRB(10, 10, 10, 10),
                child: Text('Click Here Value Is Number Or Not'),
              ),
              Padding(
                  padding: EdgeInsets.all(8.0),
                  child: Text("$output", style: TextStyle(fontSize: 20)))
            ],
          ),
        ));
  }
}

Related Post