简体   繁体   English

在按下按钮时显示小部件 Flutter

[英]Display Widget onPressed button Flutter

My goal is to display certain widgets based on clicks in a menu.我的目标是根据菜单中的点击显示某些小部件。 What I need when I click a title, is to shows a certain widget.单击标题时我需要的是显示某个小部件。

Here is the full code:这是完整的代码:

 class SideMenu extends StatelessWidget { const SideMenu({ Key? key, }): super(key: key); @override Widget build(BuildContext context) { return Drawer( child: SingleChildScrollView( // it enables scrolling child: Column( children: [ DrawerHeader( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ SizedBox( height: defaultPadding * 3, ), Image.asset( "assets/logo/logo_icon.png", scale: 5, ), SizedBox( height: defaultPadding, ), Text("Récolt"), ], )), DrawerListTile( title: "Tableau de bord", svgSrc: "assets/icons/menu_dashbord.svg", press: () {}, ), DrawerListTile( title: "Utilisateurs", svgSrc: "assets/icons/menu_profile.svg", press: () {}, ), DrawerListTile( title: "Collaborateurs", svgSrc: "assets/icons/menu_doc.svg", press: () {}, ), DrawerListTile( title: "Producteurs", svgSrc: "assets/icons/menu_store.svg", press: () {}, ), DrawerListTile( title: "Paramètres", svgSrc: "assets/icons/menu_setting.svg", press: () {}, ), ], ), ), ); } }

I've got a extern page for display my widget like this:我有一个外部页面来显示我的小部件,如下所示:

 SizedBox(height: defaultPadding), ListUserEntreprise(), SizedBox(height: defaultPadding), ListUserCollab(),

I don't know how to do it, I tried with using a boolean in onPressed but it didn't work, I don't know really how to declare it and use it with an external page.我不知道该怎么做,我尝试在 onPressed 中使用 boolean 但它没有用,我真的不知道如何声明它并将它用于外部页面。 Can someone help me, please?有谁可以帮助我吗?

use StatefulWidget and try with a boolean with the onPressed, setState() to update UI使用 StatefulWidget 并尝试使用带有 onPressed、 setState()的 boolean 来更新 UI

Example:例子:

import 'package:flutter/material.dart';

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

  @override
  State<SideMenu> createState() => _SideMenuState();
}

class _SideMenuState extends State<SideMenu> {
  bool _isShow = false;

  @override
  Widget build(BuildContext context) {
    return Drawer(
      child: SingleChildScrollView(
        // it enables scrolling
        child: Column(
          children: [
            DrawerListTile(
              title: "Tableau de bord",
              svgSrc: "assets/icons/menu_dashbord.svg",
              press: () {
                setState(() {
                  _isShow = !_isShow;
                });
              },
            ),
            Visibility(
                visible: _isShow,
                child: Container(
                  child: Text('Tableau de bord'),
                ))
          ],
        ),
      ),
    );
  }
}

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

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