简体   繁体   English

如何在 flutter 中创建 SimpleDialog 小部件的动态内容的最佳实践?

[英]how the best practice for to create dynamic content of SimpleDialog widget in flutter?

i have simpleDialog widget that is can dynamically change the its child or its content when i click the button inside its child, the example like below..我有 simpleDialog 小部件,当我单击其子项内的按钮时,它可以动态更改其子项或其内容,如下例所示。

class PopupDailyMood extends StatefulWidget {
  const PopupDailyMood({Key? key}) : super(key: key);

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

class _PopupDailyMoodState extends State<PopupDailyMood> {
  String mode = "selectMood";
  String mood = "";

  @override
  Widget build(BuildContext context) {
    double widthDialog = MediaQuery.of(context).size.width * 0.90;

    return SimpleDialog(
      insetPadding: EdgeInsets.symmetric(horizontal: 20),
      shape: RoundedRectangleBorder(borderRadius: BorderRadius.all(Radius.circular(8))),
      contentPadding: EdgeInsets.all(0),
      backgroundColor: Color(0XFF020202),
      elevation: 0,
      children: <Widget>[
        Container(
            width: widthDialog,
            padding: EdgeInsets.fromLTRB(14, 31, 14, 34),
            child: mode == "selectMood"
                ? moodSelect(context)
                : mode == "blockerConfirm"
                    ? blockerConfirm()
                    : mode == "done2"
                        ? done2()
                        : null)
      ],
    );
  }

the question.. is there another way to create dynamic simple dialog, that is can shifting or changing the simple dialog when some action have trigered?问题..是否有另一种方法来创建动态简单对话框,即可以在触发某些操作时移动或更改简单对话框?

Yes, there is, I don't recomend you use strings when talking about specific modes, you could use ints, however that makes it impossible to tell what each value should be: so I present to you: ENUMS是的,我不建议您在谈论特定模式时使用字符串,您可以使用整数,但是这使得无法判断每个值应该是什么:所以我向您展示:ENUMS

enum MoodState {
  MoodSelect,
  BlockerConfirm,
  Done2,
}

If you declare an enum like above in some file, you can now do something like this:如果你在某个文件中像上面那样声明一个枚举,你现在可以这样做:

mode == MoodState.MoodSelect
                ? moodSelect(context)
                : mode == MoodState.BlockerConfirm
                    ? blockerConfirm()
                    : mode == MoodState.Done2
                        ? done2()
                        : null)

This helps with typos and allows Intellisense to pick up on what you want to do.这有助于拼写错误,并允许 Intellisense 了解您想要执行的操作。 But it's still a bit clunky, so why not move it to a function?但它仍然有点笨重,那么为什么不把它移到 function 呢?

Widget _buildDialog(MoodState state) {
  switch (state) {
    case MoodState.MoodSelect: return moodSelect(context);
    case MoodState.BlockerConfirm: return blockerConfirm();
    case MoodState.Done2: return done2();
  }
}

you could also put it into a map of type Map<MoodState, Widget> and get it like so: child: myMap[moodState]您也可以将其放入类型为 map 的Map<MoodState, Widget>并像这样获取它: child: myMap[moodState]

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

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