How to create colour box with fixed width and height in flutter?

Wrap any widget in a SizedBox to force it to match a fixed size. As for background colors or border, use DecoratedBox. You can then combine both, which leads to const SizedBox( width: 42.0, height: 42.0, child: const DecoratedBox( decoration: const BoxDecoration( color: Colors.red ), ), ), You may as well use Container which is … Read more

How to conditionally add widgets to a list?

EDIT: Since Dart 2.2, new syntaxes supports this natively: Column( children: [ if (foo != null) Text(foo), Bar(), ], ); This problem is currently debated on github here. But for now, you can use dart sync* functions: Row( children: toList(() sync* { if (foo == 42) { yield Text(“foo”); } }), ); where toList is: … Read more

How to create a custom AppBar widget?

import ‘package:flutter/material.dart’; class CustomAppBar extends StatefulWidget implements PreferredSizeWidget { CustomAppBar({Key key}) : preferredSize = Size.fromHeight(kToolbarHeight), super(key: key); @override final Size preferredSize; // default is 56.0 @override _CustomAppBarState createState() => _CustomAppBarState(); } class _CustomAppBarState extends State<CustomAppBar>{ @override Widget build(BuildContext context) { return AppBar( title: Text(“Sample App Bar”) ); } } Hopefully this helps

How to specify ListTile height in Flutter

Applying VisualDensity allows you to expand or contract the height of list tile. VisualDensity is compactness of UI elements. Here is an example: // negative value to contract ListTile( title: Text(‘Tile title’), dense: true, visualDensity: VisualDensity(vertical: -3), // to compact onTap: () { // tap actions }, ) // positive value to expand ListTile( title: … Read more

In Flutter, how do I pass data into a Stateless Widget?

Yes you can do this simply by passing the info to the constructor. Something like this: class MyApp extends StatelessWidget { MyApp(this.yourData); final int yourData; @override Widget build(BuildContext context) { return new MaterialApp( title: ‘App Title’, theme: new ThemeData( primarySwatch: Colors.green, ), home: new MainBody(yourData), ); } } class MainBody extends StatelessWidget { MainBody(this.yourData); final … Read more

Make Function parameter optional in custom widget flutter

Optional parameters can be either positional or named, but not both. Named parameters are optional by default so you don’t have to assign the default value. If a parameter is optional but can’t be null, provide a default value. With null safety class TextInputWithIcon extends StatefulWidget { final String iconPath; final String placeHolder; final Function(bool)? … Read more

Make buttons in a row have the same width in flutter

You can use a Row wrapping your children with Expanded: Row( children: <Widget>[ Expanded( child: RaisedButton( child: Text(‘Approve’), onPressed: () => null, ), ), Expanded( child: RaisedButton( child: Text(‘Reject’), onPressed: () => null, ), ), Expanded( child: RaisedButton( child: Text(‘Need Revise’), onPressed: () => null, ), ) ], );