简体   繁体   English

如何制作dart flutter弹框?

[英]How to make dart flutter popup box?

I have an application like this:我有一个这样的应用程序:

在此处输入图像描述

My aim is that when I press the eye icon next to the text "Hello", I want a box to open just below the text and write the German version of "Hello".我的目标是当我按下文本“你好”旁边的眼睛图标时,我希望在文本下方打开一个框并写下德语版的“你好”。 So it will say "Hallo".所以它会说“你好”。

My purpose is to show the meaning of the word.我的目的是展示这个词的意思。

When I press the eye, I want to show the German of the word.当我按下眼睛时,我想展示这个词的德语。 How can I make a white box under the word Hello, that is, the box in which the German language will be written?如何在 Hello 一词下方制作一个白框,即要在其中写入德语的框?

Codes:代码:

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

class selamlasmaLearn extends StatelessWidget {

  List <wordAndMeaning> wordsList = [wordAndMeaning("Hello", "Hallo"), wordAndMeaning("Go", "Gehen")];
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Builder(
        builder: (context) {
          final double height = MediaQuery.of(context).size.height;

          return CarouselSlider(
            options: CarouselOptions(
              height: height,
              viewportFraction: 1.0,
              enlargeCenterPage: false, 
            ),
            items: wordsList.map((wordAndMeaning word) {
              return Builder(
                builder: (BuildContext context) {
                  return Container(
                    width: MediaQuery.of(context).size.width,
                    decoration: BoxDecoration(color: Colors.amber),
                    child: Center(
                      child: Row(
                        crossAxisAlignment: CrossAxisAlignment.center,
                        mainAxisAlignment: MainAxisAlignment.center,
                        children: [
                          Text(
                            word.word, 
                            style: TextStyle(fontSize: 45, color: Colors.white),
                          ),
                          SizedBox(width: 10,),
                          Icon(Icons.remove_red_eye_sharp, color: Colors.white, size: 25,), // <<<<<<<<<
                        ],
                      ),
                    ),
                  );
                },
              );
            }).toList(),
            );
        }
            ),
          );
        }
  }

class wordAndMeaning {
  String word;
  String meaning;

  wordAndMeaning(this.word, this.meaning);
}

I keep the word and its German in a list called wordsList .我将单词及其德语保存在名为wordsList的列表中。

Thanks for the help in advance.我在这里先向您的帮助表示感谢。

You can convert the widget to StatefulWidget or use a ValueNotifier to control the preserve/notify the state visibility.您可以将小部件转换为StatefulWidget或使用ValueNotifier来控制保留/通知 state 可见性。

You can use Visibility widget or just if to show and hide German text.您可以使用Visibility widget 或 just if来显示和隐藏德语文本。

class selamlasmaLearn extends StatefulWidget {
  @override
  State<selamlasmaLearn> createState() => _selamlasmaLearnState();
}

class _selamlasmaLearnState extends State<selamlasmaLearn> {
  bool _showGerman = false;

  List<wordAndMeaning> wordsList = [
    wordAndMeaning("Hello", "Hallo"),
    wordAndMeaning("Go", "Gehen")
  ];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Builder(builder: (context) {
        final double height = MediaQuery.of(context).size.height;

        return CarouselSlider(
          options: CarouselOptions(
            height: height,
            viewportFraction: 1.0,
            enlargeCenterPage: false,
          ),
          items: wordsList.map((wordAndMeaning word) {
            return Builder(
              builder: (BuildContext context) {
                return Container(
                  width: MediaQuery.of(context).size.width,
                  decoration: BoxDecoration(color: Colors.amber),
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.center,
                    mainAxisSize: MainAxisSize.min,
                    children: [
                      Column(
                        mainAxisSize: MainAxisSize.min,
                        children: [
                          Text(word.word,
                              style:
                                  TextStyle(fontSize: 45, color: Colors.white)),
                          if (_showGerman) Text(word.meaning), //modify the way you want
                        ],
                      ),
                      const SizedBox(
                        width: 10,
                      ),
                      IconButton(
                        icon: Icon(Icons.remove_red_eye_sharp),
                        color: Colors.white,
                        iconSize: 25,
                        onPressed: () {
                          setState(() {
                            _showGerman = !_showGerman;
                          });
                        },
                      ),
                    ],
                  ),
                );
              },
            );
          }).toList(),
        );
      }),
    );
  }
}

Use the Tooltip widget使用工具提示小部件

I'm emphasizing on the popup part in your question title.我强调你问题标题中的弹出部分。 When using a Tooltip you ensure that your widgets do not shift position or jump when the Tooltip widget appear, as the example below illustrates.使用工具提示时,请确保您的小部件不会在工具提示小部件出现时移动 position 或跳转,如下例所示。

Example code:示例代码:

import 'package:flutter/material.dart';

class TooltipExample extends StatelessWidget {
  const TooltipExample({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Center(
      child: Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          Tooltip(
            // Set the tooltip to trigger on a single tap, tapping outside the
            // widget will make the tooltip disappear.
            triggerMode: TooltipTriggerMode.tap,

            // The message shown when the tooltip appears.
            message: "Tooltip showing!",

            // Consider adjusting this to your needs.
            showDuration: const Duration(days: 1),

            // The widget that must be clicked to show the tooltip.
            child: Padding(
              padding: const EdgeInsets.all(8.0),
              child: Row(
                mainAxisSize: MainAxisSize.min,
                children: const [
                  Text("Hello"),
                  SizedBox(
                    width: 8,
                  ),
                  Icon(Icons.visibility),
                ],
              ),
            ),
          ),
          const Padding(
            padding: EdgeInsets.all(8.0),
            child: Text("Cover me!"),
          )
        ],
      ),
    );
  }
}

// Some code to run the above example, note the theme part that turns the
// tooltip white.
class App extends StatelessWidget {
  const App({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      // Style the overall design of tooltips in the app in one place,
      // or provide in each tooltip individually.
      theme: ThemeData(
        tooltipTheme: const TooltipThemeData(
          decoration: BoxDecoration(
            color: Colors.white,
            borderRadius: BorderRadius.all(
              Radius.circular(4),
            ),
          ),
          textStyle: TextStyle(
            backgroundColor: Colors.white,
            color: Colors.black,
          ),
        ),
      ),

      home: const Scaffold(
        backgroundColor: Colors.amber,
        body: TooltipExample(),
      ),
    );
  }
}

void main() => runApp(const App());

Here is how it looks:这是它的样子:

Note that the Tooltip widget overlays whatever is below it.请注意,工具提示小部件覆盖了它下面的任何内容。 (instead of pushing it further down - like toggling the visibility of a normal widget in a row or column would have done) (而不是将它进一步向下推 - 就像在行或列中切换普通小部件的可见性一样)

没有工具提示显示工具提示显示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM