dibaloke
1/1/2019 - 6:13 PM

TextField Stateful widget

A beginner level example on Stateful Widget with textfield and text

import 'package:flutter/material.dart';

void main() {
  runApp(MaterialApp(
    title: "Stateful Widget Example",
    home: Scaffold(
      appBar: AppBar(
        title: Center(child: Text("Stateful Widget")),
        backgroundColor: Colors.greenAccent,
      ),
      body: FavouriteCity(),
    ),
  ));
}

class FavouriteCity extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _FavouriteCity();
  }
}

class _FavouriteCity extends State<FavouriteCity> {
  String nameCity = "";
  @override
  Widget build(BuildContext context) {
    return Column(
      children: <Widget>[
        TextField(

          //onChanged: 
          onSubmitted: (String userInput) {
            setState(() {
              nameCity = userInput;
            });
          },
        ),
        Padding(
          padding: const EdgeInsets.only(top: 50.0),
          child: Center(
            child: Text(
              "Your Favourite City name is $nameCity",
              style: TextStyle(
                fontSize: 15.0,
              ),
            ),
          ),
        ),
      ],
    );
  }
}