Repository navigation
Expand file tree
/
Copy pathflutter.txt
More file actions
1375 lines (1141 loc) · 40.9 KB
/
Copy pathflutter.txt
File metadata and controls
1375 lines (1141 loc) · 40.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
adb tcpip 5555
adb connect 192.168.1.22:5555
flutter upgrad
flutter pub get
flutter pub upgrade
flutter pub get badges
flutter create .
FLUTTER TUTORIAL
=================
FLUTTER APP STRUCTURE:
* In flutter everything is a widget
* Every widget can maintain it's state as in background it extends statefulwidget
* While Flutter App Development, you don't have to REMEMBER EACH AND EVERYTHING,
* You have to type .propertyName & search for different properties available in Scrolling, read what it
does, can it be used, then docs for how it can be used.
* We run the app using main.dart file
* BuildContext is the location/context of the widget/element on the screen. & context is itself the element you are using it in. Thus BuildContext is the location of context
* We create different pages by creating home_page.dart or login_page.dart inside lib/pages/ folder
* Routing: initially everything is at "/", "/home" for home page, "/login" for login page
* CODE REUSE:
----------
-> If you want to use Same Code for a particular function or style then CREATE a separate widget for it & call it everywhere.o
-> You can create a new file theme.dart oranything, then create theme class & put code in it, this can be called anywhere in app.
* WIDGET SIZING:
-------------
-> Constraints Go Downwards.
-> Sizes Go Upwards
-> Parents sets postions. ("Parent Widget" Overwrites Constrainst & Sizes of "Child Widget")
* React Native uses Native Widgets which are the part of the operation system. But FLUTTER draws its own Widgets on the CANVAS/app screen using its 3 THREES.
* FLUTTER TREES:
> Widget Tree(we write in widget tree. It is Immutable, it gets erased and new widget is formed once BuildContext is called)
> Elemental Tree(It tells the Render tree what to draw. it checks the widget tree and new changes, it checks datatypes etc, then UPDATES itself.)
> Render Tree(it renders or draw what the element tree tells itself.)
* EXRACT WIDGET:
like creating a piece of code or function like button or any other mehtod into a STATELESS WIDGET,
which can also be converted to STATEFUL WIDGET if we wanna use it AS INSTANCE.
=====================================================
CLEAN CODE FORMAT:
flutter bloc vs provider VS redux
flutter block miniversion Qbit
======================================================
THEME, FONTS & ROUTING
in THEME specified in THEMES.dart we can set UNIVERSAL DEFAULT style for buttons, text, card etc.
main.dart
import 'package:flutter/material.dart';
import 'package:flutter/material.dart';
import 'package:myapp/pages/login_page.dart';
import 'package:myapp/utils/routes.dart';
import 'pages/home_page.dart';
import 'package:google_fonts/google_fonts.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
//THEME
themeMode: ThemeMode.light,
theme: ThemeData(primarySwatch: Colors.blue),
darkTheme: ThemeData(
brightness: Brightness.dark,
//
fontFamily: GoogleFonts.lato().fontFamily, //whole app
primaryTextTheme:
GoogleFonts.latoTextTheme(), //only primary text of whole app
),
//ROUTES: {Map<String, Widget Function(BuildContext)> routes = const <String, WidgetBuilder>{}}
initialRoute: "/",
routes: {
//routes are stored in utils/routes.dart > called on main.dart
MyRoutes.homeRoute: (context) => HomePage(),
"/login": (context) => const LoginPage(),
},
);
}
}
lib/utils/routes.dart
//create any random CLASS TO STORE STATIC ROUTES variable (no need to create object)
class MyRoutes {
static String loginRoute = "/login";
static String homeRoute = '/home';
}
NAVIGATION ROUTING ALOGIRTHM FOR APP:
------------------------------------
Add Routes in Routes.dart
Add Routes in Main.dart
Page1 From where you have to navigate to Page2
Page1 contains
onPressed: ()=> Navigator.pushNamed(context, MyRoutes.cartRoute)
DARK THEME WITH TOGGLE ALGORITHM:
---------------------------------
1) Add static color variables in themes.dart
2) create lightTheme & darkTheme widgets
3) call default Theme on Main.dart using theme
or if You Want theme according to SYSTEM "themeMode: ThemeMode.light, or ThemeMode.system"
4) call theme on homePage & otherPage like
backgroundColor: Theme.of(context).cardColor //cardColor is different for light & dark theme, it will show based on what CURRENT THEME IS USED.
main.dart
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
//preview
// useInheritedMediaQuery: true,
// locale: DevicePreview.locale(context),
// builder: DevicePreview.appBuilder,
//THEME
themeMode: ThemeMode.light,
theme: MyTheme.lightTheme(context),
darkTheme: MyTheme.darkTheme(context),
---------------------
themes.dart
import 'package:flutter/material.dart';
import 'package:google_fonts/google_fonts.dart';
class MyTheme{
static ThemeData lightTheme(BuildContext context) => ThemeData(
primarySwatch: Colors.blue,
fontFamily: GoogleFonts.poppins().fontFamily,
appBarTheme: const AppBarTheme(
color: Colors.white,
elevation: 0.0,
iconTheme: IconThemeData(color: Colors.black),
),
);
static ThemeData darkTheme(BuildContext context) => ThemeData(
fontFamily: GoogleFonts.poppins().fontFamily,
cardColor: darkColor,
canvasColor: darkColor,
appBarTheme: const AppBarTheme(
color: Colors.black,
elevation: 0.0,
iconTheme: IconThemeData(color: Colors.black),
),
);
//COLORS:
static Color creamColor = Color(0xfff5f5f5);
static Color darkColor = Color.fromARGB(0, 44, 43, 43);
static Color lightBlue = Color.fromARGB(0, 45, 129, 255);
static Color darkBlue = Color(0xff403b58);
}
=========================================================
classes:
--------
Dart classes are the blueprint of the object, or it can be called object constructors. A class can contain fields, functions, constructors etc.
-contain members, functions, methods and properties.
-Every object is an instance of a class,
NAMING IDENTIFIERS:
-------------------
CLASS: Use the class keyword to declare a class in Dart. A class definition starts with the keyword class followed by the class name; and the class body enclosed by a pair of curly braces.
class class_name{
<fields> //any variable declared
<getters/setters> //initialize and retrive
<constructors> //allocate memory to objects
<functions> //represent action of an object //also referred to as METHODS
}
FUNCTION: has starting letter small and other joint word capital
ex. Widget buildApp
--------
OBJECT: contains braces()
ex.
new LoginPage() => its a Object
LoginPage() => its a Object
WIDGET:-
--------
Whole FLUTTER app is a widget made up of simple widgets
widget tree:
MYAPP
>MaterialApp
>MyHomePage
>Scaffold
>>AppBar
>>Center
->Column
-->Text
-->Text
>>Floating Action Button
->Icon
### BASIC STRUCTURE ###
void main(){
runApp(const MyApp()); //MYAPP
}
class MyApp extends StatelessWidget { //STATELESS WIDGET
@override
Widget build(BuildContext context){
return MaterialApp( //MATERIAL APP
title: 'Welcome to Flutter',
home: Scaffold( //HOME: SCAFFOLD
appBar: AppBar( //APPBAR
title: const Text('Welcome to Flutter'), //TITLE
),
body: const Center( //CENTER
child: Text('Hello World') //TEXT
)
)
)
}
}
==================================================
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
//stateless widget
@override
Widget build(BuildContext build) {
//build context
return MaterialApp(
//material app
title: 'SINLGE LAYOUT',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(), //home: homepage
);
}
}
class MyHomePage extends StatelessWidget {
//MyHomePage with logic
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Overflow Widget"),
),
body: Center(
child: Container(
height: 50.0,
width: 50.0,
color: Colors.red,
),
));
}
}
==================================================
### HOME: MyApp extends StatelessWidget > Inside StatelessWidget - MyHomePage > Class MyHomePage extends STATEFUL WIDGET >inner class STATEFUL WIDGET CLASS contain logic
void main(){
runApp(const MyApp());
}
class MyApp extends StatelessWidget //STATELESS WIDGET
@override
Widget build(BuildContext context){
return MaterialApp( //MATERIAL APP
title: 'Button Click Count',
theme: ThemeData( //THEME
primarySwatch: Colors.blue
)
home: const MyHomePage //HOME: MyHomePage
)
}
class MyHomePage extends StatefulWidget //MyHomePage extends StatefulWidget
const MyHomePage
@override
State<MyHomePage> createState() => _MyHomePageState //CREATE STATEFULWIDGET CLASS
}
class _MyHomePageState extends State<MyHomePage> { //Add Logic inside STATEFULWIDGE CLASS
int _counter = 0;
void _incrementCounter(){
setState(){
_counter++;
}
}
}
---------------------------------------------------------------------------------
Stateless Widget are Immutable, meaning that their properties can't change - all values are final
Stateful Widget maintain the STATE that might CHANGE during the LIFETIME of the widget.
1) Stateful Widget class that creates an INSTANCE of a 2) state Class
2) State Class which contains logic
create suggestion var in RandomState widget inner class to store suggestion
create a listview in body using ListView.builder widget
---------------------
TYPES OF WIDGET:
1. Visible (Output and Input)
2. Invisible (Layout and Control)
TEXT: -
new Text(
'Hell World',
textAlign: textAlign.center,
style: new TextStyle(fontWeight: FontWeight.bold),
)
BUTTONS: -
//FLAT BUTTON
new FlatButton(
child: Text("Login"),
on Pressed:(){
//logic
};
),
//RAISED BUTTON
new RaisedButton(
child: Text("Login"),
elevation: 5.0,
onPressed:(){
//LOGIC
};
),
//COUNTER SHOWING ON CLICKING BUTTON EXAMPLE
body: Center(
child: Column(
maxAxisAlignment: MainAxiAlignment.center,
children: <WIDGET>[
const Text(
'you have clicked this many times' //showing text on body
),
Text(
'$_counter', //showing the count of _counter from floatingActionButton(which calculates count)
style: Theme.of(context).textTheme.headline4,
)
]
)
),
floatingActionButton: floatingActionButton(
onPressed: _incrementCounter, //counter logic on pressing button each time
tooltip: 'Increment',
child: const Icon(Icons.add), //+ logo
)
---------------------------------
MATERIAL APP:
============
MaterialApp Widget is a predefined class in a flutter. It is likely the main or core component of flutter. we can access all the other components and widgets provided by Flutter SDK
MaterialApp Widget is the starting point of your app, it tells Flutter that you are going to use Material components and follow the material design in your app.
MaterialApp is a widget that introduces a number of widgets Navigator, Theme that are required to build a material design app.
Scaffold Widget is used under MaterialApp, it gives you many basic functionalities, like AppBar, BottomNavigationBar, Drawer, FloatingActionButton, etc.
The Scaffold is designed to be the single top-level container for a MaterialApp although it is not necessary to nest a Scaffold.
----------------------------------
SCAFFOLD:
========
it is mainly responsible for creating a base to the app screen on which the child widgets hold on and render on the screen.
It provides many widgets or APIs for showing Drawer, SnackBar, BottomNavigationBar, AppBar, FloatingActionButton, and many more.
IT IS WIDGET WHICH PROVIDES FRAMEWORK TO ADD COMMON MATERIAL DESIGN ELEMENTS LIKE APPBAR, FLOATING ACTION BUTTONS, DRAWERS, ETC.
-scaffold is mainly used in stateless widget
SYNTAX:
return Scaffold{
//this is head just like in html
appBar: AppBar(
title: Text("Scaffold widgets"),
),
//body
body: Center(
child: Container(),
),
//for footer we have many widgets like DRAWER etc
drawer: Drawer(),
)
}
example:
import 'package:flutter/material.dart';
class HomePage extends StatelessWidget {
final int hours = 8;
final String name = "PiedPiper";
@override
Widget build(BuildContext context){
return Scaffold(
appBar: AppBar(
title: Text("Scaffold Widget"),
),
body: Center(
child: Container(
child: Text("Welcome to $hours of flutter with $name"),
),
),
drawer: Drawer(),
);
}
}
STATE MANAGEMENT
==============================================
STATEFUL WIDGET:
-------------------
Statefull Widgets are used when You Need to reflect in Change in UI realtime, when a user performs a action.
And you can use setState() method to trigger the new UI
like cart button Icon changes to Done button icon when user ADDS PRODUCT TO CART.
or ITEM REMOVED FROM CART MUST DISAPPEAR.
Contains 1) STATE OBJECT
2) STATE WIDGET
class Car extends StatefulWidget{
const Car({Key key, this.title}): super(key:key);
@override
_CarState createState() => _CarState();
}
class _CarState extends State<CAR>{
@override
Widget build(BuildContext context){
return Container(
color: const Color(000),
child: Container(
child: Container(//child: Container())
)
);
}
}
STATELESS WIDGET:
don't have any state information, it's a static page.
Examples: Text, Row, Column, Container, etc.
class MyStatelessCarWidget extends StatelessWidget{
const MyStatelessCarWidget({Key key}): super(key: key);
@override
Widget build(BuildContext context){
return Container(color: const Color(000));
}
}
REPRESH STATE IN STATELESS WIDGET:
using import 'package:velocity_x/velocity_x.dart';
* Create a AddMutation() WIDGET for adding items replace with _cart.add() method
& also use AddMutation() in VxState.listen(context, to: [AddMutation]) to redraw the widget in statelesswidget
or
FOR REFRESHING ONLY CERTAIN/component of a screen
> Use VxConsumer( mutations: const {RemoveMutation},) instaed of Container: []
or VxBuilder
---------------------
CONTAINER:
Widget class which allows customization of CHILD CLASS. Used to add borders, padding, colors, margins, etc.
entire row is placed in a container for adding margins and padding around the corner.
---------------------
LAYOUT A WIDGET:
STEPS:
1. Select a LAYOUT WIDGET
2. CREATE A VISIBLE(input and output) WIDGET
3. ADD VISIBLE WIDGET TO LAYOUT WIDGET
4. ADD LAYOUT WIDGET TO PAGE WHERE YOU WANT TO DISPLAY THE WIDGET
TYPES OF LAYOUT WIDGET:
1. SINGLE CHILD WIDGET :
>A widget which can have only 1 CHILD WIDGET inside the PARENT LAYOUT WIDGET.
e.g CONTAINER, PADDING, CENTER, SIZEDBOX, ASPECTRATIO, BASELINE, CONSTRAINEDBOX, CustomSingleChildLayout, FittedBox
2. MULTIPLE CHILD WIDGET
>ListView has nested Container, Card, Expanded etc or OWN BUILT COMPLEX WIDGET
-----------------------
FLUTTER GESTURES:
Flutter divides the gesture system into two different layers, which are given below:
* Pointers
* Gestures
Pointers:
Pointers are the first layer that represents the raw data about user interaction. It has events, which describe the LOCATION and MOVEMENT of pointers such as touches, mice, and style across the screens. Flutter does not provide any mechanism to cancel or stop the pointer-events from being dispatched further. Flutter provides a LISTENER WIDGET to listen to the pointer-events directly from the widgets layer. The pointer-events are categories into mainly four types:
PointerDownEvents
PointerMoveEvents
PointerUpEvents
PointerCancelEvents
GESTURES:
---------
It is the second layer that represents semantic actions such as tap, drag, and scale, which are recognized from multiple individual pointer events.
Tap, DoubleTap, Drag, LongPress, Pan, Pinch
Tap:
----
It means touching the surface of the screen from the fingertip for a short time and then releasing them. This gesture contains the following events:
onTapDown
onTapUp
onTap
onTapCancel
Double Tap: It is similar to a Tap gesture, but you need to tapping twice in a short time. This gesture contains the following events:
onDoubleTap
Drag:
-----
It allows us to touch the surface of the screen with a fingertip and move it from one location to another location and then releasing them. Flutter categories the drag into two types:
Horizontal Drag:
----------------
This gesture allows the pointer to move in a horizontal direction. It contains the following events:
onHorizontalDragStart
onHorizontalDragUpdate
onHorizontalDragEnd
Vertical Drag:
-------------
This gesture allows the pointer to move in a vertical direction. It contains the following events:
onVerticalDragStart
onVerticalDragStart
onVerticalDragStart
Long Press: It means touching the surface of the screen at a particular location for a long time. This gesture contains the following events:
onLongPress
Pan:
----
It means touching the surface of the screen with a fingertip, which can move in any direction without releasing the fingertip. This gesture contains the following events:
onPanStart
onPanUpdate
onPanEnd
Pinch: It means pinching (move one's finger and thumb or bring them together on a touchscreen) the surface of the screen using two fingers to zoom into or out of a screen.
Gesture Detector:
-----------------
Provides support for all types of Gestures, primarly used for detecting USER GESTURES. It's STATELESS WIDGET which CONTAIN "Different Parameter for Different Touch" in its CONSTRUCTOR.
-Multiple Gesture Detectors at a particular location.
-GestureDetectors decides which to RECOGNIZE based on "WHICH OF ITS CALLBACKS ARE NON-NULL".
-use onTap() event for using GestureDetector Process.
EX
/*
void main(){
}
class DemoClass extends StatelessWidget{
Widget build(BuildContext context) {
//RAW GESTURE DETECTOR
return RawGestureDetector(
gestures: {},
behavior: HitTestBehavior.opaque,
//PARENT CONTAINER
child: Container(
child: Center(
//RAW GESTURE DETECTOR
child: RawGestureDetector(
gestures: {},
//NESTED CONTAINER
child: Container(),
), // RawGestureDetector
), //Center
), //Container
); //RawGestureDetector
} //BuildContext
}
*/
----------------------------------------------------------------
FLUTTE STATE MANAGEMENT:
========================
The Stateless widget does not have any internal state. It means once it is built, we cannot change or modify it until they are initialized again. On the other hand, a Stateful widget is dynamic and has a state. It means we can modify it easily throughout its lifecycle without reinitialized it again.
A state is information that can be read when the widget is built and might change or modified over a lifetime of the app.
The "setState()" function allows us to set the properties of the state object that triggers a redraw of the UI.
UI = YOUR BUILD MEHTOD(THE APPLICATION STATE)
In Flutter, the state management categorizes into two conceptual types, which are given below:
------------------------------------------------------------------------------------------------
* Ephemeral State:
------------------
UI STATE OR LOCAL STATE. STATE RELATED TO SPECIFIC WIDGET, WHICH CONTAINS A SINGLE WIDGET.
- DATA REQUIRED BY -> SINGLE WIDGET -> EPHEMERAL STATE
-DON'T NEED TO USE STATE MANAGEMENT TECHNIQUE
-e.x. TextField
* App State:
------------
Application State or Shared State. It is different from the ephemeral state. It is a type of state that we want to share across various parts of our app and want to keep between user sessions.
-DATA REQUIRED BY -> MULTIPLE WIDGETS -> APP STATE
-It can be used GLOBALLY
-Ex. USER PREFERENCE, login info, notification in social networking app, shopping cart in e-commerce app, read/unread state of article in news app, etc.
LIBRARY: PROVIDER PACKAGE
-------------------------
*ChangeNotifier
*ChangeNotifierProvider
*Consumer
CHANGENOTIFIER: provides CHANGE notification to its listeners. Only need to use notifyListener() method to inform the listners.
CHANGENOTIFIERPROVIDER: It provides a INSTANCE of CHANGENOTIFIER to its DESCENDANTS.
CONSUMER: It just calls the provider in a new widget and delegates its build implementation to the builder.
MULTIPROVIDER: LIST OF ALL DIFFERENT PROVIDERS USED WITHIN ITS SCOPE. Used when there is a need to provide more than one class.
======================================================================
FLUTTER CONTAINER
t is to note that this widget cannot render directly without any parent widget. We can use Scaffold widget, Center widget, Padding widget, Row widget, or Column widget as its parent widget.
A basic container has a margin, border, and padding properties surrounding its child widget,
==================================================
FLUTTER ROWS:
If the child widgets need to fill the available horizontal space, we must wrap the children widgets in an Expanded widget.
A row widget does not appear scrollable because it displays the widgets within the visible view. So it is considered wrong if we have more children in a row which will not fit in the available space.
*If we want to make a scrollable list of row widgets, we need to use the ListView widget.
We can control how a row widget aligns its children based on our choice using the property crossAxisAlignment and mainAxisAlignment.
The row's cross-axis will run vertically, and the main-axis will run horizontally. See the below visual representation to understand it more clearly.
======================================================
FLUTTER COLUMNS:
This widget arranges its children in a vertical direction on the screen.
If the child widgets need to fill the available vertical space, we must wrap the children widgets in an Expanded widget.
If we want to make a scrollable list of column widgets, we need to use the ListView Widget.
We can also control how a column widget aligns its children using the property mainAxisAlignment and crossAxisAlignment. The column's cross-axis will run horizontally, and the main axis will run vertically.
=====================================================
Text, TextFormFiled:
Padding(
padding: const EdgeInsets.symmetric(vertical: 18.0, horizontal: 32.0),
child: Column (
children: [
TextFormField(
decoration: const InputDecoration(
hintText: "Enter Username",
labelText: "Username",
),
),
TextFormField(
decoration: const InputDecoration(
hintText: "Enter Password",
labelText: "Password",
),
),
],
),
),
=====================================================
BUTTONS:
Elevated Button:
----------------
login_page.dart
child: Column(
children: [
ElevatedButton(
style: TextButton.styleFrom(
textStyle: const TextStyle(fontSize: 18),
),
onPressed: (){
//logic
Navigator.pushNamed(context, MyRoutes.homeRoute);
},
child: const Text("Login"),
),
]
),
Floating Action Button: Button which overlapes & floats on a Page, it remains there irrespective action on page like scroll, photo zoom until redirected to another page or out of scope
----------------------
floatingActionButton: FloatingActionButton(
onPressed:(){},
child: const Icon(CupertinoIcons.cart),
),
=======================================================
NAVIGATION: move from one to another
Navigator:
ex. from login button on login page to home Page
login_page.dart
---------------
child: Column(
children: [
ElevatedButton(
style: TextButton.styleFrom(
textStyle: const TextStyle(fontSize: 18),
),
onPressed: (){
//logic
Navigator.pushNamed(context, MyRoutes.homeRoute);
},
child: const Text("Login"),
),
]
),
routes.dart
-----------
class MyRoutes {
static String loginRoute = "/login";
static String homeRoute = "/home";
}
main.dart
---------
import 'package:flutter/material.dart';
import 'package:myapp/pages/login_page.dart';
import 'package:myapp/utils/routes.dart';
import 'pages/home_page.dart';
import 'package:google_fonts/google_fonts.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
//THEME
themeMode: ThemeMode.light,
theme: ThemeData(primarySwatch: Colors.blue),
darkTheme: ThemeData(
brightness: Brightness.dark,
//
fontFamily: GoogleFonts.lato().fontFamily, //whole app
primaryTextTheme:
GoogleFonts.latoTextTheme(), //only primary text of whole app
),
//ROUTES: {Map<String, Widget Function(BuildContext)> routes = const <String, WidgetBuilder>{}}
initialRoute: "/",
routes: {
"/": (context) => const LoginPage(),
MyRoutes.homeRoute: (context) => HomePage(),
MyRoutes.loginRoute: (context) => const LoginPage(),
},
);
}
}
-------------------------------
// Within the `FirstRoute` widget
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => const SecondRoute()),
);
}
--------------------------------
// Within the SecondRoute widget
onPressed: () {
Navigator.pop(context);
}
URL NAVIGATION 2.0
------------------
IMPORT url_strategy
main.dart
void main(){
Vx.setPathUrlStrategy(); //NAVIGATOR INITIALIZATION
runApp( VxState(store: MyStore(),
child: const MyApp())
// DevicePreview(
// enabled: !kReleaseMode,
// builder: (context) => MyApp(), // Wrap your app for device preview
// ),
);
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp.router( //ROUTES
//preview
// useInheritedMediaQuery: true,
// locale: DevicePreview.locale(context),
// builder: DevicePreview.appBuilder,
//THEME
themeMode: ThemeMode.dark,
theme: MyTheme.lightTheme(context),
darkTheme: MyTheme.darkTheme(context),
debugShowCheckedModeBanner: false,
routeInformationParser: VxInformationParser(),
routerDelegate: VxNavigator(routes: {
"/": (_,__) => const MaterialPage(child: LoginPage()),
MyRoutes.homeRoute: (_,__) => const MaterialPage(child: HomePage()),
MyRoutes.loginRoute: (_,__) => const MaterialPage(child: LoginPage()),
MyRoutes.cartRoute: (_,__) => const MaterialPage(child: CartPage()),
MyRoutes.productDetailsRoute: (uri, _) {
final catalog = (VxState.store as MyStore).catalog.getById(int.parse(Uri.queryParameters["id"]));
return MaterialPage(child: ProductDetails(catalog: catalog));
},
}),
login_page.dart
class _LoginPageState extends State<LoginPage> {
String name = "";
bool changeButton = false; //for animation of login button
//a global key for the form widget to be used globally
final _formKey = GlobalKey<FormState>();
moveToHome(BuildContext context) async {
//if at currentState every Validation is passed then only GO TO ROUTE using login button
if (_formKey.currentState!.validate()) {
//now to change the state on login button click to show animation
setState(() {
changeButton = true;
});
context.vxNav.push(Uri.parse(MyRoutes.homeRoute));
Catalog_list.dart
PRODUCT ID ON URL
class CatalogList extends StatelessWidget{
const CatalogList({Key? key}) : super(key: key);
@override
Widget build(BuildContext context){
return !context.isMobile? GridView.builder(
gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: 3, crossAxisSpacing: 20),
shrinkWrap: true,
itemCount: CatalogModel.items.length,
itemBuilder: (context, index) {
//final catalog = CatalogModel.items[index]; ORg =
final catalog = CatalogModel.items[index];
return InkWell(
//Navigate from homeScreen to ProductDetails Page of Item of that Catalog
onTap: () => context.vxNav.push(Uri(path: MyRoutes.homeRoute, queryParameters: {"id": catalog.id.toString()}), params: catalog),
child: CatalogItem(catalog: catalog, key: null,
)
);
=====================================================
CONTAINER:
Make A Widget CLICKABLE USING "Gesture Detector: doesn't give ripple effect" or "Inkwell: gives some effect of click"
InkWell(
onTap: (){
Navigator.pushNamed(context, MyRoutes.homeRoute);
},
child: Container(
width: 127,
height: 45,
alignment: Alignment.center,
child: const Text("Login",
style: TextStyle(
color: Colors.white,
fontSize: 21,
),
),
decoration: BoxDecoration(
color: Colors.blue,
borderRadius: BorderRadius.circular(8),
),
),
),
=======================================================
ANIMATED CONTAINER:
InkWell needs to used within Material Widget.
declare bool changeButton = false;
Inside InkWell Widget button
Material(
color: Colors.blue,
//Decoration needs to given to Material not inside InkWell
borderRadius: BorderRadius.circular(changeButton ? 70 : 8),
child: InkWell(
onTap: () async {
//now to change the state on login button click to show animation
setState(() {
changeButton = true;
});
await Navigator.pushNamed(context, MyRoutes.homeRoute);
//After Login if the User comes back to login screen then it resets to normal Login BUtton without animation
setState(() {
changeButton = false;
});
},
child: AnimatedContainer(
duration: const Duration(seconds: 1),
//conditional statement for animation when changebutton = true;
width: changeButton ? 70 : 130,
height: 50,
alignment: Alignment.center,
child: changeButton
? Icon(Icons.done, color: Colors.white)
: const Text(
"Login",
style: TextStyle(
color: Colors.white,
fontSize: 20,
),
),
),
),
),
==========================================================
INK can also be used instead of CONTAINER
InkWell(
//animate button to red from primary color on click
splashColor: Colors.red,
onTap: () async {
//now to change the state on login button click to show animation
setState(() {
changeButton = true;
});
await Future.delayed(const Duration(seconds: 1));
Navigator.pushNamed(context, MyRoutes.homeRoute);
},
child: Ink(
//Ink can be used instead of Container or AnimatedCointer for More features.
//conditional statement for animation when changebutton = true;
width: changeButton ? 70 : 130,
height: 50,
child: changeButton
? const Icon(Icons.done, color: Colors.white)