Flutter TextEditingController不会清除readOnly TextFormField中的文本

时间:2020-06-14 23:45:14

标签: android flutter dart

我是新手。我正在尝试为表单创建一个取消按钮。我可以成功清除所有TextFormField,但该只读TextFormField的值由TextEditingController设置。

这是TextFormField。我可以将TextFormField()的值设置为用户成功选择的日期。

import Svg, { Path } from 'react-native-svg';
import * as shape from "d3-shape";
...
const getPath = () => {
    const left = shape.line().x(d => d.x).y(d => d.y)([
        { x: 0, y: 0 },
        { x: width, y: 0 },
    ]);
    const tab = shape.line().x(d => d.x).y(d => d.y).curve(shape.curveBasis)([
        { x: width, y: 0 },
        { x: width + 5, y: 0 },
        { x: width + 10, y: 10 },
        { x: width + 15, y: height },
        { x: width + tabWidth - 15, y: height },
        { x: width + tabWidth - 10, y: 10 },
        { x: width + tabWidth - 5, y: 0 },
        { x: width + tabWidth, y: 0 },
    ]);
    const right = shape.line().x(d => d.x).y(d => d.y)([
        { x: width + tabWidth, y: 0 },
        { x: width * 2, y: 0 },
        { x: width * 2, y: height },
        { x: 0, y: height },
        { x: 0, y: 0 },
    ]);
    return `${left} ${tab} ${right}`;
};

const d = getPath()
...
    return (
        <SafeAreaView style={styles.safeArea}>
            <Svg {...{ width, height }}>
                <Path {...{ d }} fill="white" />
            </Svg>
        </SafeAreaView>
    );

这是取消按钮。

final _start_time_controller = TextEditingController();
final _finish_time_controller = TextEditingController();

Flexible(
    child: TextFormField(
        onTap: () async {
            final selectedDate = await _selectDateTime(context);
            if(selectedDate == null) return;
            final selectedTime = await _selectTime(context);
            if(selectedTime == null) return;
            setState(() {
                _start_time = DateTime(selectedDate.year,selectedDate.month,selectedDate.day,selectedTime.hour,selectedTime.minute);
                _start_time_controller.text = _start_time.toString();
            });
        },
    decoration: InputDecoration(
        labelText: 'Start Time',
            border: OutlineInputBorder(borderRadius: BorderRadius.circular(10.0))
    ),
    controller: _start_time_controller,
    readOnly: true,
    validator: (value) => value.isEmpty ? 'Start Time can\'t be empty' : null,
    ),                         
),

这是取消功能。

Expanded(
    child: RoundedButton(
        text: 'Cancel',
        buttonColor: Colors.grey,
        textColor: Colors.black,
        press: cancel,
    ),
) ,        

我试图将两个TextEditingController.clear()调用包装在setstate()函数中,但是它不起作用。

0 个答案:

没有答案