'How to create dynamic introduction_screen in Flutter with introduction_screen package package link in description
How to create dynamic introduction_screen in Flutter with introduction_screen package.
Means We are pass 3 screen in api. introduction_screen 3 pages.
We are pass 4 screen in api. introduction_screen 4 pages.
Package Link: https://pub.dev/packages/introduction_screen
Api Like this (3 screen):
[
{
"name_Title": "title name",
"description" "Description"
},
{
"name_Title": "title name",
"description" "Description"
},
{
"name_Title": "title name",
"description" "Description"
},
],
Api Like this (4 screen):
[
{
"name_Title": "title name",
"description" "Description"
},
{
"name_Title": "title name",
"description" "Description"
},
{
"name_Title": "title name",
"description" "Description"
},
{
"name_Title": "title name",
"description" "Description"
},
]
Thank You for giving me your important time.
Solution 1:[1]
I am used page view builder.
Why: Because introduction_screen package had used page view. Not using page view builder.
That means page view builder property build run time widget.
And page view not build runtime widget.
that means you first define how many widget print like 5, that print 5 widget.
****Main thing we are use builder because it's draw widgets runtime like Future builder and list view builder
you need a code. please reply this answer below thank you
Solution 2:[2]
You need to parse your data to PageViewModel:
// your api data
List listViews = [
{"name_Title": "title name", "description": "Description"},
{"name_Title": "title name", "description": "Description"},
{"name_Title": "title name", "description": "Description"},
{"name_Title": "title name", "description": "Description"}
];
//method to convert data to PageViewModel
Widget PageModelView getPageViewModel(dynamic data) {
return PageViewModel(
title: data.name_Title,
bodyWidget: Center(child: Text(data.description)),
);
}
// call your introduction screen like that
IntroductionScreen(
pages: listViews.map((e) => getPageViewModel(e)).toList(),
done: const Text("Done", style: TextStyle(fontWeight: FontWeight.w600)),
onDone: () {
// When done button is press
},
);
Solution 3:[3]
Convert your JSON into a model class and then use it to create a list of PageViewModel
Your model class will be:
// To parse this JSON data, do
//
// final welcome = welcomeFromJson(jsonString);
import 'dart:convert';
List<Welcome> welcomeFromJson(String str) => List<Welcome>.from(json.decode(str).map((x) => Welcome.fromJson(x)));
String welcomeToJson(List<Welcome> data) => json.encode(List<dynamic>.from(data.map((x) => x.toJson())));
class Welcome {
Welcome({
this.nameTitle,
this.description,
});
String nameTitle;
String description;
factory Welcome.fromJson(Map<String, dynamic> json) => Welcome(
nameTitle: json["name_Title"] == null ? null : json["name_Title"],
description: json["description"] == null ? null : json["description"],
);
Map<String, dynamic> toJson() => {
"name_Title": nameTitle == null ? null : nameTitle,
"description": description == null ? null : description,
};
}
Solution 4:[4]
look this Example
Link :
https://github.com/Tbsdev-Anmol/Schoolie/blob/main/lib/Screen/Intor_Page.dart
class Home extends StatefulWidget {
const Home({Key? key}) : super(key: key);
@override
_HomeState createState() => _HomeState();
}
class _HomeState extends State<Home> {
List lists {
return [
{"name_Title": "title name", "description": "Description"},
{"name_Title": "title name", "description": "Description"},
{"name_Title": "title name", "description": "Description"},
{"name_Title": "title name", "description": "Description"}
];
}
Widget PageModelView getPageViewModel(dynamic data) {
return PageViewModel(
title: data.name_Title,
bodyWidget: Center(child: Text(data.description)),
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.white,
body: SingleChildScrollView(
scrollDirection: Axis.vertical,
child: Column(children: [
Padding(
padding: const EdgeInsets.only(top: 25),
child: Container(height: MediaQuery.of(context).size.height*.7,
child: IntroductionScreen(
next: Text(""),
pages: lists.map((e) => getPageViewModel(e)).toList(),
showNextButton: false,
showDoneButton: false,
done: Text(""),
onDone: () {},
),
),
),
InkWell(onTap: (){
Navigator.pushReplacement(context,
MaterialPageRoute(builder:
(context) =>
(){}
)
);
},
child: Container(
width: MediaQuery.of(context).size.width*.8,
height: 50,
decoration: BoxDecoration(
gradient: LinearGradient(
colors: [
MyColors.Intro_Text_Color,
MyColors.Intro_button_Color
]
)
),
child: Center(child: Text("Get started",style: TextStyle(color: MyColors.Text_Coloe_white,fontSize: 20,fontWeight: FontWeight.bold),)),
),
)
],),
),
);
}
}
Solution 5:[5]
Intromodel.dart
class IntroModel {
String? nameTitle;
String? description;
IntroModel({this.nameTitle, this.description});
IntroModel.fromJson(Map<String, dynamic> json) {
nameTitle = json['name_Title'];
description = json['description'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['name_Title'] = this.nameTitle;
data['description'] = this.description;
return data;
}
}
Intropage.dart
class IntroPage extends StatelessWidget {
final Isscar4;
IntroPage({
Key? key,
required this.Isscar4,
}) : super(key: key);
List<PageViewModel> listPagesViewModel = [];
List<IntroModel> models = [];
@override
Widget build(BuildContext context) {
List<dynamic> intro = fetchIntroApi(Isscar4);
intro.forEach((element) {
var element2 = element as Map<String, dynamic>;
var cd = IntroModel.fromJson(element2);
models.add(cd);
});
models.forEach((element) {
listPagesViewModel.add(PageViewModel(
title: element.nameTitle,
body: element.description,
image: Center(
child: Image.network(
Isscar4
? "https://c.tenor.com/75ffA59OV-sAAAAC/broke-down-red-car.gif"
: 'https://c.tenor.com/Jd1J0SSD_3sAAAAC/bike-bicycle.gif',
height: 175.0),
),
));
});
return MaterialApp(
debugShowCheckedModeBanner: false,
initialRoute: "/intro",
home: Scaffold(
body: IntroductionScreen(
next: ElevatedButton(
onPressed: () {},
child: Text(''),
),
pages: listPagesViewModel,
done: const Text('Done',
style: TextStyle(fontWeight: FontWeight.w600)),
onDone: () {
// When done button is press
},
),
));
}
List fetchIntroApi(bool bool) {
var four = bool;
if (four) {
var data =
'[ {"name_Title": "title name1","description": "description1"}, {"name_Title": "title name2","description": "description2"}, {"name_Title": "title name3","description": "description3"}, {"name_Title": "title name4","description": "description4"} ]';
return json.decode(data);
} else {
var data =
'[ {"name_Title": "title name","description": "description1"}, {"name_Title": "title name2","description": "description2"}, {"name_Title": "title name3","description": "description3"} ]';
return json.decode(data);
}
}
}
MainPage.dart
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
// });
return MaterialApp(
debugShowCheckedModeBanner: false,
initialRoute: "/",
home: Scaffold(
body:
Column(mainAxisAlignment: MainAxisAlignment.center, children: [
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Container(
height: 100,
width: 100,
child: ElevatedButton(
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => IntroPage(Isscar4: true)),
);
},
child: RichText(
text: TextSpan(
text: 'CAR',
style: TextStyle(
letterSpacing: 3,
color: Colors.white,
fontWeight: FontWeight.w400),
children: [
TextSpan(
text: '4',
style: TextStyle(
fontSize: 25,
color: Colors.red,
fontWeight: FontWeight.bold))
],
)
// IntroPage(listPagesViewModel: listPagesViewModel) //Material App,
),
),
),
],
),
SizedBox(
height: 10,
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Container(
height: 100,
width: 100,
child: ElevatedButton(
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => IntroPage(Isscar4: false)),
);
},
child: RichText(
text: TextSpan(
text: 'BIKE',
style: TextStyle(
letterSpacing: 3,
color: Colors.white,
fontWeight: FontWeight.w400),
children: [
TextSpan(
text: '2',
style: TextStyle(
fontSize: 25,
color: Colors.red,
fontWeight: FontWeight.bold))
],
)
// IntroPage(listPagesViewModel: listPagesViewModel) //Material App,
),
),
),
],
)
])));
}
MaterialApp Swithwidget(istrue) {
return MaterialApp(
initialRoute: "/intro",
home: Scaffold(
body: IntroPage(
Isscar4: istrue,
),
),
);
}
}
FullCode
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:introduction_screen/introduction_screen.dart';
void main() {
runApp(
// For widgets to be able to read providers, we need to wrap the entire
// application in a 'ProviderScope' widget.
// This is where the state of our providers will be stored.
MaterialApp(debugShowCheckedModeBanner: false, home: MyApp()),
);
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
// });
return MaterialApp(
debugShowCheckedModeBanner: false,
initialRoute: "/",
home: Scaffold(
body:
Column(mainAxisAlignment: MainAxisAlignment.center, children: [
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Container(
height: 100,
width: 100,
child: ElevatedButton(
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => IntroPage(Isscar4: true)),
);
},
child: RichText(
text: TextSpan(
text: 'CAR',
style: TextStyle(
letterSpacing: 3,
color: Colors.white,
fontWeight: FontWeight.w400),
children: [
TextSpan(
text: '4',
style: TextStyle(
fontSize: 25,
color: Colors.red,
fontWeight: FontWeight.bold))
],
)
// IntroPage(listPagesViewModel: listPagesViewModel) //Material App,
),
),
),
],
),
SizedBox(
height: 10,
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Container(
height: 100,
width: 100,
child: ElevatedButton(
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => IntroPage(Isscar4: false)),
);
},
child: RichText(
text: TextSpan(
text: 'BIKE',
style: TextStyle(
letterSpacing: 3,
color: Colors.white,
fontWeight: FontWeight.w400),
children: [
TextSpan(
text: '2',
style: TextStyle(
fontSize: 25,
color: Colors.red,
fontWeight: FontWeight.bold))
],
)
// IntroPage(listPagesViewModel: listPagesViewModel) //Material App,
),
),
),
],
)
])));
}
MaterialApp Swithwidget(istrue) {
return MaterialApp(
initialRoute: "/intro",
home: Scaffold(
body: IntroPage(
Isscar4: istrue,
),
),
);
}
}
class Hi extends StatelessWidget {
const Hi({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Container(
child: Text("df"),
);
}
}
class IntroPage extends StatelessWidget {
final Isscar4;
IntroPage({
Key? key,
required this.Isscar4,
}) : super(key: key);
List<PageViewModel> listPagesViewModel = [];
List<IntroModel> models = [];
@override
Widget build(BuildContext context) {
List<dynamic> intro = fetchIntroApi(Isscar4);
intro.forEach((element) {
var element2 = element as Map<String, dynamic>;
var cd = IntroModel.fromJson(element2);
models.add(cd);
});
models.forEach((element) {
listPagesViewModel.add(PageViewModel(
title: element.nameTitle,
body: element.description,
image: Center(
child: Image.network(
Isscar4
? "https://c.tenor.com/75ffA59OV-sAAAAC/broke-down-red-car.gif"
: 'https://c.tenor.com/Jd1J0SSD_3sAAAAC/bike-bicycle.gif',
height: 175.0),
),
));
});
return MaterialApp(
debugShowCheckedModeBanner: false,
initialRoute: "/intro",
home: Scaffold(
body: IntroductionScreen(
next: ElevatedButton(
onPressed: () {},
child: Text(''),
),
pages: listPagesViewModel,
done: const Text('Done',
style: TextStyle(fontWeight: FontWeight.w600)),
onDone: () {
// When done button is press
},
),
));
}
List fetchIntroApi(bool bool) {
var four = bool;
if (four) {
var data =
'[ {"name_Title": "title name1","description": "description1"}, {"name_Title": "title name2","description": "description2"}, {"name_Title": "title name3","description": "description3"}, {"name_Title": "title name4","description": "description4"} ]';
return json.decode(data);
} else {
var data =
'[ {"name_Title1": "title name","description": "description1"}, {"name_Title": "title name2","description": "description2"}, {"name_Title": "title name3","description": "description3"} ]';
return json.decode(data);
}
}
}
class IntroModel {
String? nameTitle;
String? description;
IntroModel({this.nameTitle, this.description});
IntroModel.fromJson(Map<String, dynamic> json) {
nameTitle = json['name_Title'];
description = json['description'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['name_Title'] = this.nameTitle;
data['description'] = this.description;
return data;
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | Maikzen |
| Solution 3 | |
| Solution 4 | |
| Solution 5 | lava |



