Edit Text Button – Flutter

huangapple go评论60阅读模式
英文:

Edit Text Button - Flutter

问题

I want the user to be able to edit the text within the note tile (see screenshot) when the user clicks the edit button. I've watched tutorial after tutorial and I can't get it to work. How do I do that? Your help is very much appreciated!

Here's the code:

import 'package:flutter/material.dart';

class PatientNotesTile extends StatelessWidget {
  final String date;
  final String text;

  const PatientNotesTile({super.key, required this.date, required this.text});

  @override
  Widget build(BuildContext context) {
    return Container(
      decoration: BoxDecoration(
        color: Colors.deepPurple.shade300.withOpacity(0.4),
        borderRadius: BorderRadius.circular(15),
      ),
      child: ListTile(
        title: Padding(
          padding: const EdgeInsets.only(bottom: 5.0),
          child: Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Text(
                date,
                style: TextStyle(
                    fontWeight: FontWeight.bold,
                    fontSize: 15.0,
                    color: Colors.deepPurple.shade100),
              ),

              // Edit Button
              IconButton(
                icon: Icon(Icons.edit, color: Colors.deepPurple.shade100),
                onPressed: () {},
              ),
            ],
          ),
        ),
        subtitle: Padding(
          padding: const EdgeInsets.only(bottom: 15.0),
          child: Text(
            text,
            style: const TextStyle(
                fontWeight: FontWeight.bold,
                fontSize: 18.0,
                color: Colors.white),
          ),
        ),
      ),
    );
  }
}
英文:

I want the user to be able to edit the text within the note tile (see screenshot) when the user clicks the edit button. I've watched tutorial after tutorial and I can't get it to work. How do I do that? Your help is very much appreciated!

Here's the code:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

import &#39;package:flutter/material.dart&#39;;

class PatientNotesTile extends StatelessWidget {
  final String date;
  final String text;

  const PatientNotesTile({super.key, required this.date, required this.text});

  @override
  Widget build(BuildContext context) {
    return Container(
      decoration: BoxDecoration(
        color: Colors.deepPurple.shade300.withOpacity(0.4),
        borderRadius: BorderRadius.circular(15),
      ),
      child: ListTile(
        title: Padding(
          padding: const EdgeInsets.only(bottom: 5.0),
          child: Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Text(
                date,
                style: TextStyle(
                    fontWeight: FontWeight.bold,
                    fontSize: 15.0,
                    color: Colors.deepPurple.shade100),
              ),

              // Edit Button
              IconButton(
                icon: Icon(Icons.edit, color: Colors.deepPurple.shade100),
                onPressed: () {},
              ),
            ],
          ),
        ),
        subtitle: Padding(
          padding: const EdgeInsets.only(bottom: 15.0),
          child: Text(
            text,
            style: const TextStyle(
                fontWeight: FontWeight.bold,
                fontSize: 18.0,
                color: Colors.white),
          ),
        ),
      ),
    );
  }
}

<!-- end snippet -->

答案1

得分: 1

你可以在TextField上使用 readOnly: readOnly,当用户点击编辑图标时,将其设置为可编辑。其他装饰可以根据readOnly布尔值进行调整。

class PatientNotesTile extends StatefulWidget {
  final String date;
  final String text;

  const PatientNotesTile({super.key, required this.date, required this.text});

  @override
  State<PatientNotesTile> createState() => _PatientNotesTileState();
}

class _PatientNotesTileState extends State<PatientNotesTile> {
  late final TextEditingController controller = TextEditingController.fromValue(TextEditingValue(text: widget.text, selection: TextSelection.collapsed(offset: widget.text.length)));

  bool readOnly = true;
  @override
  Widget build(BuildContext context) {
    return Container(
      decoration: BoxDecoration(
        color: Colors.deepPurple.shade300.withOpacity(0.4),
        borderRadius: BorderRadius.circular(15),
      ),
      child: ListTile(
        title: Padding(
          padding: const EdgeInsets.only(bottom: 5.0),
          child: Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Text(
                widget.date,
                style: TextStyle(fontWeight: FontWeight.bold, fontSize: 15.0, color: Colors.deepPurple.shade100),
              ),

              // Edit Button
              IconButton(
                icon: Icon(
                  readOnly ? Icons.edit : Icons.save,
                  color: Colors.deepPurple.shade100,
                ),
                onPressed: () {
                  setState(() {
                    readOnly = !readOnly;
                  });
                },
              ),
            ],
          ),
        ),
        subtitle: Padding(
          padding: const EdgeInsets.only(bottom: 15.0),
          child: TextField(
            controller: controller,
            readOnly: readOnly,
            decoration: InputDecoration(
              border: readOnly ? InputBorder.none : OutlineInputBorder(borderRadius: BorderRadius.circular(15.0)),
              hintText: "Enter notes here",
              hintStyle: TextStyle(color: Colors.deepPurple.shade100),
            ),
            style: const TextStyle(fontWeight: FontWeight.bold, fontSize: 18.0, color: Colors.white),
          ),
        ),
      ),
    );
  }
}
英文:

You can use readOnly: readOnly on TextFiled. when user press edit icon, make it writable. Others decoration can be tweak based on read only bool.

class PatientNotesTile extends StatefulWidget {
  final String date;
  final String text;

  const PatientNotesTile({super.key, required this.date, required this.text});

  @override
  State&lt;PatientNotesTile&gt; createState() =&gt; _PatientNotesTileState();
}

class _PatientNotesTileState extends State&lt;PatientNotesTile&gt; {
  late final TextEditingController controller = TextEditingController.fromValue(TextEditingValue(text: widget.text, selection: TextSelection.collapsed(offset: widget.text.length)));

  bool readOnly = true;
  @override
  Widget build(BuildContext context) {
    return Container(
      decoration: BoxDecoration(
        color: Colors.deepPurple.shade300.withOpacity(0.4),
        borderRadius: BorderRadius.circular(15),
      ),
      child: ListTile(
        title: Padding(
          padding: const EdgeInsets.only(bottom: 5.0),
          child: Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Text(
                widget.date,
                style: TextStyle(fontWeight: FontWeight.bold, fontSize: 15.0, color: Colors.deepPurple.shade100),
              ),

              // Edit Button
              IconButton(
                icon: Icon(
                  readOnly ? Icons.edit : Icons.save,
                  color: Colors.deepPurple.shade100,
                ),
                onPressed: () {
                  setState(() {
                    readOnly = !readOnly;
                  });
                },
              ),
            ],
          ),
        ),
        subtitle: Padding(
          padding: const EdgeInsets.only(bottom: 15.0),
          child: TextField(
            controller: controller,
            readOnly: readOnly,
            decoration: InputDecoration(
              border: readOnly ? InputBorder.none : OutlineInputBorder(borderRadius: BorderRadius.circular(15.0)),
              hintText: &quot;Enter notes here&quot;,
              hintStyle: TextStyle(color: Colors.deepPurple.shade100),
            ),
            style: const TextStyle(fontWeight: FontWeight.bold, fontSize: 18.0, color: Colors.white),
          ),
        ),
      ),
    );
  }
}

huangapple
  • 本文由 发表于 2023年5月22日 00:49:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/76300970.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定