Randomly select an item from a list using Random from dart:math
import 'dart:math'; ... int min = 0; int max = somList.length - 1; Random rnd = new Random(); int r = min + rnd.nextInt(max - min); return someList[r];
Try to have all the colors in a single class for your application. Use a color class to set colors in flutter app. It will be easy to change the color everywhere at once.
Don't
style: Textstyle(color: Colors.white, fontsize: 14),
Do
class AppColors{ static const Color red = Color(0xXFFFFO000); static const Color green = Color(oxFF4CAF50); static const Color errorRed Color(OxFFFF6E6E); } style: Textstyle(color: errorRed, fontsize: 14),
One way to avoid this error is to use the Navigator API to trigger the dialog as a route.
body: Center( child: ElevatedBut ton( child: Text ('Launch screen') onPressed: () { // Navigate to the second screen using a named route. Navigator.pushNamed (context, '/second'); // Immediately show a dialog upon loading the second screen. Navigator.push( context, PageRouteBuilder ( barrierDismissible : true, opaque: false, pageBuilder: (_, anim1, anim2) => MyDialog(), ), ); } ) )
API levels
compileSdkVersion = 33 targetSdkVersion = 33 minSdkVersion = 21
commands
flutter clean flutter pub get flutter build <platform> # e.g., flutter build apk
ConstrainedBox
- It constraints size of it's child, as per the given values.
- The size of ConstrainedBox itself depends on it's parent.
// Restrict the width between 70 — 150 ConstrainedBox( constraints: Boxconstraints( minWidth: 70, maxWidth: 150, ), child: AnyWidget(), )
UnconstrainedBox
- UnconstrainedBox imposes no constraints on its child.
- It renders the child as if it were alone on an infinite canvas with no constraints.
// No restriction applied on the child UnconstrainedBox( child: AnyWidget(), )
OverflowBox
- OverflowBox is similar to UnconstrainedBox the difference is that it won't display any warnings if the child doesn't fit the space.
- That is, it allows overflow.
// Will overflow the width after 150 px OverflowBox ( minWidth: 70, maxWidth: 150, child: AnyWidget(), )
LimitedBox
- As the name suggests, it limits the size of its child.
- Difference between ConstrainedBox and LimitedBox is that, the later limits only when it gets infinite constraints,
// limits the width to 100 px UnconstrainedBox( child: LimitedBox( maxWidth: 100, child: AnyWidget(), ) )
FittedBox
- FittedBox will increase or decrease the size of it's child, until it is completely filled.
- In the example, FittedBox scales the Text until it fills all of the available width.
// Text size will increase to occupy all 100 px Container( width: 100.0, child: FittedBox( child: Text('l am fit. '), ) )
Here's an overview of the primary sections you'll find in a pubspec.yaml file:
Metadata
At the top of the file, you'll find metadata about your project:
name: your_project_name description: A brief description of your project publish_to: 'none' # Remove this line if you want to publish to pub.dev version: 1.0.0+1 # The version number and build number
Environment
This section specifies the Dart SDK version your project supports:
environment: sdk: '>=2.12.0 <3.0.0'
Dependencies
This section lists the packages your project depends on:
dependencies: flutter: sdk: flutter cupertino_icons: ^1.0.2 http: ^0.14.0
Flutter Specific
If your project is a Flutter project, you'll want to include a flutter section:
flutter: uses-material-design: true assets: - assets/images/ - assets/icons/
Fonts
To include custom fonts in your project, use the following structure:
fonts: - family: Roboto fonts: - asset: fonts/Roboto-Regular.ttf - asset: fonts/Roboto-Bold.ttf weight: 700
Dependency Overrides
In some cases, you might need to override specific dependencies:
dependency_overrides: http: ^0.13.3
You can manage dependencies in your project using the following commands in the terminal:
Here's an example of a complete pubspec.yaml file for a Flutter project:
name: my_flutter_app description: A sample Flutter application publish_to: 'none' version: 1.0.0+1 environment: sdk: '>=2.12.0 <3.0.0' dependencies: flutter: sdk: flutter cupertino_icons: ^1.0.2 http: ^0.14.0 dev_dependencies: flutter_test: sdk: flutter flutter: uses-material-design: true assets: - assets/images/ - assets/icons/ fonts: - family: Roboto fonts: - asset: fonts/Roboto-Regular.ttf - asset: fonts/Roboto-Bold.ttf weight: 700 dependency_overrides: http: ^0.13.3