Usare showModalBottomSheet in Flutter

Mattepuffo's logo
Usare showModalBottomSheet in Flutter

Usare showModalBottomSheet in Flutter

showModalBottomSheet è un widget per Flutter con il quale possiamo creare un menu alternativo che compare da sotto alla schermata.

Potete usarlo anche come un menu aggiuntivo volendo.

In questo articolo ne vediamo un esempio.

Non ci sono librerie da aggiungere:

import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Test',
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(
          seedColor: Colors.deepPurple,
        ),
        useMaterial3: true,
      ),
      home: const MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key});

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Theme.of(context).colorScheme.inversePrimary,
        title: const Text('Flutter Test'),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            ElevatedButton(
              onPressed: () {
                showModalBottomSheet(
                  context: context,
                  builder: (BuildContext context) {
                    return Container(
                      height: 100,
                      color: Colors.white,
                      child: Column(
                        children: [
                          ListTile(
                            leading: const Icon(Icons.login),
                            title: const Text('Login'),
                            onTap: () {
                              print('LOGIN');
                            },
                          ),
                          ListTile(
                            leading: const Icon(Icons.logout),
                            title: const Text('Logout'),
                            onTap: () {
                              print('LOGOUT');
                            },
                          ),
                        ],
                      ),
                    );
                  },
                );
              },
              child: const Text('PREMI'),
            ),
          ],
        ),
      ),
    );
  }
}

Enjoy!


Condividi

Commentami!