【问题标题】:flutter call widget from another listed widgets来自另一个列出的小部件的颤振调用小部件
【发布时间】:2020-06-05 10:44:09
【问题描述】:

我正在尝试解决这个问题,它有一个小部件(主小部件),它有一个子小部件(colum-widget)并构建按钮小部件列表。因为我正在使用许多有状态的小部件,我如何从另一个小部件调用主页(全局访问从另一个小部件调用)。

你可以运行我制作的这段代码并放一些 TODO: cmets 供参考:)

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,
                        ),
                      ),
                    ),
                  ),
                ),
              ),
          ),
        ],
    );
  }
}

【问题讨论】:

    标签: flutter flutter-widget


    【解决方案1】:

    好的,这是我的答案。希望能帮到你。

    【讨论】:

    • 对不起我的错误信息,当我回到你的代码时,逻辑是正确的但需要加强,也许我会做出正确的答案以便可以理解..顺便说一句,谢谢寻求帮助。 :)
    猜你喜欢
    • 2021-06-11
    • 2019-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-19
    • 1970-01-01
    • 2021-08-19
    • 2020-11-15
    相关资源
    最近更新 更多