In today’s video we went over the more complex way of using the text data that comes from the what a user types in the TextFormField widget.

Although it is not a lot more code to write there is a lot more happening. Here is the code from the video:

import 'package:circles/widgets/bottomNavBar.dart';
import 'package:flutter/material.dart';

class PreferencesScreen extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return PreferencesScreenState();
  }
}

class PreferencesScreenState extends State<PreferencesScreen> {
  final _controller = TextEditingController();
  String textValue;

  @override
  void initState() {
    _controller.addListener(() {
      print('TEXT: ${_controller.text}');

      setState(() {
        textValue = _controller.text;
      });

      _controller.value = _controller.value.copyWith(text: textValue);
    });

    super.initState();
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        automaticallyImplyLeading: false,
        title: Text('Preferences'),
      ),
      body: Container(
        child: Column(
          children: <Widget>[
            Text('$textValue'),
            TextFormField(
              controller: _controller,
            )
          ],
        ),
      ),
      bottomNavigationBar: BottomNavBar(),
    );
  }
}