Flutter & Dart Tips - Week In Review #8
source link: https://dev.to/offlineprogrammer/flutter-dart-tips-week-in-review-8-3jk7
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Week In Review (8 Part Series)
Hello Reader,
This post is the 8th of the Flutter & Dart Tips series. My goal is to have at least 100 tips in this series. Do you know how many I have shared with you so far?
1- You can use the validator widget to validate the Form Inputs in your submit function and apply the validation condition on each TextFormField.
...
void _submit() {
final isValid = _formKey.currentState.validate();
if (!isValid) {
return;
}
_formKey.currentState.save();
}
...
Form(
key: _formKey,
child: Column(
children: [
Text(
"Form-Validation",
style: TextStyle(fontSize: 24.0, fontWeight: FontWeight.bold),
),
//styling
SizedBox(
height: MediaQuery.of(context).size.width * 0.1,
),
TextFormField(
decoration: InputDecoration(labelText: 'E-Mail'),
keyboardType: TextInputType.emailAddress,
onFieldSubmitted: (value) {
//Validator
},
validator: (value) {
if (value.isEmpty ||
!RegExp(r"^[a-zA-Z0-9.a-zA-Z0-9.!#$%&'*+-/=?^_`{|}~]+@[a-zA-Z0-9]+\.[a-zA-Z]+")
.hasMatch(value)) {
return 'Enter a valid email!';
}
return null;
},
),
//box styling
SizedBox(
height: MediaQuery.of(context).size.width * 0.1,
),
//text input
TextFormField(
decoration: InputDecoration(labelText: 'Password'),
keyboardType: TextInputType.emailAddress,
onFieldSubmitted: (value) {},
obscureText: true,
validator: (value) {
if (value.isEmpty) {
return 'Enter a valid password!';
}
return null;
},
),
SizedBox(
height: MediaQuery.of(context).size.width * 0.1,
),
RaisedButton(
padding: EdgeInsets.symmetric(
vertical: 10.0,
horizontal: 15.0,
),
child: Text(
"Submit",
style: TextStyle(
fontSize: 24.0,
),
),
onPressed: () => _submit(),
)
],
),
),
...
2- Use the ListView to create a horizontal list by setting the scrollDirection parameter to Axis.horizontal
...
ListView(
scrollDirection: Axis.horizontal,
children: [
Container(
height: 480.0,
width: 240.0,
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage('images/image1.png'),
fit: BoxFit.fill,
),
shape: BoxShape.rectangle,
),
),
Container(
height: 480.0,
width: 240.0,
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage('images/image2.webp'),
fit: BoxFit.fill,
),
shape: BoxShape.rectangle,
),
),
Container(
height: 480.0,
width: 240.0,
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage('images/image3.jpg'),
fit: BoxFit.fill,
),
shape: BoxShape.rectangle,
),
),
Container(
height: 480.0,
width: 240.0,
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage('images/image4.jpg'),
fit: BoxFit.fill,
),
shape: BoxShape.rectangle,
),
),
],
),
...
3- Center the FloatingActionButton by setting the floatingActionButtonLocation parameter as below
...
floatingActionButton: FloatingActionButton(
onPressed: () {},
child: Icon(Icons.settings_voice),
),
floatingActionButtonLocation: FloatingActionButtonLocation.centerFloat,
...
4- You can provide an action label in SnackBar to act when pressed on it as below
...
final snackBar = SnackBar(
content: const Text('Amazing SnackBar!'),
action: SnackBarAction(
label: 'Undo',
onPressed: () {
// Do something.
},
),
);
...
5- Use the barrierDismissible property to prevent dismissing the AlertDialog when the user taps on the screen outside the alert box.
...
showDialog<String>(
context: context,
barrierDismissible: false,
builder: (BuildContext context) => AlertDialog(
title: const Text('This is a title'),
content: const Text('This is a description'),
actions: <Widget>[
TextButton(
onPressed: () => Navigator.pop(context, 'Cancel'),
child: const Text('Cancel'),
),
TextButton(
onPressed: () => Navigator.pop(context, 'OK'),
child: const Text('OK'),
),
],
),
),
...
6- You can create a circular icon button using ElevatedButton. Check the following example.
ElevatedButton(
style: ElevatedButton.styleFrom(
shape: CircleBorder(), padding: EdgeInsets.all(30)),
child: Icon(
Icons.add,
size: 50,
),
onPressed: () {},
)
See you next week. 👋🏻
Follow me on Twitter for more tips about #coding, #learning, #technology...etc.
Check my Apps on Google Play & Apple Store
Cover image Ethan Robertson on Unsplash
Recommend
-
19
许多语言学家认为,一个人说的自然语言会影响他们的思维方式。这个理论适用于计算机语...
-
40
What Is Flutter? Flutter is an open-source UI framework for creating high-level native interfaces on Android and iOS. The initial release of Flutt...
-
50
奇技指南 本文是Flutter系列第四篇文章,阅读本文 你将得到什么: 1、dart:io 是什么? 2、dart:io 使用 3、dart:io 组成部分 4、dart:io 总结 本文...
-
6
Creating in-memory databases using SQLite in Dart and Flutter Thursday, October 29, 2020 I have been playing around with the Alpha version of Flutter Desktop lately, and needed to us...
-
6
Article Flutter Immutable collections and objects for Flutter and Dart Bevan Steele 22 Oct 2020 • 2 min read
-
22
1 什么是Stream? Stream是Dart用来处理异步的API,和同样用来处理异步的Future不同的是,Stream可以异步的返回多个结果,而Future只能返回一个,如果你对Future有疑问,可以参考作者的上一篇文章,
-
3
January 20, 2021 / #Flutter How to Build a Chat App UI With Flutter and Dart
-
5
Files Permalink Latest commit message Commit time
-
6
Flutter&Dart日期格式化及时间戳转换 分类: Flutter&Dart, 编程语言作者: m...
-
6
Introducing Realm for Dart & FlutterA case study in using Dart platform primitives to build a rich multi-platform library for Dart and Flutter apps.We’re excited to see MongoDB...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK