0

i have tried this different ways but it just will not work. i like the input text written inside the textfield by user to stay after submit/upload so when the user comes back to the page the text should be there and user gets to clear it manually , not sure whats gone wrong here. thanks in advance .

    class MondayScreen extends StatefulWidget {
  static final String id = 'monday_screen';

  @override
  _MondayScreenState createState() => _MondayScreenState();

}

TextEditingController _oneController = TextEditingController();
class _MondayScreenState extends State<MondayScreen> {

  String _alOne = '';


  _submit()  {
    Post post = Post(
      authorId: Provider.of<UserData>(context ,listen: false).currentUserId,
      timestamp: Timestamp.fromDate(DateTime.now()),
      alOne: _alOne,

    );
    DatabaseService.createPostMonday(post);

    setState(() {
_oneController.text;

    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Colors.black,
        title: Text(
          'Monday Lunch',
          style: TextStyle(color: Colors.white),
        ),
        actions: <Widget>[
          FlatButton(
            child: Text(
              'Spara',
              style:
                  TextStyle(color: Colors.white, fontWeight: FontWeight.w600),
            ),
            onPressed: _submit,
            color: Colors.black,
          )
        ],
      ),
      body: SingleChildScrollView(
        child: Padding(
          padding: const EdgeInsets.only(top: 60.0),
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              Padding(
                padding: EdgeInsets.symmetric(horizontal: 30.0),
                child: TextField(
                  controller: TextEditingController(),
                  style: TextStyle(fontSize: 18.0),
                  decoration: InputDecoration(
                    labelText: 'Alternativ',
                  ),
                  onSubmitted: (input) => _alOne = input,


                ),
              ),
Error Place
  • 45
  • 1
  • 1
  • 8

1 Answers1

2

I had similar problem when I was building a form using StatelessWidget.

Ensure you use StatefulWidget and _oneController is a field of the state. Reference https://flutter.dev/docs/cookbook/forms/validation

Pass _oneController to the TextFormField

controller: _oneController

suztomo
  • 5,114
  • 2
  • 20
  • 21