Posts

Showing posts with the label Dart

Add Border To A Container With BorderRadius In Flutter

Image
Answer : It's not possible to add border: and borderRadius: at the same time, you'll get this error: A borderRadius can only be given for uniform borders. You can achieve what you want using the borderRadius: and a boxShadow: instead of border: like this: boxShadow: [ BoxShadow(color: Colors.green, spreadRadius: 3) ] Your sample code would be like this: Container( child: Text( 'This is a Container', textScaleFactor: 2, style: TextStyle(color: Colors.black), ), decoration: BoxDecoration( borderRadius: BorderRadius.circular(10), color: Colors.white, boxShadow: [ BoxShadow(color: Colors.green, spreadRadius: 3), ], ), height: 50, ), Edit: To achieve the example you now provided, you could do this: Container( padding: EdgeInsets.only(left: 12.0), decoration: BoxDecoration( borderRadius: BorderRadius.circular(10.0), color: Colors.green, ), height: 50, child: Container( decoration: BoxD...

Adding A Splash Screen To Flutter Apps

Image
Answer : I want to shed some more light on the actual way of doing a Splash screen in Flutter. I followed a little bit the trace here and I saw that things aren't looking so bad about the Splash Screen in Flutter. Maybe most of the devs (like me) are thinking that there isn't a Splash screen by default in Flutter and they need to do something about that. There is a Splash screen, but it's with white background and nobody can understand that there is already a splash screen for iOS and Android by default. The only thing that the developer needs to do is to put the Branding image in the right place and the splash screen will start working just like that. Here is how you can do it step by step: First on Android (because is my favorite Platform :) ) Find the "android" folder in your Flutter project. Browse to the app -> src -> main -> res folder and place all of the variants of your branding image in the corresponding folders. For example: th...

Adding Shadows At The Bottom Of A Container In Flutter?

Image
Answer : Or you can wrap your Container widget with a Material widget which contains an elevation property to give the shadowy effects. Container( child: Row( mainAxisAlignment: MainAxisAlignment.center, children: <Widget>[ Material( elevation: 15.0, child: Container( height: 100, width: 100, color: Colors.blue, child: Center(child: Text("Material",style: TextStyle(color: Colors.white),)), ), ), SizedBox(width: 100,), Container( height: 100, width: 100, decoration: BoxDecoration( boxShadow: <BoxShadow>[ BoxShadow( color: Colors.black54, blurRadius: 15.0, offset: Offset(0.0, 0.75) ...

Center Expanded ListView Inside Column Flutter

Image
Answer : The ListView fills the entire Expanded Widget, that's why using the Center widget didn't work, so shrinkWrap: true should be added so the ListView takes only the height of it's children. After skimming through the documentation I found about Flexible Widget Flexible, which does not force the child to fill the available space. Made the change and works like a charm Flexible( child: ListView.builder( shrinkWrap: true, itemCount: 4, itemBuilder: (BuildContext context, int index) => CustomAppText( text: 'Custom App', ), ), ), For those still looking for an answer, this is what worked for me: Column( children: [ Container(), // some top content Expanded( child: Center( child: ListView( shrinkWrap: true, children: [] //your list view co...

Button With Image Background Flutter

Answer : the "RaisedButton" is a material component , its take it shape depends on "material design" roles , you can create your own custom button widget GestureDetector( child: Container( width:120, height: 40, decoration: BoxDecoration( color: Colors.black, image: DecorationImage( image:AssetImage("assets/background_button.png"), fit:BoxFit.cover ), child: Text("clickMe") // button text ) ),onTap:(){ print("you clicked me"); } ) If anyone else come here looking for this, MaterialButton works perfectly. MaterialButton( padding: EdgeInsets.all(8.0), textColor: Colors.white, splashColor: Colors.greenAccent, elevation: 8.0, child: Container( decoration: BoxDecoration( image: DecorationImage( image: AssetImage('assets/button_colo...

Android Studio Where Is Color Picker For Flutter Plugin

Image
Answer : The color picker is not clickable in Android Studio running Flutter( Dart code), see picture below. But i found a work around using the Color class and manually opening color picker. Then pick a color and copy/paste it like this: Here is how i do it: 1. Double tap shift to run search 2. Type Color or Picker 3. Open Color Picker from the search list 4. Copy/paste the HEX color code into your color class. Expert tip: Add color picker to a keyboard shortcut . You can find the settings for Keymap , under File > Settings > Keymap

Adjust GridView Child Height According To The Dynamic Content In Flutter

Image
Answer : Edit: I added the constructor StaggeredTile.fit in the 0.2.0. With that you should be able to build you app ;-). First comment: For now with StaggeredGridView, the layout and the children rendering are completely independant. So as @rmtmckenzie said, you will have to get the image size to create your tiles. Then you can use StaggeredTile.count constructor with a double value for the mainAxisCellCount parameter: new StaggeredTile.count(x, x*h/w) (where h is the height of your image and w its width. So that the tile with have the same aspect ratio as your image. What you want to accomplish will need more work because you want to have an area below the image with some information. For that I think you will have to compute the real width of your tile before creating it and use the StaggeredTile.extent constructor. I understand this is not ideal and I'm currently working on a new way to create the layout. I hope it will help to build scenarios like yours. F...

Android Alarm Manager Is Not Working For Flutter Project App

Image
Answer : I've finally fixed this issue myself, after struggling for a couple of hours (but felt a lot longer!). The breakthrough came when I actually cloned the Flutter Plugins Github repository that contains android_alarm_manager and poked around the example code and looked at how it was laid out in an IDE, rather than looking at isolated files online. The Readme is not very clear on what exactly to do, if you're not versed in Android Java development, but it becomes clear when you look at the working example code. You need to drop in the Application.java file they give you in the example directory into your actual project, in the same folder as your existing MainActivity.java file. The contents should look like this: package io.flutter.plugins.androidalarmmanagerexample; import io.flutter.app.FlutterApplication; import io.flutter.plugin.common.PluginRegistry; import io.flutter.plugin.common.PluginRegistry.PluginRegistrantCallback; import io.flutter.plugins.Genera...

Adding A Gradient Background To Appbar On Flutter

Image
Answer : I don't believe you can pass a gradient to an AppBar as it expects a Color rather than a gradient. You can, however, create your own widget that mimics an AppBar except by using a gradient. Take a look at this example that I've pieced together from the Planets-Flutter tutorial along with the code below it. import "package:flutter/material.dart"; class Page extends StatelessWidget { @override Widget build(BuildContext context) { return Column(children : <Widget>[GradientAppBar("Custom Gradient App Bar"), Container()],); } } class GradientAppBar extends StatelessWidget { final String title; final double barHeight = 50.0; GradientAppBar(this.title); @override Widget build(BuildContext context) { final double statusbarHeight = MediaQuery .of(context) .padding .top; return new Container( padding: EdgeInsets.only(top: statusbarHeight), height: statusbarHeight + barHeigh...

Angular2 Dynamic Change CSS Property

Answer : 1) Using inline styles <div [style.color]="myDynamicColor"> 2) Use multiple CSS classes mapping to what you want and switch classes like: /* CSS */ .theme { /* any shared styles */ } .theme.blue { color: blue; } .theme.red { color: red; } /* Template */ <div class="theme" [ngClass]="{blue: isBlue, red: isRed}"> <div class="theme" [class.blue]="isBlue"> Code samples from: https://angular.io/cheatsheet More info on ngClass directive : https://angular.io/docs/ts/latest/api/common/index/NgClass-directive.html Just use standard CSS variables: Your global css (eg: styles.css) body { --my-var: #000 } In your component's css or whatever it is: span { color: var(--my-var) } Then you can change the value of the variable directly with TS/JS by setting inline style to html element: document.querySelector("body").style.cssText = "--my-var: #000"; Otherwise you...

Check Value In Array Exists Flutter Dart

Answer : list.contains(x); Contains method Above are the correct answers to the current question. But if someone like me is here to check value inside List of Class object then here is the answer. class DownloadedFile { String Url; String location; } List of DownloadedFile List<DownloadedFile> listOfDownloadedFile = List(); listOfDownloadedFile.add(...); Now check if a specific value is inside this list var contain = listOfDownloadedFile.where((element) => element.Url == "your URL link"); if (contain.isEmpty) //value not exists else //value exists There maybe better way/approach. If someone know, then let me know. :) List<int> values = [1, 2, 3, 4]; values.contains(1); // true values.contains(99); // false Method - Contains of Iterable does exactly what you need. See the example above.

Center Widget Vertically Inside A SingleChildScrollView

Image
Answer : ArtiomLK Suggested a solution in comments which helped me: wrap SingleChildScrollView in a Center . The widgets tree is: Center( child: SingleChildScrollView( child: Column(...))) None of the others helped. Solution: Put your top level Stack inside Center widget. body: Center(child: Stack( children: _buildBody(), ))); Tip to debug: Use Flutter Inspector to find where the layout is going wrong. I edited your code a bit(to make to work in my local) and then I inspected. It showed like below We have a Stack and SingleChildScrollView as per code(refer right side of the diagram where the stack of widgets are displayed). As size is determined by SingleChildScrollView (contents inside it), Stack occupies only little space and by default, it aligned at top . So put it under Center , the whole Stack view will come in center. There's a section about it in the official docs: Using SingleChildScrollView with a Column Source: https://api....

Blurred Decoration Image In Flutter

Image
Answer : You could do something like this, by blurring the container child instead. class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return new MaterialApp( home: new Container( decoration: new BoxDecoration( image: new DecorationImage( image: new ExactAssetImage('assets/dog.png'), fit: BoxFit.cover, ), ), child: new BackdropFilter( filter: new ImageFilter.blur(sigmaX: 10.0, sigmaY: 10.0), child: new Container( decoration: new BoxDecoration(color: Colors.white.withOpacity(0.0)), ), ), ), ); } } Screenshot Screenshot: Using Stack : SizedBox( height: 200, child: Stack( fit: StackFit.expand, children: [ Image.asset('chocolate_image', fit: BoxFit.cover), ClipRRect( // Clip it cleanly. child: BackdropFilter( filter: ImageFilter.blur(sigmaX: 10,...

Background Concurrent Copying GC Freed - Flutter

Answer : This is not an error, it's just an Android log message notifying you about when garbage collection takes place. Everything's normal. The log messages don't harm your app, see this question regarding the same topic on native Android. It's only a problem if you go out of memory, or you see performance hiccups due to garbage collection. Phew. That being said, let's see how you might get less of these messages. Typically, an emulator's resources are limited . So, the easiest way would be to increase the emulator's RAM size or use an actual phone instead of an emulator. Secondly, make sure your logic doesn't handle huge amounts of data , or if it does, that it gets disposed as soon as possible. Also, don't "cache" widgets yourself by storing them in a state like this: class _MyWidgetState extends State<MyWidget> { Widget button; @override void initState() { super.initState(); button = RaisedButton(.....

Change LabelText Direction To RTL In InputDecoration() Flutter

Answer : Simply use Directionality: new Directionality( textDirection: TextDirection.rtl, child: TextField( textAlign: TextAlign.right, controller: _textEdittingControler_bookName, autofocus: true, decoration: new InputDecoration( labelText: "افزودن کتاب", hintText: "نام کتاب را وارد کنید" ), ) Directionality's docs Can be used textAlign TextFormField( textAlign: TextAlign.right, decoration: InputDecoration( hintText: 'ادخل تفاصيل الكتابة الخاصة بك', ),

BannerAd.dispose Not Working Flutter Admob

Answer : You need to dispose the banner like this try { _myBanner?.dispose(); _myBanner = null; } catch (ex) { log("banner dispose error"); }