Skip to content

Instantly share code, notes, and snippets.

@loonix
Created April 2, 2020 07:37
Show Gist options
  • Save loonix/757ada47083f00fd7dbfeef1a71fbc02 to your computer and use it in GitHub Desktop.
Save loonix/757ada47083f00fd7dbfeef1a71fbc02 to your computer and use it in GitHub Desktop.
[Flutter] Snippets #flutter
///
/// Converting class objects to JSON string in Flutter
///
class Employee {
final String name;
final String email;
Employee(this.name, this.email);
Employee.fromJson(Map<String, dynamic> json)
: name = json['name'],
email = json['email'];
Map<String, dynamic> toJson() =>
{
'name': name,
'email': email,
};
}
//Now CONVERT SIMPLE JSON TO FLUTTER OBJECT
Map employeeMap = jsonDecode(jsonString);
var employee = Employee.fromJson(employeeMap);
//CONVERT FLUTTER OBJECT TO SIMPLE JSON STRING
String json = jsonEncode(employee);
///
/// Hide title from BottomNavigationBar in Flutter
///
BottomNavigationBar(
showSelectedLabels: false,
showUnselectedLabels: false,
items: <BottomNavigationBarItem> []
);
///
/// Add box shadow to container in flutter
///
new Container(
height: 200.0,
decoration: new BoxDecoration(
boxShadow: [
BoxShadow(
color: Colors.red,
blurRadius: 25.0, // soften the shadow
spreadRadius: 5.0, //extend the shadow
offset: Offset(
15.0, // Move to right 10 horizontally
15.0, // Move to bottom 10 Vertically
),
)
],
);
child: new Text("Hello world"),
);
///
///Add border to widget in Flutter
///
//ADD BORDER TO ALL SIDES
Container(
decoration: BoxDecoration(
border: Border.all(
color: Colors.red,
width: 3.0,
),
),
)
//ADD BORDER TO SPECIFIC SIDES
Container(
decoration: BoxDecoration(
border: Border(
left: BorderSide(
color: Colors.red,
width: 3.0,
),
top: BorderSide(
color: Colors.blue,
width: 3.0,
),
),
),
)
///
///Simple stateless widget in Flutter
///
import 'package:flutter/material.dart'
class WidgetName extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'App title',
home: Container(
child: Text('Hello World')
)
)
}
}
///
/// Add padding to container in Flutter
///
Container(
padding: EdgeInsets.all(50.0),
child: Text(
'Hello world'
),
),
///
/// Add margin to container in Flutter
///
Container(
margin: EdgeInsets.all(40.0),
child: Text(
'Hello world'
),
),
///
/// Add border radius to container in flutter
///
Container(
margin: EdgeInsets.all(30.0),
padding: EdgeInsets.all(20.0),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(10.0),
color: Colors.red,
),
child: Text(
'Hello world'
),
),
///
/// Add border to container in flutter
///
Container(
decoration: BoxDecoration(
border: Border.all(
width: 2.0,
color: Colors.blue,
style: BorderStyle.solid
),
),
child: Text(
'Hello world'
),
),
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment