Skip to content

Instantly share code, notes, and snippets.

View Blasanka's full-sized avatar
🚀
Flutter dev

B Liyanage Asanka Blasanka

🚀
Flutter dev
View GitHub Profile
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: MyHomePage(),
);
@Blasanka
Blasanka / horizontal_list_alignment.dart
Created May 4, 2020 13:15
Center items as long as there are only few items on the list which does not exceed available screen size and when it does and scrolling is available left align all items in the list view.
// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
@Blasanka
Blasanka / nested_list_iteration.dart
Last active May 1, 2020 16:05
This is a example to interate over nested list and access map inside it and render values on it.
import 'dart:convert';
import 'package:flutter/material.dart';
final Color darkBlue = Color.fromARGB(255, 18, 32, 47);
void main() {
runApp(MyApp());
}
@Blasanka
Blasanka / replash_hash_tag_url.dart
Created January 23, 2020 02:14
This is how you can replace hash tag with forward slashes in url
void main() {
String text = "Test #/first1hashtag test #/second2hashtag test.";
RegExp exp = new RegExp(r"\B#\/");
exp.allMatches(text).forEach((match){
print(match.group(0));
text = text.replaceAll(match.group(0), "");
});
print(text);
String text2 = "Test #/first1hashtag test #/second2hashtag test.";
void main() {
var locations = {
{
'country': 'Egypt',
'city': 'Cairo',
'Latitude': 30.033333,
'Longitude': 31.233334,
'utcOffset': 2
},
{
@Blasanka
Blasanka / str_manipulation.dart
Created January 16, 2020 06:23
Different ways to replace string
void main() {
String str = "one.two";
print(str.replaceAll(".two", ""));
// or
print(str.split(".").first);// split() will split from . and gives new List with separated elements.
// or
@Blasanka
Blasanka / add_map_to_complex_map.dart
Created January 15, 2020 16:18
Here is how to add complex map to complex map.
void main() {
Map parentMap = {
'childOne': {
'grandSon': {
'grandGrandChild': {
'childOne': 'John',
'childTwo': 'Ron'
}
},
'grandDaughter': {
@Blasanka
Blasanka / main.dart
Last active January 15, 2020 11:30
How to add values to a Map and finally that map to a list (Dart).
void main() {
List<Map> _pecasList = [];
List<String> alphas = ["a", "b", "c"];
Map<String, dynamic> newPecas = Map();
for (String c in alphas) {
if (c.isEmpty || c.trimLeft() == "") {
print("Campo Vazio");
} else {
@Blasanka
Blasanka / main.dart
Created December 29, 2019 04:51
Compare two list and create new list that doesnt have matching elements.
void main() {
List<String> first = ['A','B','C','D'];
List<String> second = ['B','D'];
List<String> result = first.where((item) => !second.contains(item)).toList();
print(result);
}
@Blasanka
Blasanka / index.dart
Last active December 22, 2019 14:51
Regular expression to find urls in String
void main() {
final text = """My website url: https://blasanka.github.io/
Google search using: www.google.com, social media is facebook.com, http://example.com/method?param=flutter
stackoverflow.com is my greatest website. DartPad share: https://github.com/dart-lang/dart-pad/wiki/Sharing-Guide see this example and edit it here """;
RegExp exp = new RegExp(r'(?:(?:https?|ftp):\/\/)?[\w/\-?=%.]+\.[\w/\-?=%.]+');
Iterable<RegExpMatch> matches = exp.allMatches(text);
matches.forEach((match) {
print(text.substring(match.start, match.end));