Two fields of TextForm Field, like two sides



  • I can't figure out how to interact between two TextForm Field. It is necessary (for example): to add the number in the first field, to display the triple value of the first field in the second field, and vice versa to the second field, to display the divided value of the second field. The problem is, the fields either alter each other in a way that's not clear. It's just that it's from one field to another and vice versa. But with the calculations, there's always been some mistakes. Maybe I work wrong with numbers? Commented on what leads to errors.

    import 'package:flutter/material.dart';
    

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

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

    class _pageState extends State<MyPage>
    {
    final myController1 = TextEditingController();
    final myController2 = TextEditingController();

    @override
    void initState() {
    super.initState();

    // Начинаем слушать изменения.
    myController1.addListener(() =&gt; _listener('textField1'));
    myController2.addListener(() =&gt; _listener('textField2'));
    

    }

    //Слушатель
    void _listener(String textField) {
    switch (textField) {
    case ('textField1'):
    myController2.value = TextEditingValue(
    //---!!!---
    //text: (int.parse(myController1.text) * 3).toString(),
    text: myController1.text,
    selection: TextSelection.fromPosition(
    TextPosition(offset: myController1.text.length),
    ),
    );
    break;
    case ('textField2'):
    myController1.value = TextEditingValue(
    //---!!!---
    //text: (int.parse(myController2.text) / 3).toString(),
    text: myController2.text,
    selection: TextSelection.fromPosition(
    TextPosition(offset: myController2.text.length),
    ),
    );
    break;
    }
    }

    @override
    void dispose() {
    myController1.dispose();
    myController2.dispose();
    super.dispose();
    }

    @override
    Widget build(BuildContext context) {
    return Scaffold(
    body: Padding(
    padding: const EdgeInsets.all(16.0),
    child: Column(
    children: [
    TextFormField(
    keyboardType: TextInputType.number,
    controller: myController1,
    ),
    TextFormField(
    keyboardType: TextInputType.number,
    controller: myController2,
    ),
    ],
    ),
    ),
    );
    }
    }

    введите сюда описание изображения



  • You have a problem with every textEditingController responding to changes. That's a simple solution to do another thing:

    import 'package:flutter/material.dart';
    

    void main() {
    runApp(MyApp());
    }

    class MyApp extends StatelessWidget {
    @override
    Widget build(BuildContext context) {
    return const MaterialApp(
    debugShowCheckedModeBanner: false,
    home: MyPage(),
    );
    }
    }

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

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

    class _PageState extends State<MyPage> {
    late final myController1 = TextEditingController();
    late final myController2 = TextEditingController();

    @override
    void dispose() {
    myController1.dispose();
    myController2.dispose();
    super.dispose();
    }

    @override
    Widget build(BuildContext context) {
    return Scaffold(
    body: Padding(
    padding: const EdgeInsets.all(16.0),
    child: Column(
    children: [
    TextFormField(
    keyboardType: TextInputType.number,
    controller: myController1,
    onChanged: (String s) {
    if (s.isEmpty) {
    myController2.text = '';
    } else {
    myController2.text = (int.parse(s) * 3).toString();
    }
    },
    ),
    TextFormField(
    keyboardType: TextInputType.number,
    controller: myController2,
    onChanged: (String s) {
    if (s.isEmpty) {
    myController1.text = '';
    } else {
    myController1.text = (int.parse(s) / 3).toString();
    }
    },
    ),
    ],
    ),
    ),
    );
    }
    }




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2