Reputation: 495
I want to make a set of buttons with the ripple effect, elevation and shape like a FAB button. I read that I can only use one FAB button per screen, so I need to create my owns instead of using a set of FAB buttons.
How can I achieve the effect? It's possible to modify the FAB code to this?
Upvotes: 0
Views: 596
Reputation: 12693
So if you want the fabs in a vertical order wrap them in a Column like this
class RandomFabScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
floatingActionButton: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
FloatingActionButton(
child: Icon(Icons.home, color: Colors.white,),
onPressed: (){},
backgroundColor: Colors.cyan,
),
SizedBox(height: 10,),
FloatingActionButton(
child: Icon(Icons.playlist_play, color: Colors.white,),
onPressed: (){},
backgroundColor: Colors.red,
),
SizedBox(height: 10,),
FloatingActionButton(
child: Icon(Icons.person, color: Colors.white,),
backgroundColor: Colors.yellow,
onPressed: (){},
),
SizedBox(height: 10,),
FloatingActionButton(
child: Icon(Icons.warning, color: Colors.white,),
onPressed: (){},
backgroundColor: Colors.blue,
),
],
),
);
}
}
And if you want them in the horizontal order, wrap them in a Row
like this
class RandomFabScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
floatingActionButton: Row(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
FloatingActionButton(
child: Icon(Icons.home, color: Colors.white,),
onPressed: (){},
backgroundColor: Colors.cyan,
),
SizedBox(width: 10,),
FloatingActionButton(
child: Icon(Icons.playlist_play, color: Colors.white,),
onPressed: (){},
backgroundColor: Colors.red,
),
SizedBox(width: 10,),
FloatingActionButton(
child: Icon(Icons.person, color: Colors.white,),
backgroundColor: Colors.yellow,
onPressed: (){},
),
SizedBox(width: 10,),
FloatingActionButton(
child: Icon(Icons.warning, color: Colors.white,),
onPressed: (){},
backgroundColor: Colors.blue,
),
],
),
);
}
}
Upvotes: 2
Reputation: 54397
You can use Row
to wrap FloatingActionButton
code snippet
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
FloatingActionButton(
onPressed: () {
print("click 1");
},
child: Icon(Icons.navigation),
backgroundColor: Colors.green,
),
FloatingActionButton(
onPressed: () {
print("click 2");
},
child: Icon(Icons.event),
backgroundColor: Colors.red,
),
FloatingActionButton(
onPressed: () {
print("click 3");
},
child: Icon(Icons.gradient),
backgroundColor: Colors.yellow,
),
FloatingActionButton(
onPressed: () {
print("click 4");
},
child: Icon(Icons.album),
backgroundColor: Colors.blue,
),
]),
working demo
full code
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
void _incrementCounter() {
setState(() {
_counter++;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
FloatingActionButton(
onPressed: () {
print("click 1");
},
child: Icon(Icons.navigation),
backgroundColor: Colors.green,
),
FloatingActionButton(
onPressed: () {
print("click 2");
},
child: Icon(Icons.event),
backgroundColor: Colors.red,
),
FloatingActionButton(
onPressed: () {
print("click 3");
},
child: Icon(Icons.gradient),
backgroundColor: Colors.yellow,
),
FloatingActionButton(
onPressed: () {
print("click 4");
},
child: Icon(Icons.album),
backgroundColor: Colors.blue,
),
]),
Text(
'You have pushed the button this many times:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.headline4,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: Icon(Icons.add),
),
);
}
}
Upvotes: 0