【问题标题】:flutter, Switch widget does not work properly in the showDialog颤动,切换小部件在 showDialog 中无法正常工作
【发布时间】:2020-09-13 23:05:54
【问题描述】:

我找到this site 并运行代码。 该示例代码运行良好。 这段代码在这里。

import 'package:flutter/material.dart';

void main() {
  runApp(MaterialApp(
    home: MyApp(),
  ));
}

class MyApp extends StatefulWidget {
  @override
  _State createState() => _State();
}

class _State extends State<MyApp> {
  bool isSwitched = false;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text('Flutter - tutorialkart.com'),
        ),
        body: Center(
          child: Switch(
            value: isSwitched,
            onChanged: (value) {
              setState(() {
                isSwitched = value;
                print(isSwitched);
              });
            },
            activeTrackColor: Colors.lightGreenAccent,
            activeColor: Colors.green,
          ),
        )
    );
  }
}

固定 Switch Widget 放置在 ShowDialog 内。

import 'package:flutter/material.dart';

void main() {
  runApp(MaterialApp(
    home: MyApp(),
  ));
}

class MyApp extends StatefulWidget {
  @override
  _State createState() => _State();
}

class _State extends State<MyApp> {
  bool isSwitched = false;

  @override
  Widget build(BuildContext context) {

    return Scaffold(
        body: Center(
          child: RaisedButton(onPressed: () {
            var ret = showDialog(
              context: context,
              builder: (BuildContext context) {
                return AlertDialog(

                  content: Container(

                    height: MediaQuery.of(context).size.height / 3,
                    child: Center(
                      child: Switch(
                        value: isSwitched,
                        onChanged: (value) {
                          setState(() {
                            isSwitched = value;
                            print(isSwitched);
                          });
                        },
                        activeTrackColor: Colors.lightGreenAccent,
                        activeColor: Colors.green,
                      ),
                    ),
                  ),
                );
              },
            );
          },child: Text("show toggle button"),),
        ),
    );
  }
}

isSwitched 变量没有改变。

如果单独运行 Switch Widget,它可以正常工作。

import 'package:flutter/material.dart';

void main() {
  runApp(MaterialApp(
    home: MyApp(),
  ));
}
class MyApp extends StatefulWidget {
  @override
  _State createState() => _State();
}

class _State extends State<MyApp> {

  @override
  Widget build(BuildContext context) {

    return Scaffold(
        body: Center(
          child: RaisedButton(onPressed: () {
            showDialog(
              context: context,
              builder: (BuildContext context) {
                return AlertDialog(

                  content: Container(

                    height: MediaQuery.of(context).size.height / 3,
                    child: Center(
                      child: SwitchWidget(),
                    ),
                  ),
                );
              },
            );
          },child: Text("show toggle button"),),
        ),
    );
  }
}

class SwitchWidget extends StatefulWidget {
  @override
  _SwitchWidgetState createState() => _SwitchWidgetState();
}

class _SwitchWidgetState extends State<SwitchWidget> {
  bool isSwitched = false;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        backgroundColor: Colors.transparent,
        body: Center(
          child: Switch(
            value: isSwitched,
            onChanged: (value) {
              setState(() {
                isSwitched = value;
                print(isSwitched);
              });
            },
            activeTrackColor: Colors.lightGreenAccent,
            activeColor: Colors.green,
          ),
        )
    );
  }
}

有没有办法将 Switch Widget 放入 ShowDialog 而不将其分离到另一个有状态的小部件中?

【问题讨论】:

    标签: flutter showdialog


    【解决方案1】:

    有很多方法可以实现,但正确的方法是为所有 AlertDialog 使用单独的 StateFul Widget,您可以查看与此主题相关的其他答案 here

    另一方面,我可以建议你在这种只想操作简单数据的场景中使用ValueNotifier,它使用起来非常简单,试试下一个:

    class _State extends State<MyApp> {
      ValueNotifier<bool> _isSwitched = ValueNotifier(false);
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          body: Center(
            child: RaisedButton(
              onPressed: () {
                var ret = showDialog(
                  context: context,
                  builder: (BuildContext context) {
                    return AlertDialog(
                      content: Container(
                        height: MediaQuery.of(context).size.height / 3,
                        child: Center(
                          child: ValueListenableBuilder<bool>(
                              valueListenable: _isSwitched,
                              builder: (context, currentState, child) {
                                return Switch(
                                  value: currentState,
                                  onChanged: (value) {
                                    _isSwitched.value = value;
                                  },
                                  activeTrackColor: Colors.lightGreenAccent,
                                  activeColor: Colors.green,
                                );
                              }),
                        ),
                      ),
                    );
                  },
                );
              },
              child: Text("show toggle button"),
            ),
          ),
        );
      }
    }
    

    希望对你有帮助。

    【讨论】:

      【解决方案2】:

      您可以在下面复制粘贴运行完整代码
      您可以在AlertDialogcontent 属性中使用StatefulBuilder

      代码sn-p

       return AlertDialog(content: StatefulBuilder(
                          builder: (BuildContext context, StateSetter setState) {
                        return Container(
      

      工作演示

      完整代码

      import 'package:flutter/material.dart';
      
      void main() {
        runApp(MaterialApp(
          home: MyApp(),
        ));
      }
      
      class MyApp extends StatefulWidget {
        @override
        _State createState() => _State();
      }
      
      class _State extends State<MyApp> {
        bool isSwitched = false;
      
        @override
        Widget build(BuildContext context) {
          return Scaffold(
            body: Center(
              child: RaisedButton(
                onPressed: () {
                  var ret = showDialog(
                    context: context,
                    builder: (BuildContext context) {
                      return AlertDialog(content: StatefulBuilder(
                          builder: (BuildContext context, StateSetter setState) {
                        return Container(
                          height: MediaQuery.of(context).size.height / 3,
                          child: Center(
                            child: Switch(
                              value: isSwitched,
                              onChanged: (value) {
                                setState(() {
                                  isSwitched = value;
                                  print(isSwitched);
                                });
                              },
                              activeTrackColor: Colors.lightGreenAccent,
                              activeColor: Colors.green,
                            ),
                          ),
                        );
                      }));
                    },
                  );
                },
                child: Text("show toggle button"),
              ),
            ),
          );
        }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多