Home > Net >  How to build a Sliding Up Panel when User taps on button
How to build a Sliding Up Panel when User taps on button

Time:05-23

I want to open a Panel (from the sliding_up_panel package) when I click on a TextButton. The Panel should be hidden and only visible after I clicked the button.

panel.dart

import 'package:flutter/material.dart';
import 'package:sliding_up_panel/sliding_up_panel.dart';

class Panel extends StatelessWidget {
  final ScrollController controller;
  final PanelController panelController;

  const Panel(
      {Key? key, required this.controller, required this.panelController})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    final size = MediaQuery.of(context).size;
    return ListView(
      physics: const BouncingScrollPhysics(),
      padding: const EdgeInsets.fromLTRB(20, 0, 20, 20),
      controller: controller,
      children: const <Widget>[
        SizedBox(height: 32),
        Center(child: Text("PANEL")),
      ],
    );
  }
}

MyHomePage.dart

class MyHomePage extends StatefulWidget {
  const MyHomePage({Key? key, required this.title}) : super(key: key);

  final String title;

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

class _MyHomePageState extends State<MyHomePage> {
  final panelController = PanelController();
  final controller = ScrollController();

  @override
  Widget build(BuildContext context) {
    final panelHeightOpen = MediaQuery.of(context).size.height * 0.7;
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: SlidingUpPanel(
        body: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              const Text(
                'Click Here to Build a New Panel',
              ),
              TextButton(
                onPressed: () {
                  //TODO: Build new Panel
                },
                child: const Text("Click"),
              ),
            ],
          ),
        ),
        controller: panelController,
        maxHeight: panelHeightOpen,
        defaultPanelState: PanelState.CLOSED,
        minHeight: 120,
        borderRadius: const BorderRadius.vertical(top: Radius.circular(20)),
        backdropTapClosesPanel: true,
        //panel: ,
        panelBuilder: (controller) {
          return Panel(
            controller: controller,
            panelController: panelController,
          );
        },
      ),
    );
  }
}

CodePudding user response:

To make SlidingUpPanel initially you need to provide

        defaultPanelState: PanelState.CLOSED,
        minHeight: 0,

To open use panelController.open();.

TextButton(
  onPressed: () {
    panelController.open();
  },
  child: const Text("Click"),
),
  • Related