首页 > 解决方案 > 如何根据对 textformfield 的更改来更改按钮的文本

问题描述

TextEditingController _popUpDeleteSoldText = new TextEditingController(text: "Delete");
return showDialog(context: context, builder: (context) {
      return AlertDialog(
        title: Text(_popUpDeleteSoldText.text + " " + doc['name'] + "?"),
        content: Container(
          height: 145,
          child: Column(
            children: <Widget>[
              Padding(
                padding: const EdgeInsets.only(top: 0.0),
                child: Align(alignment: Alignment.centerLeft, child: Text('Did you sold the property?', style: TextStyle(color: myColors.blue),)),
              ),
              MyTextFormField(
                soldController,
                'For how much?',
                false,
                keyboardType: TextInputType.number,
                digitsOnly: true,
                onChanged: (text) {
                  if (soldController.text != "0.00") {
                    print('text field: $text');
                    setState(() {
                      _popUpDeleteSoldText = new TextEditingController(text: "Sell");
                    });
                  } else {
                    setState(() {
                      _popUpDeleteSoldText = new TextEditingController(text: "Delete");
                    });
                  }
                },
              ),

如您所见,我正在根据正在更改的 textformfield 更改 _popUpDeleteSoldText 控制器(mytextformfield 只是我自己的基于原始字段的字段,以标准化整个应用程序的设计)。该控制器是我在弹出按钮上用来说“出售”或“删除”的控制器,因为我想根据字段上的值更改按钮文本我知道在我使用打印测试时触发了 onchanged,但是,按钮总是说“删除”(初始初始化)并且不会更新为“卖出”

标签: flutter

解决方案


这是另一个解决方案,除了将按钮文本保持为状态外,我还保留了属性值。我还添加了一个 TextInputFormatter 以便只接受正数。

在此处输入图像描述

import 'dart:math';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

void main() {
  runApp(
    MaterialApp(
      debugShowCheckedModeBanner: false,
      title: 'Flutter Demo',
      home: Scaffold(
        body: MyWidget(),
      ),
    ),
  );
}

class MyWidget extends StatefulWidget {
  @override
  _MyWidgetState createState() => _MyWidgetState();
}

class _MyWidgetState extends State<MyWidget> {
  int propertyValue = 0;

  @override
  Widget build(BuildContext context) {
    return Center(
      child: Column(
        mainAxisSize: MainAxisSize.min,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: <Widget>[
          Text('Did you sell the property???'),
          IntrinsicWidth(
            child: TextFormField(
              initialValue: propertyValue.toString(),
              keyboardType: TextInputType.number,
              inputFormatters: <TextInputFormatter>[
                FilteringTextInputFormatter.digitsOnly,
                PositiveNumberFormatter(),
              ],
              textAlign: TextAlign.center,
              onChanged: (value) =>
                  setState(() => propertyValue = int.parse(value)),
            ),
          ),
          SizedBox(height: 16.0),
          propertyValue > 0
              ? ElevatedButton.icon(
                  icon: Icon(Icons.attach_money),
                  label: Text('Sell'),
                  onPressed: () => print('SELL'),
                )
              : ElevatedButton.icon(
                  icon: Icon(Icons.delete),
                  label: Text('Delete'),
                  onPressed: () => print('DELETE'),
                ),
        ],
      ),
    );
  }
}

class PositiveNumberFormatter extends TextInputFormatter {
  @override
  TextEditingValue formatEditUpdate(
      TextEditingValue oldValue, TextEditingValue newValue) {
    return TextEditingValue(
      text: max(0, double.tryParse(newValue.text) ?? 0).toString(),
      selection: newValue.text.length == 0
          ? TextSelection.collapsed(offset: 1)
          : newValue.selection,
    );
  }
}

推荐阅读