-2

I want to disable Rate/kg for writing and give it a value by calculating like this

Rate = amount/weight +carriage/weight +unloading/weight

I dont know how to perform these operations on text field plz help link

Abhishek Bhagate
  • 5,583
  • 3
  • 15
  • 32
  • check this https://stackoverflow.com/questions/44490622/disable-a-text-edit-field-in-flutter – Merym Jun 17 '20 at 22:00

2 Answers2

2

Simple solution would be executing calculate method onChange of every TextField.

Here is the example in which we have three TextField, and we want to show sum of first two TextField in third TextField.

You can disable the TextField with enabled property or you can use AbsorbPointer.

import 'package:flutter/material.dart';

class StackOverFlow extends StatefulWidget {
  @override
  _StackOverFlowState createState() => _StackOverFlowState();
}

class _StackOverFlowState extends State<StackOverFlow> {
  TextEditingController _firstController = TextEditingController();
  TextEditingController _secondController = TextEditingController();
  TextEditingController _thirdController = TextEditingController();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(),
      body: Container(
        child: Column(
          children: <Widget>[
            TextField(
              controller: _firstController,
              onChanged: (value) {
                _calculate();
              },
              keyboardType: TextInputType.number,
            ),
            TextField(
              controller: _secondController,
              onChanged: (value) {
                _calculate();
              },
              keyboardType: TextInputType.number,
            ),
            AbsorbPointer(
              child: TextField(
                controller: _thirdController,
                keyboardType: TextInputType.number,
              ),
            ),
          ],
        ),
      ),
    );
  }

  void _calculate() {
    if (_firstController.text.trim().isNotEmpty &&
        _secondController.text.trim().isNotEmpty) {
      final firstValue = double.parse(_firstController.text);
      final secondValue = double.parse(_secondController.text);
      _thirdController.text = (firstValue + secondValue).toString();
    }
  }
}
Varun Kamani
  • 86
  • 1
  • 8
0

In-Text widget, You can use the Arithmetic operations like this,

Container(
child: Text(
(currentTemp + kelvin).toStringAsFixed(1),
style: TextStyle(
fontSize: 60,
color: Colors.white,
),
),
),
Suramack
  • 111
  • 1
  • 5