在使用对话框的“编辑单击"上编辑Listview项 [英] Edit Listview Item on Edit Click Using Dialog

查看:133
本文介绍了在使用对话框的“编辑单击"上编辑Listview项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要编辑项目单击编辑"按钮.当用户按下编辑按钮时,我希望出现一个带有TextField的对话框,以便用户可以在其中输入文本并将其替换为输入的文本.我尝试过,但是找不到任何解决方案.这个你能帮我吗.我附上了我的布局的屏幕截图,并发布了一些代码.

I want to edit an item On Edit Button Click. When a user presses on the edit button, I want a dialog with a TextField in it to appear so that the user can enter text in it and replace it with the text entered. I tried but I can't find any solutions. Please help me out with this. I attached a screenshot of my layout as well as posted some code.

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

void main() {
  runApp(MyApp(
    items: List<String>.generate(100, (i) => "List item $i"),
  ));
}

Future<String> _asyncInputDialog(BuildContext context) async {
  String sampleText = '';
  return showDialog<String>(
    context: context,
    barrierDismissible:
    false, // dialog is dismissible with a tap on the barrier
    builder: (BuildContext context) {
      return AlertDialog(
        title: Text('Enter Text'),
        content: new Row(
          children: <Widget>[
            new Expanded(
                child: new TextField(
                  autofocus: true,
                  decoration: new InputDecoration(
                      labelText: 'Text Here', hintText: 'eg. ABCD'),
                  onChanged: (value) {
                    sampleText = value;
                  },
                ))
          ],
        ),
        actions: <Widget>[
          FlatButton(
            child: Text('Ok'),
            onPressed: () {
              Navigator.of(context).pop(sampleText);
            },
          ),
        ],
      );
    },
  );
}

class MyApp extends StatefulWidget {
  final List<String> items;

  MyApp({Key key, @required this.items}) : super(key: key);

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

class _MyAppState extends State<MyApp> {
  @override
  Widget build(BuildContext context) {
    final title = 'Long List';

    return MaterialApp(
      title: title,
      home: Scaffold(
        appBar: AppBar(
          title: Text(title),
        ),
        body: ListView.builder(
          itemCount: widget.items.length,
          itemBuilder: (context, index) {
            return ListTile(
              title: Text('${widget.items[index]}'),
              trailing: RaisedButton(
                child: Text('Edit'),
                color: Colors.pinkAccent,
                onPressed: () async {
                  final String newText = await _asyncInputDialog(context);
                  setState(() {

                  });
                  Scaffold.of(context).showSnackBar(new SnackBar(content: new Text("$newText"),));
                },
              ),
            );
          },
        ),
      ),
    );
  }
}

推荐答案

在您的ListTile中,将items[index] = newText;放入setState((){});中,这样我会看起来像这样.

In your ListTile put items[index] = newText; in setState((){}); so I would look like this.

ListTile(
              title: Text('${items[index]}'),
              trailing: RaisedButton(
                child: Text('Edit'),
                color: Colors.pinkAccent,
                onPressed: () async {
                  final String newText = await _asyncInputDialog(context);
                  setState(() {
                    items[index] = newText;
                  });
                  Scaffold.of(context).showSnackBar(new SnackBar(
                    content: new Text("$newText"),
                  ));
                },
              ),
            );

这篇关于在使用对话框的“编辑单击"上编辑Listview项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆