Dhemas Eka
Dhemas Eka

Reputation: 105

Creating flutter expandable with toggle button

I got some difficulties implementing UI design in flutter. I wanna make an expanded widget like this, can I have a clue?

collapsed:

collapsed

expanded

expanded

I have tried to use Expandable but I just can't get it looks like my design. Thanks

Upvotes: 4

Views: 2875

Answers (1)

Augustin R
Augustin R

Reputation: 7799

You can use AnimatedContainer.

For example :

import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      debugShowCheckedModeBanner: false,
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  bool expanded = false;
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('Example')),
      body: Column(
        children: [
          Container(color: Colors.green, height: 100),
          AnimatedContainer(
            duration: const Duration(milliseconds: 200),
            height: expanded ? 120 : 0,
            child: Container(height: 120, color: Colors.red),
          ),
          Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              FloatingActionButton(
                child:
                    Icon(expanded ? Icons.arrow_upward : Icons.arrow_downward),
                onPressed: () => setState(() {
                  expanded = !expanded;
                }),
              ),
            ],
          ),
        ],
      ),
    );
  }
}


Upvotes: 8

Related Questions