1) Fetching Random Name from API using Dio and Bloc
https://blog.usejournal.com/flutter-http-requests-with-dio-rxdart-and-bloc-da325ca5fe33
original source – https://github.com/jhomlala/randomuser
2) Carousel
ref – https://pub.dev/packages/carousel_slider
First, we have an array of image URLs where images are located.
code for imgList
1 2 3 4 5 6 7 8 |
final List<String> imgList = [ 'https://images.unsplash.com/photo-1520342868574-5fa3804e551c?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=6ff92caffcdd63681a35134a6770ed3b&auto=format&fit=crop&w=1951&q=80', 'https://images.unsplash.com/photo-1522205408450-add114ad53fe?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=368f45b0888aeb0b7b08e3a1084d3ede&auto=format&fit=crop&w=1950&q=80', 'https://images.unsplash.com/photo-1519125323398-675f0ddb6308?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=94a1e718d89ca60a6337a6008341ca50&auto=format&fit=crop&w=1950&q=80', 'https://images.unsplash.com/photo-1523205771623-e0faa4d2813d?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=89719a0d55dd05e2deae4120227e6efc&auto=format&fit=crop&w=1953&q=80', 'https://images.unsplash.com/photo-1508704019882-f9cf40e475b4?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=8c6e5e3aba713b17aa1fe71ab4f0ae5b&auto=format&fit=crop&w=1352&q=80', 'https://images.unsplash.com/photo-1519985176271-adb1088fa94c?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=a0c8d632e977f94e5d312d9893258f59&auto=format&fit=crop&w=1355&q=80' ]; |
We take that array and for each item, we create a Container, with another Container as the child.
1 2 3 |
final List<Widget> imageSliders = imgList.map((item) => Container( child: Container(....) )).toList(); |
The child Container has a ClipRRect, which has a Stack.
1 2 3 4 5 6 7 8 |
ClipRRect( borderRadius: BorderRadius.all(Radius.circular(5.0)), child: Stack( children: <Widget>[ Image(...), Positioned(...) .. ..)) |
This Stack means it contains an Image, with a Positioned Widget.
The Positioned widget has a Container and fits text inside of some padding.
1 2 3 4 5 |
Container( BoxDecoration(...), Padding(...), Text(...) ); |
Thus, this completes our List of Widgets.
The code for imageSliders
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 |
final List<Widget> imageSliders = imgList.map((item) => Container( child: Container( margin: EdgeInsets.all(5.0), child: ClipRRect( borderRadius: BorderRadius.all(Radius.circular(5.0)), child: Stack( children: <Widget>[ Image.network(item, fit: BoxFit.cover, width: 1000.0), Positioned( bottom: 0.0, left: 0.0, right: 0.0, child: Container( decoration: BoxDecoration( gradient: LinearGradient( colors: [ Color.fromARGB(200, 0, 0, 0), Color.fromARGB(0, 0, 0, 0) ], begin: Alignment.bottomCenter, end: Alignment.topCenter, ), ), padding: EdgeInsets.symmetric(vertical: 10.0, horizontal: 20.0), child: Text( 'No. ${imgList.indexOf(item)} image', style: TextStyle( color: Colors.white, fontSize: 20.0, fontWeight: FontWeight.bold, ), ), ), ), ], ) ), ), )).toList(); |
We then stick this list of Widgets to the items property of CarouselSlider.
code for CarouselSlider
1 2 3 4 5 6 7 8 9 |
CarouselSlider( options: CarouselOptions( aspectRatio: 2.0, enlargeCenterPage: true, scrollDirection: Axis.vertical, autoPlay: true, ), items: imageSliders, ) |
Then in your class, you use it like so:
VerticalSliderDemo class
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
class VerticalSliderDemo extends StatelessWidget { @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar(title: Text('Vertical sliding carousel demo')), body: Container( child: CarouselSlider( options: CarouselOptions( aspectRatio: 2.0, enlargeCenterPage: true, scrollDirection: Axis.vertical, autoPlay: true, ), items: imageSliders, ) ), ); } } |
3) Navigating from one page to another
ref – https://flutter.dev/docs/cookbook/navigation/navigation-basics
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 |
import 'package:flutter/material.dart'; void main() { runApp(MaterialApp( title: 'Navigation Basics', home: FirstRoute(), )); } class FirstRoute extends StatelessWidget { @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text('First Route'), ), body: Center( child: ElevatedButton( child: Text('Open route'), onPressed: () { Navigator.push( context, MaterialPageRoute(builder: (context) => SecondRoute()), ); }, ), ), ); } } class SecondRoute extends StatelessWidget { @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text("Second Route"), ), body: Center( child: ElevatedButton( onPressed: () { Navigator.pop(context); }, child: Text('Go back!'), ), ), ); } }, |