1

I am working on a custom dialog called "Alertbox" where the user inserts a name into a textfield and after he pushes the button a function called "addTeam" created a team out of the string.

This is how I created my dialog "Alertbox":

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:trainings_app/config/palette.dart';

class Alertbox extends StatelessWidget {
  final Function addTeamFunction;
  const Alertbox(this.addTeamFunction);

  @override
  Widget build(BuildContext context) {
    return Dialog(
      backgroundColor: Colors.transparent,
      elevation: 0,
      insetPadding: EdgeInsets.all(10),
      child: Center(
        child: Container(
          decoration: new BoxDecoration(
            borderRadius: new BorderRadius.all(const Radius.circular(20)),
            color: Colors.white,
          ),
          width: 350,
          height: 200,
          child: Row(
            children: [
              SizedBox(width: 12),
              Expanded(
                child: TextField(
                  textAlign: TextAlign.center,
                  autofocus: true,
                ),
              ),
              SizedBox(width: 12),
              ElevatedButton(
                onPressed: () => addTeamFunction(),
                child: const Text('✓'),
                style: ButtonStyle(
                  backgroundColor: MaterialStateProperty.all(Palette.orange),
                ),
              ),
              SizedBox(width: 8),
            ],
          ),
        ),
      ),
    );
  }
}

And here I am using it:

void newTeam() {
  showDialog<AlertDialog>(
    context: context,
    builder: (BuildContext context) {
      return Alertbox(() {
        Navigator.of(context).pop();
      });
    },
  );
}

void addTeam(String name) {
  setState(() {
    teams.add(name);
  });
  Navigator.of(context).pop();

  sharedPreferences.setStringList('teams', teams);
}

But I can't find a way to parse the input from the textfield into the function "addTeam" where it is needed. Can anyone help me please?

Obscuritas
  • 59
  • 6
  • You want to added text in textfield , this text is display on alertbox when you button press correct? – Ravindra S. Patil Aug 03 '21 at 11:31
  • Yes, inside alertbox there is a textfield and when I push the button also inside alertbox the input from that field should go to the "addTeam" function – Obscuritas Aug 03 '21 at 11:33

2 Answers2

1

Use a TextFormField instead of a TexiField widget contained in a Form widget that has a GlobalKey, which will be useful to you during validation!

How to get the value which is already entered on the keyboard?
Uses a TextEditingController or the onSaved method of the TextFormField.

Marcel Hofgesang
  • 951
  • 1
  • 15
  • 36
1

You Should try below code hope its helps you:

import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
      title: 'Testing',
      home: MyCustomForm(),
    );
  }
}

class MyCustomForm extends StatefulWidget {
  const MyCustomForm({Key? key}) : super(key: key);

  @override
  _MyCustomFormState createState() => _MyCustomFormState();
}


class _MyCustomFormState extends State<MyCustomForm> {
  
  final myController = TextEditingController();

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

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Retrieve Text Input'),
      ),
      body: Padding(
        padding: const EdgeInsets.all(16.0),
        child: TextField(
          controller: myController,
        ),
      ),
      floatingActionButton: FloatingActionButton(
       
        onPressed: () {
          showDialog(
            context: context,
            builder: (context) {
              return AlertDialog(
               
                content: Text(myController.text),
              );
            },
          );
        },
        tooltip: 'Show the value!',
        child: const Icon(Icons.add),
      ),
    );
  }
}

Your Screen like -> enter image description here

Ravindra S. Patil
  • 11,757
  • 3
  • 13
  • 40
  • Thanks to you, I know how to get the text inside the field now. But I'm still unsure how to parse the value of it into another function with the current structure I have – Obscuritas Aug 04 '21 at 12:43