Flutter Tips
Hey Coders 🤓 ,
Welcome to flutter tips,
Here I m going to add Flutter snippets which are useful,
although yes we can google and all but instead I want it all at one place,
So easy to copy and use ,
Basically I was making this for me only ,later I thought it can be useful to every coder,
So as I'm Going to learn as well as I'm Going to update this page.
So Enjoy Flutter Guys !!
FAB at center
class ContaPage extends StatelessWidget {
@override
Widget build(BuildContext context) => new Scaffold(
floatingActionButton: new FloatingActionButton(
),
floatingActionButtonLocation: FloatingActionButtonLocation.centerFloat,
);
}
FAB background Color
floatingActionButton: FloatingActionButton(
onPressed: () {
},
child: Container(
width: 60,
height: 60,
child: Icon(
Icons.add,
),
decoration: new BoxDecoration(
shape: BoxShape.circle,
gradient: new RadialGradient(
colors: [Colors.orange, Color(0xFFF500).withOpacity(0.5)],
),
),
),
),
Decoration common code:
decoration: new BoxDecoration(
shape: BoxShape.circle,
gradient: new RadialGradient(
colors: [Colors.orange, Color(0xFFF500).withOpacity(0.5)],
),
),
Decoration Gradient Color
Reference for extra:Gradient
body: Container(
decoration: BoxDecoration(
gradient: LinearGradient(
begin: Alignment(6.123234262925839e-17, 1),
end: Alignment(-1, 6.123234262925839e-17),
begin: Alignment.topLeft,
end: Alignment.bottomRight,
stops: [0.3, 1],
colors: [
Color.fromRGBO(69, 182, 73, 1),
Color.fromRGBO(46, 196, 182, 1)
]),
),
onPressed: () {
showModalBottomSheet<void>(
context: context,
builder: (BuildContext context) {
return Container(
height: 200,
color: Colors.amber,
child: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
mainAxisSize: MainAxisSize.min,
children: <Widget>[
const Text('Modal BottomSheet'),
],
),
),
);
},
);
},
Border Radius
decoration: BoxDecoration(
borderRadius: BorderRadius.only(
topLeft: Radius.circular(25),
topRight: Radius.circular(25)),
)
Important Modal Bottom Sheet points:
In Bottom Sheet if we apply radius it may show white color at background
So 2 Solutions :
1.Override
showModalBottomSheet<void>(
backgroundColor: Colors.transparent,
......
.
2.Set Theme
MaterialApp(
theme: ThemeData(
bottomSheetTheme: BottomSheetThemeData(
backgroundColor: Colors.white,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.vertical(top: Radius.circular(10))
)
)
),
Use Google Font
import 'package:google_fonts/google_fonts.dart';
Text('What made you Happy today?',
style:
GoogleFonts.alice(color: Colors.white, fontSize: 18),
),
Add Space Box (Empty Space)
SizedBox(height: 80)
Simple Text Field In Container
Define Text Editing Controller first
TextEditingController nameController = TextEditingController();
Text Field
Container(
margin: EdgeInsets.all(40),
child: TextField(
controller: nameController,
decoration: InputDecoration(
hintText: 'I realize my worth..today',
hintStyle: GoogleFonts.alice(color: Colors.white)),
onChanged: (text) {
setState(() {
});
}),
),
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => FeelingScreen()),
);
},
Gesture Detector which can be applied to any component
So we can make any Component clickable Unlike OnPressed() its is available to
some components only,So Just Wrap any component By Gesture Detector to use it.
GestureDetector(
child: Image.asset("assets/images/funnyemo.png"),
onTap: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => Jardone()),
);
}),
Center Column or Row Content
Column:
mainAxisAlignment: MainAxisAlignment.center
crossAxisAlignment: CrossAxisAlignment.center
Row:
mainAxisAlignment: MainAxisAlignment.center
crossAxisAlignment: CrossAxisAlignment.center
Add Image
Add Image from asset:
Image.asset(
"assets/images/jar.png",
width: 200,
height: 400,
)
Size :By height and width
Image.asset(
'assets/images/file-name.jpg',
height: 100,
width: 100,
)
Adjust Image Size by Scale
Image.asset(
'assets/images/file-name.jpg',
scale: 0.8
)
Fit Image Size
Image.asset(
'assets/images/file-name.jpg',
height: 100,
width: 200,
fit: BoxFit.fitWidth,
)
Blend Image with a Color
Image.asset(
'assets/images/file-name.jpg',
height: 100,
width: 100,
color: Colors.red,
colorBlendMode: BlendMode.darken,
fit: BoxFit.fitWidth,
)