виджет вызова флаттера из других перечисленных виджетов - PullRequest
0 голосов
/ 21 февраля 2020

Я пытаюсь решить эту проблему, у которой есть виджет (основной виджет), который имеет дочерний элемент (виджет-столбец) и список элементов виджетов кнопок. так как я работаю со многими виджетами с состоянием, как я могу вызвать главную страницу из другого виджета (глобально доступ к вызову из другого виджета).

вы можете запустить этот код, который я делаю, и добавить некоторые 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,
                        ),
                      ),
                    ),
                  ),
                ),
              ),
          ),
        ],
    );
  }
}

1 Ответ

1 голос
/ 21 февраля 2020

enter image description here хорошо, это мой ответ. Надеюсь помочь вам.

...