Error: LateInitializationError: Field '_splitScreenMode#963084504' has not been initialized - flutter

I got the error: "LateInitializationError: Field '_splitScreenMode#963084504' has not been initialized", in my splash screen when I did try to make my second page(next page after splash, named: pageOne) responsive by importing flutter_screen_util package. I added .w and .h for width and height measurments. I am new in flutter, so I can't identify this error.
My splash screen code:
import 'package:flutter/material.dart';
import 'package:my_wallet/screens/categories.dart';
import 'package:my_wallet/screens/page_1.dart';
class ScreenSplash extends StatefulWidget {
const ScreenSplash({Key? key}) : super(key: key);
#override
State<ScreenSplash> createState() => _ScreenSplashState();
}
class _ScreenSplashState extends State<ScreenSplash> {
#override
void initState() {
checkUser(context);
super.initState();
}
#override
Widget build(BuildContext context) {
return Container(
decoration: const BoxDecoration(
gradient: LinearGradient(
begin: Alignment.bottomCenter,
end: Alignment.topRight,
colors: [
Color.fromARGB(255, 0, 27, 48),
Color.fromARGB(255, 17, 149, 186),
],
),
),
child: Scaffold(
backgroundColor: Colors.transparent,
body: Center(
child: Image.asset(
'lib/assets/coin-spin3.gif',
height: 180,
),
),
),
);
}
}
Future<void> checkUser(context) async {
await Future.delayed(
const Duration(seconds: 4),
);
Navigator.of(context).pushReplacement(
MaterialPageRoute(
// builder: (ctx) => const PageOne(),
builder: (ctx) => const PageOne(),
),
);
}
My pageOne (screen after splash) code:
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:my_wallet/screens/page_2.dart';
class PageOne extends StatelessWidget {
const PageOne({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
child: Container(
height: double.infinity,
decoration: const BoxDecoration(
gradient: LinearGradient(
begin: Alignment.bottomLeft,
end: Alignment.topRight,
colors: [
Color.fromARGB(255, 0, 27, 48),
Color.fromARGB(255, 17, 149, 186),
],
),
),
child: Column(
mainAxisAlignment: MainAxisAlignment.end,
children: [
SizedBox(
height: 220.h,
child: const Image(
image: AssetImage(
'lib/assets/moneymanagement1.png',
),
),
),
const SizedBox(
height: 70,
),
const Text(
'Track Your Expense',
style: TextStyle(
color: Color.fromARGB(255, 255, 255, 255), fontSize: 25),
),
const SizedBox(
height: 100,
),
Row(
mainAxisAlignment: MainAxisAlignment.end,
children: [
const CircleAvatar(
backgroundColor: Color.fromARGB(255, 255, 255, 255),
radius: 4.5,
),
const SizedBox(
width: 10,
),
const CircleAvatar(
backgroundColor: Color.fromARGB(255, 104, 104, 104),
radius: 4.5,
),
const SizedBox(
width: 120,
),
IconButton(
onPressed: (() {
Navigator.of(context).pushReplacement(
MaterialPageRoute(
builder: (ctx1) => const PageTwo(),
),
);
}),
icon: const Icon(
Icons.arrow_forward_ios,
color: Colors.white,
),
),
const SizedBox(
width: 10,
),
],
),
const SizedBox(
height: 40,
),
],
),
),
),
);
}
}

Have you setup code in main.dart for "ScreenUtilInit"?
as mentioned in https://pub.dev/packages/flutter_screenutil readme section steps.

Related

how to display a dragged container inside another container when dragged into it in flutter

What I have done till now is make widget draggable and a drag target according to my requirements. What i want is when the user drops the draggable container into the drag target i want to show that dragged widget inside that big drag target container and able to score if that is the right option or not.
second problem i am having is that i am using Text to speech for the sound and it is not working i tried many things but still it isn't working. i have a play button and give it a letter like A,B,D,G etc. whenever user presses the button i want to play the sound of that button and drag the right container into the drag target. Thankyou for your help.
// ignore_for_file: prefer_const_constructors, file_names, non_constant_identifier_names
import 'package:dyslexia/pages/round_2/Q8sound.dart';
import 'package:dyslexia/utilities/nextButton.dart';
import 'package:flutter/material.dart';
import 'package:flutter_tts/flutter_tts.dart';
import '../../utilities/QuestionWidget.dart';
class Q1DAD extends StatefulWidget {
const Q1DAD({Key? key}) : super(key: key);
#override
State<Q1DAD> createState() => _Q1DADState();
}
class _Q1DADState extends State<Q1DAD> {
String question =
"Listen to this letter sound and then choose the letter whose sound you hear";
var changeButton = false;
var score = 0;
final FlutterTts flutterTts = FlutterTts();
speak(word) async {
await flutterTts.setLanguage("en-US");
await flutterTts.setPitch(1);
await flutterTts.setVolume(10.0);
await flutterTts.speak(word);
}
var word = "M";
bool isPlayingMsg = false;
bool draged = false;
bool showDRag = false;
#override
Widget build(BuildContext context) {
bool isPlayingMsg = false;
return Scaffold(
appBar: AppBar(
automaticallyImplyLeading: true,
backgroundColor: Colors.cyan,
title: Text("AD&DY"),
),
body: Padding(
padding: const EdgeInsets.all(14.0),
child: Column(children: [
QuestionWidget(question: question),
SizedBox(height: MediaQuery.of(context).size.height * 0.05),
Material(
elevation: 5,
color: Colors.cyan[50],
child: ListTile(
onTap: () async {
speak(word);
isPlayingMsg = true;
},
leading: Icon(isPlayingMsg ? Icons.stop : Icons.play_arrow),
title: Text(isPlayingMsg ? "Stop" : "Play",
textScaleFactor: 1.2,
style: TextStyle(
color: Colors.black,
)),
),
),
Divider(
thickness: 1.0,
),
SizedBox(height: MediaQuery.of(context).size.height * 0.05),
Column(
children: [
DragTarget1(),
SizedBox(height: MediaQuery.of(context).size.height * 0.07),
Wrap(
spacing: 10,
runSpacing: 5,
children: [
draggableWord("M", false, score),
draggableWord("N", false, score),
draggableWord("O", false, score),
draggableWord("R", false, score),
],
),
],
),
SizedBox(height: MediaQuery.of(context).size.height * 0.1),
nextButton(changeButton: changeButton, Location: Q8sound()),
]),
),
);
}
int acceptedData = 0;
Widget DragTarget1() {
return Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
DragTarget<int>(
builder: (
BuildContext context,
List<dynamic> accepted,
List<dynamic> rejected,
) {
return Material(
elevation: 10,
borderRadius: BorderRadius.circular(80),
child: Container(
height: 100.0,
width: 250.0,
color: Color.fromARGB(255, 78, 166, 178),
child: draged
? Container(
height: 50,
width: 70,
color: Colors.lightGreenAccent,
child: Align(
child: Text(word),
),
)
: Center(
child: Text('Drag target here'),
),
),
);
},
onAccept: (int data) {
setState(() {
draged = true;
showDRag = true;
});
},
),
],
);
}
Widget draggableWord(word, option, score) {
return Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
showDRag
? SizedBox()
: Draggable<int>(
// Data is the value this Draggable stores.
data: 10,
feedback: Container(
color: Colors.yellow[100],
height: 50,
width: 70,
child: Center(child: Text(word)),
),
childWhenDragging: Material(
elevation: 5,
borderRadius: BorderRadius.circular(10),
child: Container(
height: 50.0,
width: 70.0,
color: Colors.grey,
child: Center(
child: Text(""),
),
),
),
child: Material(
elevation: 5,
borderRadius: BorderRadius.circular(10),
child: Container(
height: 50,
width: 70,
color: Colors.lightGreenAccent,
child: Center(
child: Text(word),
),
),
),
)
],
);
}
}
here is my question widget
import 'package:flutter/material.dart';
class QuestionWidget extends StatelessWidget {
const QuestionWidget({
Key? key,
required this.question,
}) : super(key: key);
final String question;
#override
Widget build(BuildContext context) {
return Material(
elevation: 12,
color: Color.fromARGB(255, 125, 200, 210),
shadowColor: Colors.grey,
borderRadius: BorderRadius.circular(8),
child: Container(
height: 80,
width: 280,
alignment: Alignment.center,
child: Text(question,
textScaleFactor: 1.2,
style: TextStyle(
color: Colors.black,
))),
);
}
}

Why does a Card widget not wrap all the items inside a Wrap widget?

Initially, I created a Column to show all of the inner elements like a Container Widget with an image, an icon, texts. The Column widget squashes the interior items, so I changed the Column to a Wrap widget; it solved the problem.
However, when I tried to set onPressed/onTap logic only half of the items were clickable in the vertical axis. As an experiment, I wrapped the Wrap widget with a Card widget to see what area is covered. It turns out that the Card widget was covering exactly only that vertical part that was clickable.
To visualize this I am providing this screenshot of a UI
I tried:
Setting up a container with a fixed size to change the Card widget geometry
Setting up a sized box and following steps like in 1.
Setting expandables to interior elements
These are some of the solutions I used, however, none of them worked.
So what are suggestions as to what may cause this problem? And what can be a solution?
Code is provided here:
Widget _buildWomanProductContainer({
required double width,
required double height,
required String imagePath,
required String clothProductName,
required String clothType,
required String clothPrice,}) {
return InkWell(
onTap: () => Navigator.of(context).push(MaterialPageRoute(
builder: (context) => WomenProductScreen(),
)),
child: Card(
child: Wrap(
alignment: WrapAlignment.center,
children: [
SizedBox(height: 0),
Stack(
children: [
Container(
width: width,
height: height,
child: Image.asset(
imagePath,
width: width,
height: height,
fit: BoxFit.cover,
color: Color.fromRGBO(0, 0, 0, 0.4),
colorBlendMode: BlendMode.darken,
),
),
Positioned(
right: 5,
bottom: 5,
child: IconButton(
alignment: Alignment.center,
icon: Icon(Icons.favorite,
size: 30, color: Color.fromRGBO(255, 255, 255, 1)),
onPressed: () => {
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
content: const Text('Сохранено'),
duration: const Duration(seconds: 2),
))
},
),
),
],
),
Column(
children: [
const SizedBox(height: 8),
Text(clothProductName,
style: TextStyle(
color: Color.fromRGBO(58, 67, 59, 1),
fontFamily: 'Merriweather-Regular',
)),
Text(clothType,
style: TextStyle(
color: Color.fromRGBO(58, 67, 59, 0.5),
fontFamily: 'Merriweather-Regular',
fontSize: 12)),
OutlinedButton(
child: Text(clothPrice,
style: TextStyle(
color: Color.fromRGBO(58, 67, 59, 1),
fontFamily: 'SolomonSans-SemiBold')),
onPressed: () =>
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
content: const Text('Сохранено'),
duration: const Duration(seconds: 2),
)),
style: OutlinedButton.styleFrom(
side: BorderSide(
width: 1.0, color: Color.fromRGBO(58, 67, 59, 1)),
),
),
],
)
],
),
),
);}
UPDATE
Can you try this
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
// This widget is the root of your application.
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key}) : super(key: key);
#override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
#override
Widget build(BuildContext context) {
return Scaffold(
body: Container(
width: MediaQuery.of(context).size.width,
height: MediaQuery.of(context).size.height,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Expanded(
child: Stack(
children: [
Card(
color: Colors.transparent,
margin: const EdgeInsets.symmetric(
horizontal: 20, vertical: 40),
elevation: 5,
child: ClipRRect(
borderRadius: BorderRadius.circular(12.0),
child: Image.asset(
'assets/image.jpeg',
fit: BoxFit.fill,
),
),
),
Positioned(
right: 15,
bottom: 35,
child: IconButton(
alignment: Alignment.center,
icon: Icon(Icons.favorite,
size: 30, color: Color.fromRGBO(255, 255, 255, 1)),
onPressed: () => {
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
content: const Text('Сохранено'),
duration: const Duration(seconds: 2),
))
},
),
),
],
),
),
Text('Product name',
style: TextStyle(
color: Color.fromRGBO(58, 67, 59, 1),
fontFamily: 'Merriweather-Regular',
)),
Text('type',
style: TextStyle(
color: Color.fromRGBO(58, 67, 59, 0.5),
fontFamily: 'Merriweather-Regular',
fontSize: 12)),
OutlinedButton(
child: Text('10000',
style: TextStyle(
color: Color.fromRGBO(58, 67, 59, 1),
fontFamily: 'SolomonSans-SemiBold')),
onPressed: () =>
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
content: const Text('Сохранено'),
duration: const Duration(seconds: 2),
)),
style: OutlinedButton.styleFrom(
side: BorderSide(
width: 1.0, color: Color.fromRGBO(58, 67, 59, 1)),
),
),
],
),
),
);
}
}
I have tested this code on the smallest possible device and there is no pixel overflows. The thing is you have wrap your Stack widget inside the Expanded and it will take the maximum available space. No need to use Wrap widget in your case.
Screenshot:

Flutter: i want to save the image from camera or gallery to next page after cropping it here is my code

I want to save the image to the next page but after cropping the image it opens the next page but the image is not saved, here is my code it opens the camera take the pic or open gallery but after cropping it only take to the next page what should I do to save the cropped image to next page.I tried a method but it returns the image on UIpage.
import 'package:flutter/material.dart';
import 'package:fyp/ui.dart';
import 'package:fyp/Analyze.dart';
void main() {
runApp(MaterialApp(home: MyApp()));
}
class MyApp extends StatefulWidget {
#override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
#override
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
child: Ui(),
),
);
}
}
import 'package:flutter/material.dart';
import 'package:fyp/Analyze.dart';
import 'dart:io';
import 'package:image_picker/image_picker.dart';
import 'package:image_cropper/image_cropper.dart';
class Ui extends StatefulWidget {
#override
_UiState createState() => _UiState();
}
class _UiState extends State<Ui> {
String path;
File _selectedFile;
bool _inProcess1 = false;
#override
void initState() {
super.initState();
}
getImage(ImageSource source) async {
this.setState((){
_inProcess1 = true;
});
File image = await ImagePicker.pickImage(source: source);
if(image != null){
File cropped = await ImageCropper.cropImage(
sourcePath: image.path,
aspectRatio: CropAspectRatio(
ratioX: 1, ratioY: 1),
compressQuality: 100,
maxWidth: 700,
maxHeight: 700,
compressFormat: ImageCompressFormat.jpg,
androidUiSettings: AndroidUiSettings(
toolbarColor: Colors.deepOrange,
toolbarTitle: "RPS Cropper",
statusBarColor: Colors.deepOrange.shade900,
backgroundColor: Colors.white,
)
);
this.setState((){
_selectedFile = cropped;
return Analyze();
_inProcess1 = false;
});
} else {
this.setState((){
_inProcess1 = false;
});
}
}
#override
Widget build(BuildContext context) {
return SafeArea(
child: Scaffold(
appBar: AppBar(
elevation: 0,
toolbarHeight: 100,
leading:Padding(
padding: const EdgeInsets.fromLTRB(15, 20, 0, 0),
child: Column(
children: [
IconButton(icon: Icon(Icons.info_outlined, color: Colors.white,
size: 30,),
onPressed: ()=>null,),
Text(" Help",style: TextStyle(
color: Colors.white,
fontSize: 16,
), ),
],
),
),
actions: [
Padding(
padding: const EdgeInsets.fromLTRB(0, 20, 20, 0),
child: Column(
children: [
IconButton(icon: Icon(Icons.upload_outlined, color: Colors.white,
size: 30,),
onPressed: ()
{ getImage(ImageSource.gallery); }
),
Text("Upload",style: TextStyle(
color: Colors.white,
fontSize: 16,
), ),
],
),
)
],
),
backgroundColor: Colors.blue,
body: Column(
mainAxisAlignment: MainAxisAlignment.end,
children: [
Padding(
padding: const EdgeInsets.fromLTRB(0, 90, 0, 60),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Stack(
alignment: Alignment.center,
children: [
Opacity(
opacity:0.6,
child: CircleAvatar( // use to create circle
backgroundColor: Colors.white,
radius: 80,
),
),
Opacity(
opacity:0.5,
child: CircleAvatar(
backgroundColor: Colors.white,
radius: 120,
),
),
Opacity(
opacity:0.3,
child: CircleAvatar(
backgroundColor: Colors.white,
radius: 150,
),
),
Opacity(
opacity:0.7,
child: CircleAvatar(
backgroundColor: Colors.blue,
radius: 180,
),
),
IconButton(
iconSize: 80,
icon: Icon(Icons.camera_alt, color: Colors.white,),
onPressed: () {
getImage(ImageSource.camera);
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => Analyze(
),
),
);
},
),
],
),
],
),
),
Text("Tap to scan", style: TextStyle(
color: Colors.white,
fontSize: 20,
),),
SizedBox(
height: 20,
width: 150,
child: Divider(
color: Colors.white,
thickness: 3,
),
),
],
),
),
);
}
}
import 'package:flutter/material.dart';
import 'dart:io';
class Analyze extends StatefulWidget {
#override
_AnalyzeState createState() => _AnalyzeState();
}
class _AnalyzeState extends State<Analyze> {
File _selectedFile;
bool _inProcess = false;
#override
Widget build(BuildContext context) {
Widget getImageWidget(File selectedFile) {
if (_selectedFile != null) {
return Image.file(
_selectedFile,
width: 250,
height: 250,
fit: BoxFit.cover,
);
} else {
return Image.asset(
"assets/placeholder.jpg",
width: 250,
height: 250,
fit: BoxFit.cover,
);
}
}
(_inProcess)
? Container(
color: Colors.white,
height: MediaQuery.of(context).size.height * 0.95,
child: Center(
child: CircularProgressIndicator(),
),
)
: Center();
return SafeArea(
child: Scaffold(
body: Column(
children: [
getImageWidget(_selectedFile),
],
),
),
);
}

In the bottom of my app there is a white space Flutter

import 'package:flutter/material.dart';
import 'package:carousel_slider/carousel_slider.dart';
import 'package:url_launcher/url_launcher.dart';
class HomeScreen extends StatefulWidget {
#override
_HomeScreenState createState() => _HomeScreenState();
}
class _HomeScreenState extends State<HomeScreen> {
int _currentIndex = 0;
List cardList = [Item1(), Item2(), Item3(), Item4()];
List<T> map<T>(List list, Function handler) {
List<T> result = [];
for (var i = 0; i < list.length; i++) {
result.add(handler(i, list[i]));
}
return result;
}
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Card Carousel App',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: SafeArea(
child: Scaffold(
resizeToAvoidBottomPadding: false,
appBar: AppBar(
title: Row(
mainAxisAlignment: MainAxisAlignment.start,
children: [
Image(
image: AssetImage('assets/liberta.jpeg'),
width: 120,
),
Text(
'Libertà&Ricchezza',
style: TextStyle(color: Colors.black),
)
],
),
backgroundColor: Colors.white,
),
body: SingleChildScrollView(
child: Container(
decoration: BoxDecoration(
color: Colors.white,
image: DecorationImage(
image: AssetImage('assets/background.jpeg'),
fit: BoxFit.cover,
)
),
child: Column(
children: <Widget>[
SizedBox(
height: 10,
),
CarouselSlider(
options: CarouselOptions(
height: 240.0,
autoPlay: true,
autoPlayInterval: Duration(seconds: 3),
autoPlayAnimationDuration: Duration(milliseconds: 800),
autoPlayCurve: Curves.fastOutSlowIn,
pauseAutoPlayOnTouch: true,
aspectRatio: 2.0,
onPageChanged: (index, reason) {
setState(() {
_currentIndex = index;
});
},
),
items: cardList.map((card) {
return Builder(builder: (BuildContext context) {
return Container(
height: MediaQuery.of(context).size.height * 0.30,
width: MediaQuery.of(context).size.width,
child: Card(
color: Colors.blueAccent,
child: card,
),
);
});
}).toList(),
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: map<Widget>(cardList, (index, url) {
return Container(
width: 10.0,
height: 10.0,
margin:
EdgeInsets.symmetric(vertical: 10.0, horizontal: 2.0),
decoration: BoxDecoration(
shape: BoxShape.circle,
color: _currentIndex == index
? Colors.white
: Colors.grey,
),
);
}),
),
SizedBox(
height: 20,
),
Row(
children: [
Expanded(
child: Container(
height: 85,
margin: EdgeInsets.all(10),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(20),
color: Color(0xff024f9c),
),
child: Center(
child: Text(
"L&R PER LA PERSONA",
style: TextStyle(
color: Colors.white,
fontSize:20,
fontWeight: FontWeight.bold
),
textAlign: TextAlign.center,
),
),
)),
],
),
Row(
children: [
Expanded(
child: GestureDetector(
onTap: () {
setState(() {
Navigator.pushNamed(context, '/persona');
});
},
child: Container(
height: 85,
margin: EdgeInsets.all(10),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(20),
color: Color(0xff9c0202),
),
child: Center(
child: Text(
"L&R PER L'AZIENDA",
style: TextStyle(
color: Colors.white,
fontSize:20,
fontWeight: FontWeight.bold
),
textAlign: TextAlign.center,
),
),
)),
),
],
)
],
),
),
),
),
));
}
}
class Item1 extends StatelessWidget {
const Item1({Key key}) : super(key: key);
#override
Widget build(BuildContext context) {
return Container(
child: InkWell(
child: Image(
image: AssetImage('assets/mandela.jpeg'),
fit: BoxFit.cover,
),
onTap: () => launch('https://www.instagram.com/libertaericchezza/'),
),
decoration: BoxDecoration(
gradient: LinearGradient(
begin: Alignment.topLeft,
end: Alignment.bottomRight,
stops: [
0.3,
1
],
colors: [
Color(0xffff4000),
Color(0xffffcc66),
]),
),
);
}
}
class Item2 extends StatelessWidget {
const Item2({Key key}) : super(key: key);
#override
Widget build(BuildContext context) {
return Container(
child:InkWell(
child: Image(
image: AssetImage('assets/astronauta.jpeg'),
fit: BoxFit.cover,
),
onTap: () => launch('https://www.instagram.com/libertaericchezza/'),
),
decoration: BoxDecoration(
gradient: LinearGradient(
begin: Alignment.topLeft,
end: Alignment.bottomRight,
stops: [
0.3,
1
],
colors: [
Color(0xffff4000),
Color(0xffffcc66),
]),
),
);
}
}
class Item3 extends StatelessWidget {
const Item3({Key key}) : super(key: key);
#override
Widget build(BuildContext context) {
return Container(
child:InkWell(
child: Image(
image: AssetImage('assets/farfalle.jpeg'),
fit: BoxFit.cover,
),
onTap: () => launch('https://www.instagram.com/libertaericchezza/'),
),
decoration: BoxDecoration(
gradient: LinearGradient(
begin: Alignment.topLeft,
end: Alignment.bottomRight,
stops: [
0.3,
1
],
colors: [
Color(0xffff4000),
Color(0xffffcc66),
]),
),
);
}
}
class Item4 extends StatelessWidget {
const Item4({Key key}) : super(key: key);
#override
Widget build(BuildContext context) {
return Container(
child: InkWell(
child: Image(
image: AssetImage('assets/car.jpeg'),
fit: BoxFit.cover,
),
onTap: () => launch('https://www.instagram.com/libertaericchezza/'),
),
decoration: BoxDecoration(
gradient: LinearGradient(
begin: Alignment.topLeft,
end: Alignment.bottomRight,
stops: [
0.3,
1
],
colors: [
Color(0xffff4000),
Color(0xffffcc66),
]),
),
);
}
}
I have insert a background image but there is a white space on the bottom and i don-t understand why, i try the resizedToAVoidBottomPadding but doesn-t work,I have insert a background image but there is a white space on the bottom and i don-t understand why, i try the resizedToAVoidBottomPadding but doesn-t workI have insert a background image but there is a white space on the bottom and i don-t understand why, i try the resizedToAVoidBottomPadding but doesn-t work
Have you try remove bottom safearea, safearea will occupy both top and bottom for notch and control area:
SafeArea(
bottom: false,

How to download network image in Flutter

I am building a simple wallpaper app with Api by watching tutorials on YouTube. But in the last stage, I can't save the image from the URL in the android simulator. Can you guys help me with how can I do this work?
import 'package:flutter/material.dart';
class ImageView extends StatefulWidget {
final String imageUrl;
ImageView({#required this.imageUrl});
#override
_ImageViewState createState() => _ImageViewState();}
class _ImageViewState extends State<ImageView> {
#override
Widget build(BuildContext context) {
return Scaffold(
body: Stack(
children: [
Hero(
tag: widget.imageUrl,
child: Container(
height: MediaQuery.of(context).size.height,
width: MediaQuery.of(context).size.width,
child: Image.network(
widget.imageUrl,
fit: BoxFit.cover,
),
),
),
//the buttons is here
Container(
alignment: Alignment.bottomCenter,
height: MediaQuery.of(context).size.height,
width: MediaQuery.of(context).size.width,
child: Column(
mainAxisAlignment: MainAxisAlignment.end,
children: [
//wallpaper download button
GestureDetector(
onTap: () {},
child: Stack(
children: [
Container(
height: 55,
width: MediaQuery.of(context).size.width / 2,
decoration: BoxDecoration(
color: Color(0xff1c1b1b).withOpacity(0.8),
borderRadius: BorderRadius.circular(30),
)),
Container(
padding:
EdgeInsets.symmetric(horizontal: 8, vertical: 8),
width: MediaQuery.of(context).size.width / 2,
height: 55,
decoration: BoxDecoration(
border: Border.all(
color: Colors.white,
width: 1,
),
borderRadius: BorderRadius.circular(30),
gradient: LinearGradient(colors: [
Colors.blueGrey[100],
Colors.grey[900],
])),
child: Column(
children: [
Text(
'Set Wallpaper',
style: TextStyle(
fontSize: 16,
color: Colors.white,
),
),
SizedBox(height: 5),
Text(
'Image wil be save in gallery',
style: TextStyle(
fontSize: 10,
color: Colors.white,
),
)
],
)),
],
),
),
SizedBox(height: 16),
//for closeing the showing wallpaper
GestureDetector(
onTap: () => Navigator.pop(context),
child: Text(
'Cancel',
style: TextStyle(
color: Colors.amber,
),
),
),
SizedBox(height: 50)
],
),
)
],
),
);
}
}
You can copy paste run full code below
Step 1: To save image to gallery, add <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> to AndroidManifest.xml
Step 2: Request permission with permission_handler
Step 3: Download file with flutter_cache_manager's DefaultCacheManager().getSingleFile(widget.imageUrl)
Step 4: Save to gallery with image_gallery_saver's ImageGallerySaver.saveFile(file.path)
Step 5: Directly set wall paper with wallpaper_manager's WallpaperManager.setWallpaperFromFile(file.path, WallpaperManager.HOME_SCREEN)
code snippet
_requestPermission() async {
Map<Permission, PermissionStatus> statuses = await [
Permission.storage,
].request();
final info = statuses[Permission.storage].toString();
print(info);
}
...
GestureDetector(
onTap: () async {
File file = await DefaultCacheManager()
.getSingleFile(widget.imageUrl);
print(file.path);
final result = await ImageGallerySaver.saveFile(file.path);
print("gallerysaver result $result");
String resultWall =
await WallpaperManager.setWallpaperFromFile(
file.path, WallpaperManager.HOME_SCREEN);
print("resultWall $resultWall");
},
working demo
full code
import 'dart:io';
import 'package:permission_handler/permission_handler.dart';
import 'package:flutter/material.dart';
import 'package:flutter_cache_manager/flutter_cache_manager.dart';
import 'package:wallpaper_manager/wallpaper_manager.dart';
import 'package:image_gallery_saver/image_gallery_saver.dart';
class ImageView extends StatefulWidget {
final String imageUrl;
ImageView({#required this.imageUrl});
#override
_ImageViewState createState() => _ImageViewState();
}
class _ImageViewState extends State<ImageView> {
#override
void initState() {
super.initState();
_requestPermission();
}
_requestPermission() async {
Map<Permission, PermissionStatus> statuses = await [
Permission.storage,
].request();
final info = statuses[Permission.storage].toString();
print(info);
}
#override
Widget build(BuildContext context) {
return Scaffold(
body: Stack(
children: [
Hero(
tag: widget.imageUrl,
child: Container(
height: MediaQuery.of(context).size.height,
width: MediaQuery.of(context).size.width,
child: Image.network(
widget.imageUrl,
fit: BoxFit.cover,
),
),
),
//the buttons is here
Container(
alignment: Alignment.bottomCenter,
height: MediaQuery.of(context).size.height,
width: MediaQuery.of(context).size.width,
child: Column(
mainAxisAlignment: MainAxisAlignment.end,
children: [
//wallpaper download button
GestureDetector(
onTap: () async {
File file = await DefaultCacheManager()
.getSingleFile(widget.imageUrl);
print(file.path);
final result = await ImageGallerySaver.saveFile(file.path);
print("gallerysaver result $result");
String resultWall =
await WallpaperManager.setWallpaperFromFile(
file.path, WallpaperManager.HOME_SCREEN);
print("resultWall $resultWall");
},
child: Stack(
children: [
Container(
height: 55,
width: MediaQuery.of(context).size.width / 2,
decoration: BoxDecoration(
color: Color(0xff1c1b1b).withOpacity(0.8),
borderRadius: BorderRadius.circular(30),
)),
Container(
padding:
EdgeInsets.symmetric(horizontal: 8, vertical: 8),
width: MediaQuery.of(context).size.width / 2,
height: 55,
decoration: BoxDecoration(
border: Border.all(
color: Colors.white,
width: 1,
),
borderRadius: BorderRadius.circular(30),
gradient: LinearGradient(colors: [
Colors.blueGrey[100],
Colors.grey[900],
])),
child: Column(
children: [
Text(
'Set Wallpaper',
style: TextStyle(
fontSize: 16,
color: Colors.white,
),
),
SizedBox(height: 5),
Text(
'Image wil be save in gallery',
style: TextStyle(
fontSize: 10,
color: Colors.white,
),
)
],
)),
],
),
),
SizedBox(height: 16),
//for closeing the showing wallpaper
GestureDetector(
onTap: () => Navigator.pop(context),
child: Text(
'Cancel',
style: TextStyle(
color: Colors.amber,
),
),
),
SizedBox(height: 50)
],
),
)
],
),
);
}
}
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
#override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
ElevatedButton(
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => ImageView(
imageUrl: "https://picsum.photos/250?image=9")),
);
},
child: Text('To set wall paper')),
])));
}
}