简体   繁体   English

来自另一个列出的小部件的颤动调用小部件

[英]flutter call widget from another listed widgets

I am trying to fix this problem that has widget(main-widget) that has a child(colum-widget) and build list of buttons widgets.我正在尝试解决这个问题,它有一个小部件(主小部件),它有一个子部件(列小部件)并构建按钮小部件列表。 since i am working with many stateful widget, how do i call the main page from the other widget (globallike access to call from the other widget).由于我正在使用许多有状态小部件,我如何从其他小部件调用主页(从其他小部件调用的全局访问权限)。

you can run this code I make and put some TODO: comments for the reference :)您可以运行我制作的这段代码并添加一些 TODO: 注释以供参考:)

import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: Main(param: 1),
    );
  }
}


class Main extends StatefulWidget {

  final int param;

  const Main({Key key,this.param}) : super(key: key);

  @override
  _MainState createState() => _MainState();

}

class _MainState extends State<Main> {

  bool change1;
  bool change2;
  bool change3;

  @override
  initState() {
    super.initState();
    if (widget.param == 1) {
      setChange1();
    }
    else if (widget.param == 2) {
      setChange2();
    }
    else if (widget.param == 3) {
      setChange3();
    }
  }

  void setChange1() {
    setState(() {
      change1 = true;
      change2 = change3 = false;
    });
  }

  void setChange2() {
    setState(() {
      change1 = change3 = false;
      change2 = true;
    });
  }

  void setChange3() {
    setState(() {
      change1 = change2 = false;
      change3 = true;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SafeArea(
        child: Column(
          children: <Widget>[
            Container(
              color: Colors.grey,
              constraints: BoxConstraints.expand(
                height: 60.0,
              ),
              child: Stack(
                children: <Widget>[
                  Container(
                    alignment: Alignment.bottomCenter,
                    child: Row(
                      children: <Widget>[
                        Expanded(
                          child: GestureDetector(
                            onTap: () {
                              setState(() {
                                setChange1();
                                build(context);
                              });
                            },
                            child: Container(
                              height: 50.0,
                              child: Padding(
                                padding: const EdgeInsets.all(10.0),
                                child: Center(
                                  child: Text(
                                    "Button1",
                                    style: TextStyle(
                                        fontSize: 20.0, color: Colors.white),
                                  ),
                                ),
                              ),
                            ),
                          ),
                        ),
                        Expanded(
                          child: GestureDetector(
                            onTap: () {
                              setState(() {
                                setChange2();
                                build(context);
                              });
                            },
                            child: Container(
                              height: 50.0,
                              child: Padding(
                                padding: const EdgeInsets.all(10.0),
                                child: Center(
                                  child: Text(
                                    "Button2",
                                    style: TextStyle(
                                        fontSize: 20.0, color: Colors.white),
                                  ),
                                ),
                              ),
                            ),
                          ),
                        ),
                      ],
                    ),
                  ),
                ],
              ),
            ),
            //TODO:
            new Visibility(
              //Called changed and viewOne
              visible: change1,
              child: view1(),
            ),
            new Visibility(
              //Called not changed and viewTwo
              visible: change2,
              child: view2(),
            ),
            new Visibility(
              //Called not changed and viewTwo
              visible: change3,
              child: view3(),
            ),
          ],
        ),
      ),
    );
  }
}


class view1 extends StatefulWidget {
  @override
  _view1State createState() => _view1State();
}

class _view1State extends State<view1> {
  @override
  Widget build(BuildContext context) {
    return Container(
      height: 600.0,
      color: Colors.blue,
    );
  }
}

class view2 extends StatefulWidget {
  @override
  _view2State createState() => _view2State();
}

class _view2State extends State<view2> {
  @override
  Widget build(BuildContext context) {
    return Container(
      height: 600.0,
      color: Colors.orange,
      child: Column(
        children: <Widget>[
          ListWithButton(),
        ],
      ),
    );
  }
}

class view3 extends StatefulWidget {
  @override
  _view3State createState() => _view3State();
}

class _view3State extends State<view3> {
  @override
  Widget build(BuildContext context) {
    return Container(
      height: 600.0,
      color: Colors.green,
    );
  }
}

class ListWithButton extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Row(
        children: <Widget>[
          Expanded(
              child: Padding(
                padding: const EdgeInsets.symmetric(vertical: 3.0,horizontal: 5.0),
                child: GestureDetector(
                  onTap: (){
                    //TODO:  this will trigger the view_1 upon Main page
                    Main(param: 1).createState();
                    build(context);
                    //TODO: that was id like to try.
                  },
                  child: Container(
                    height: 50.0,
                    color: Colors.red,
                    child:Center(
                      child: Text(
                        "list_Button1",
                        style: TextStyle(
                          fontSize: 20.0,
                          fontWeight: FontWeight.bold,
                          color: Colors.white,
                        ),
                      ),
                    ),
                  ),
                ),
              ),
            ),

         Expanded(
              child: Padding(
                padding: const EdgeInsets.symmetric(vertical: 3.0,horizontal: 5.0),
                child: GestureDetector(
                  onTap: (){
                    //TODO:  this will trigger the view_3 upon Main page
                    Main(param: 3).createState();
                    build(context);
                    //TODO: that was id like to try.
                  },
                  child: Container(
                    height: 50.0,
                    color: Colors.red,
                    child:Center(
                      child: Text(
                        "list_Button2",
                        style: TextStyle(
                          fontSize: 20.0,
                          fontWeight: FontWeight.bold,
                          color: Colors.white,
                        ),
                      ),
                    ),
                  ),
                ),
              ),
          ),
        ],
    );
  }
}

在此处输入图片说明 ok, this is my answer.好的,这是我的答案。 Hope to help you.希望能帮到你。

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

相关问题 Flutter - 从另一个小部件调用 onTap - Flutter - Call onTap from another widget Position 小部件围绕 Flutter 中的另一个小部件运行 - Position widgets in orbit around another widget in Flutter 从另一个有状态小部件调用一个有状态小部件中的方法 - Flutter - call method in one stateful widget from another stateful widget - Flutter 从小部件列表中删除小部件 [Flutter] - Deleting a widget from a list of widgets [Flutter] 如何将子有状态小部件的索引单选按钮值返回到 Flutter 中的另一个有状态小部件 - how to return index radio button value from children Stateful widgets to another stateful widget in Flutter Flutter - 触发另一个小部件方法,该小部件位于另一个文件中 - Flutter - Triggering another widgets method which widget is in another file Flutter:当小部件没有父/子关系时,如何从小部件 B 调用小部件 A 中的 function? - Flutter: How do I call a function in widget A from widget B when the widgets do not have a parent/child relationship? 如何从 Flutter 中的另一个文件调用小部件部分 - How to call widget section from another file in Flutter Flutter 混合/屏蔽堆栈中另一个小部件下的多个小部件 - Flutter blend/mask multiple widgets under another widget in a stack 如何从 Flutter 中的另一个小部件滚动小部件 - How to scroll a widget from another widget in Flutter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM