显示一个按钮后,如何在按下按钮后显示一个文本字段?

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

How to display a TextField after pressing a button?

问题

我想在我的应用程序的帖子功能中添加一个举报用例。用户可以在看到评论有问题时进行举报。他点击下面显示的三个点:

然后他应该有一个文本字段,以便他可以输入举报评论。只有当用户点击举报按钮时,这个文本字段才可见,当用户完成写举报评论并点击确定按钮时,它应该消失。我找到的一种解决方案是将文本字段设计成一个浮动的元素,类似于 Snackbar,它出现,让用户输入评论,然后消失。但问题是我不知道如何实现它。

英文:

I want to add a report usecase to a post feature on my app. The user can report a comment if he sees something wrong with it. He presses on the three dots shown below:

显示一个按钮后,如何在按下按钮后显示一个文本字段?

And then he should have a TextField so that he can input his report comment. This TextField should only be visible if the user clicks on the report button and should dispear when the user finishes writing his report comment and presses on the Okay button. A solution that I find is to make that TextField a floating one, like a snackbar, it appears, gives to the user the hand to write a comment, and dispears. But the issue is I don't know how to implement it.

答案1

得分: 1

你可以使用这段代码并根据你的自定义来实现你的目标,

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Bottom Sheet Demo',
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  void _showBottomSheet() {
    showModalBottomSheet(
        context: context,
        builder: (context) {
          return Container(
            height: 200,
            child: Column(
              children: [
                Padding(
                  padding: EdgeInsets.all(8),
                  child: TextField(),
                ),
                ElevatedButton(
                  onPressed: () {
                    Navigator.pop(context);
                  },
                  child: Text('Okay'),
                )
              ],
            ),
          );
        });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: ElevatedButton(
          onPressed: _showBottomSheet,
          child: Text('Show Bottom Sheet'),
        ),
      ),
    );
  }
}
英文:

You can achieve your goal using this code with your customization,

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

void main() =&gt; runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: &#39;Bottom Sheet Demo&#39;,
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() =&gt; _MyHomePageState();
}

class _MyHomePageState extends State&lt;MyHomePage&gt; {
  void _showBottomSheet() {
    showModalBottomSheet(
        context: context,
        builder: (context) {
          return Container(
            height: 200,
            child: Column(
              children: [
                Padding(
                  padding: EdgeInsets.all(8),
                  child: TextField(),
                ),
                ElevatedButton(
                  onPressed: () {
                    Navigator.pop(context);
                  },
                  child: Text(&#39;Okay&#39;),
                )
              ],
            ),
          );
        });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: ElevatedButton(
          onPressed: _showBottomSheet,
          child: Text(&#39;Show Bottom Sheet&#39;),
        ),
      ),
    );
  }
}

huangapple
  • 本文由 发表于 2023年8月10日 23:58:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/76877459.html
匿名

发表评论

匿名网友

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

确定