0

Necesito que aparezca un BottomSheet cuando presiono sobre un IconButton:

IconButton(
           icon: Icon(Icons.more_vert),
           iconSize: 25,
           splashRadius: 20,
           onPressed: () {
                MyHomePage();
           },
           tooltip: "Opciones")

En un tutorial que estaba siguiendo debería aparecer el bottomshet mediante la llamada a MyHomePage, pero no me aparece nada, este es el código que tengo:

class MyHomePage extends StatefulWidget {
    MyHomePage({Key key, this.title}) : super(key: key);
    final String title;

    @override
    _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
    String _selectedItem = "";
    
    @override
    Widget build(BuildContext context) {
    return Scaffold(
        body: Center(
            child: Column(
                children: [
                    RaisedButton(onPressed: () => _onButtonPressed()),
                    Text(_selectedItem, style: TextStyle(fontSize: 30))
                ],
            ),
        ));
    }

    void _onButtonPressed() {
        showModalBottomSheet(
            context: context,
            builder: (context) {
              return Column(
                children: [ListTile(title: Text("BottomSheet"))],
              );
            });
        }
    }
}
7

1 respuesta 1

1

Este es el ejemplo de tu código completo donde modifico tu código para que el BottomSheet se muestre al dar click en el IconButton.

La función se ejecuta dentro de la clase Main, y no dentro de el HomePage

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter/services.dart';

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

class Main extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    SystemChrome.setSystemUIOverlayStyle(
      SystemUiOverlayStyle(statusBarColor: Colors.transparent),
    );
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      title: "Social",
      theme: ThemeData(primaryColor: Colors.white, primarySwatch: Colors.teal),
      home: Scaffold(
        drawer: Builder(
          builder: (context) => Drawer(
            child: ListView(
                physics: BouncingScrollPhysics(),
                padding: EdgeInsets.zero,
                children: [
                  UserAccountsDrawerHeader(
                      accountName: Text("Nombre"),
                      accountEmail: Text("Correo Electronico"),
                      currentAccountPicture: CircleAvatar(
                        child: Text("N"),
                      )),
                  ListTile(
                    title: Text('Inicio'),
                    leading: Icon(Icons.home_outlined),
                    onTap: () {},
                  )
                ]),
          ),
        ),
        appBar: AppBar(title: Text("Social"), actions: <Widget>[
          IconButton(
              icon: Icon(Icons.search),
              onPressed: () {},
              splashRadius: 20,
              tooltip: "Buscar",
              iconSize: 25)
        ]),
        body: SingleChildScrollView(
          physics: BouncingScrollPhysics(),
          child: Column(
            children: [
              Container(
                margin: EdgeInsets.only(top: 5),
                child: ListView.builder(
                  shrinkWrap: true,
                  physics: NeverScrollableScrollPhysics(),
                  itemCount: 1,
                  itemBuilder: (BuildContext context, int i) => Card(
                    child: Container(
                      child: Column(
                        children: [
                          Padding(
                            padding: EdgeInsets.only(
                              left: 10,
                              top: 10,
                              right: 5,
                              bottom: 10,
                            ),
                            child: Row(
                              mainAxisAlignment: MainAxisAlignment.start,
                              children: [
                                GestureDetector(
                                  onTap: () {
                                    Scaffold.of(context).showSnackBar(
                                      SnackBar(
                                        content: Text("click"),
                                      ),
                                    );
                                  },
                                  child: CircleAvatar(
                                    child: Text("N"),
                                    radius: 20,
                                  ),
                                ),
                                Expanded(
                                  child: Column(
                                    crossAxisAlignment:
                                        CrossAxisAlignment.start,
                                    children: [
                                      Padding(
                                        padding: EdgeInsets.only(
                                          left: 10,
                                          bottom: 5,
                                        ),
                                        child: Text(
                                          "N",
                                          style: TextStyle(
                                              fontWeight: FontWeight.bold),
                                          overflow: TextOverflow.ellipsis,
                                          maxLines: 3,
                                        ),
                                      ),
                                      Padding(
                                        padding: EdgeInsets.only(left: 10),
                                        child: Row(
                                          children: [
                                            Icon(
                                              Icons.timer_outlined,
                                              size: 12,
                                            ),
                                            Padding(
                                              padding: EdgeInsets.only(
                                                left: 5,
                                              ),
                                              child: Text(
                                                "Fecha y Hora",
                                                style: TextStyle(
                                                  fontSize: 12,
                                                ),
                                              ),
                                            ),
                                          ],
                                        ),
                                      ),
                                    ],
                                  ),
                                ),
                                IconButton(
                                  icon: Icon(Icons.more_vert),
                                  iconSize: 25,
                                  splashRadius: 20,
                                  onPressed: () {
                                    _onButtonPressed(context);
                                  },
                                  tooltip: "Opciones",
                                ),
                              ],
                            ),
                          ),
                          Container(
                            height: 500,
                          )
                        ],
                      ),
                    ),
                    elevation: 0,
                    color: Colors.white,
                    margin: EdgeInsets.only(
                      top: 10,
                      bottom: 10,
                    ),
                  ),
                ),
              ),
            ],
          ),
        ),
        floatingActionButton: FloatingActionButton.extended(
          onPressed: () {},
          label: Text(""),
          icon: Icon(Icons.add),
        ),
        backgroundColor: Color(0xFFE0E0E0),
      ),
    );
  }

  void _onButtonPressed(BuildContext context) {
    showModalBottomSheet(
      context: context,
      builder: (context) {
        return MyHomePage();
      },
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);
  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  String _selectedItem = "";
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Home'),
      ),
      body: Center(
        child: Column(
          children: [Text(_selectedItem, style: TextStyle(fontSize: 30))],
        ),
      ),
    );
  }
}


Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.