Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 434
Dhemas Eka Avatar asked Mar 03 '23 23:03

Dhemas Eka


1 Answers

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;
                }),
              ),
            ],
          ),
        ],
      ),
    );
  }
}


like image 124
Augustin R Avatar answered Mar 05 '23 11:03

Augustin R