How can I Get capture image in the next page using Flutter? - android-studio

I made a Login screen,
want to Launch camera(font) on click of Activate Button , then how to show the capture image in next page?
I am done with the camera open part , onPressed of Activate Button and also done routing to the next page
----> that's mean in my case Activate button , doing 2 things--> (1) its captured image and (2) going to the next page
Now how can I Show the Capture image in next page which I routing with loginPage Activate button
class LoginFormState extends State<LoginForm> {
File imagefile;
_openCamera(BuildContext context)async{
var picture = await ImagePicker.pickImage(source: ImageSource.camera);
setState(() {
imagefile = picture;
});
}
showImage(){
if(imagefile == null){
return Text("No Image selected here");
}else {
Image.file(imagefile,width:400,height:400);
}
}
var textLength = 6;
var mobileLength = 10;
#override
Widget build(context) {
return Container(
margin: EdgeInsets.only(
top: 0.0,
right: 43.0,
left: 43),
child: Column(
children: [
userField(),
Container(margin: EdgeInsets.only(top: 40.0)),
numberField(),
Container(margin: EdgeInsets.only(top: 60.0, )),
activateButton(),
Container(margin: EdgeInsets.only(top: 60.0, )),
showImage(),
],
),
);
}
Widget activateButton() {
return RaisedButton(
color: Color(0xFFF4D3AE),
child: Text('Activate',
style: TextStyle(
fontFamily: 'Californian FB',
color: Colors.black, //Color(0xFF425c5a),
fontWeight: FontWeight.normal, //bold
letterSpacing: 2.0,
fontSize: 17.0,
)),
padding: EdgeInsets.symmetric(
horizontal: 75.0,
vertical:
15.0), //Activate button box will take more space horizontally & vertically.
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.only(
bottomLeft: Radius.circular(20),
bottomRight: Radius.circular(20),
topRight: Radius.circular(20),
topLeft: Radius.circular(20),
),
),
onPressed: () {
_openCamera(context);
Navigator.of(context).pushNamed("/CameraAccess");
},
);
}
}
----------------------- This is my second page code:-----------------------------------
import 'package:flutter/material.dart';
class CameraAccess extends StatefulWidget{
#override
createState() {
return CameraAccessState();
}
}
class CameraAccessState extends State<CameraAccess>{
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Color(0xFF425c5a),
body: ListView(
children: <Widget>[
Center(
child: Container(child:
Text('Your Image here',
style: TextStyle(
fontFamily: 'Californian FB',
color: Colors.black, //Color(0xFF425c5a),
fontWeight: FontWeight.normal, //bold
letterSpacing: 2.0,
fontSize: 27.0,
)
),
),
),
]
)
);
}
}

Related

How to make second container?

I recently started programming in flutter. I want to add second container in my app. When i duplicate the container code in my class, i get an error dead code in this part of the text. Could you please help me with adding the second container?
NewsBoxFavourit(this._num);
#override
State<StatefulWidget> createState() => MyFormState();
}
class MyFormState extends State {
final _formKey = GlobalKey<FormState>();
#override
Widget build(BuildContext context) {
return Expanded(
child: Column(
children: <Widget>[
Text('Выполнено кругов'),
TextFormField(validator: (value){if (value.isEmpty) return 'Введите количество кругов';}),
],
),
);
}
}
class NewsBox extends StatelessWidget {
final String _title;
final String _text;
String _imageurl;
int _num;
NewsBox(this._title, this._text, {String imageurl, int num = 0,}) {
_imageurl = imageurl;
_num = num;
}
#override
Widget build(BuildContext context) {
if (_imageurl != null && _imageurl != '') return new Container(
color: Colors.black12,
height: 100.0,
child: Row(children: [
Image.network(_imageurl, width: 100.0, height: 100.0, fit: BoxFit.cover,),
Expanded(child: Container(padding: EdgeInsets.all(5.0), child: Column(children: [
Text('Дата'),
TextFormField(validator: (value){if (value.isEmpty) return 'Введите количество кругов';}),
Expanded(child: Text(_text, softWrap: true, textAlign: TextAlign.justify,))
]
))
), NewsBoxFavourit(_num,)
])
);
return Container(
color: Colors.black12,
height: 100.0,
child: Row(children: [
Expanded(child: Container(padding: EdgeInsets.all(5.0), child: Column(children: [
Text(_title, style: TextStyle(fontSize: 20.0), overflow: TextOverflow.ellipsis),
Expanded(child: Text(_text, softWrap: true, textAlign: TextAlign.justify,))
]
))
), NewsBoxFavourit(_num,)
])
);
#override
Widget build(BuildContext context) {
if (_imageurl != null && _imageurl != '') return new Container(
color: Colors.black12,
height: 100.0,
child: Row(children: [
Image.network(_imageurl, width: 100.0, height: 100.0, fit: BoxFit.cover,),
Expanded(child: Container(padding: EdgeInsets.all(5.0), child: Column(children: [
Text('Дата'),
TextFormField(validator: (value){if (value.isEmpty) return 'Введите количество кругов';}),
Expanded(child: Text(_text, softWrap: true, textAlign: TextAlign.justify,))
]
))
), NewsBoxFavourit(_num,)
])
);
return Container(color:Colors.black12,
height: 100.0,
child: Row(children: [
Expanded(child: Container(padding: EdgeInsets.all(5.0), child: Column(children: [
Text(_title, style: TextStyle(fontSize: 20.0), overflow: TextOverflow.ellipsis),
Expanded(child: Text(_text, softWrap: true, textAlign: TextAlign.justify,))
]
))
), NewsBoxFavourit(_num,)
])
);
}
}
Please, explain why it does not execute the code? How to make second Conteiner? What i need to do with dead code?
Flutter does not work like what your code is showing. Your NewsBox custom widget can only have 1 build method. If you want 2 containers in a single widget then try using Column, Row, Stack and many other widgets. These can be found under the Multi-child layout widgets in the flutter documentation. I'd assume you'd want the Stack widget though I do not know what design you'd like to achieve.

Flutter "audio" directory for audio files not working

I have a simple flutter app that plays a bark noise when tapped on the Circle avatar of the dog. If I keep my audio files in pubspec.yaml file in the following way then it works
flutter:
uses-material-design: true
assets:
- assets/
- images/
The avatar is wrapped with a FlatButton Widget and the onPressed method is like this:
onPressed: (){
audioCache.play('bark.mp3');
},
BUT, the same does not happen when I keep the audio file in the "audio" directory, like this:
flutter:
uses-material-design: true
assets:
- audios/
- images/
And the onPressed method:
onPressed: (){
audioCache.play('audios/bark.mp3');
},
When I keep the audio file using the second method, it gives me the following error:
Cannot find asset: "assets/audios/bark.mp3"
What can be the problem?
Here is the following code in main.dart
import 'package:flutter/material.dart';
import 'package:audioplayers/audio_cache.dart';
import 'package:audioplayers/audioplayers.dart';
// The main function is the starting point.
void main() {
runApp(MyApp());
}
class MyApp extends StatefulWidget {
#override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
AudioCache audioCache;
#override
void initState() {
// TODO: implement initState
super.initState();
audioCache = AudioCache();
}
#override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
backgroundColor: Colors.indigo[300],
body: SafeArea(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
FlatButton(
onPressed: (){
audioCache.play('bark.mp3');
},
child: CircleAvatar(
radius: 50,
backgroundImage: AssetImage("images/doggo.jpg"),
),
),
Text(
"Roger Pupperino",
style: TextStyle(
fontSize: 25,
fontWeight: FontWeight.bold,
color: Colors.white,
fontFamily: 'Pacifico',
),
),
Text(
"Goodest Boi Ever".toUpperCase(),
style: TextStyle(
fontFamily: 'SourceSansPro',
color: Colors.indigo,
letterSpacing: 2.5,
),
),
SizedBox(
height: 20,
width: 200,
child: Divider(
color: Colors.indigo[300],
),
),
Card(
margin: EdgeInsets.symmetric(vertical: 10,horizontal: 25),
color: Colors.white,
child: ListTile(
leading: Icon(Icons.phone, color: Colors.indigo),
title: Text(
"+880 13033 84426",
style: TextStyle(
color: Colors.indigo,
fontFamily: 'Source Sans Pro',
fontSize: 20,
),
),
),
),
Card(
margin: EdgeInsets.symmetric(vertical: 10,horizontal: 25),
color: Colors.white,
child: ListTile(
leading: Icon(Icons.mail, color: Colors.indigo),
title: Text(
"roger.goodboi#pup.com",
style: TextStyle(
color: Colors.indigo,
fontFamily: 'Source Sans Pro',
fontSize: 20,
),
),
),
),
],
),
),
),
);
}
}
Below I have the directory tree image. Notice that the audios and images folder are in the root directory. Images work fine but Audios are not working.
AudioPlayers is hardcoded to the "assets\" folder, so you can't put your audio files into a different root folder.
https://github.com/SSolheim2000/AudioPlayer_Basic/
I think you missing something try below code on OnPressed method
onPressed: (){
audioCache.play('assets/audios/bark.mp3');
},

How do I use Flutter expanded widget to fill entire screen?

I have been experimenting with Flutter trying to recreate an app that I wrote for Android 5 years ago. But I can't seem to get the layout to work very well.
I have tried a number of different widgets, but each time I try something, it seems like I am taking 2 steps forward and 3 steps back.
The app is going to be vertical position only.
Simple banner image at the top.
A trivia question.
Radio button group with 4 answers.
Then 2 buttons - 'Check Answer' and 'Skip Question'
Bottom bar
I am trying to make sure that the radio buttons and the buttons don't bounce around when the question changes.
This is what I have
This is what I would like
And this is the code that I have been fighting with.
Any help or tips would be greatly appreciated.
class _MyHomePageState extends State<MyHomePage> {
final TriviaBloc _triviaBloc = TriviaBloc();
TriviaQuestion _initTriviaQuestion = new TriviaQuestion('', '', '', '', '', 1, 1);
String _selectedAnswer = "";
TextStyle _answerStyle = new TextStyle(textBaseline: TextBaseline.alphabetic, fontStyle: FontStyle.normal, fontFamily: 'QarmicsansAbridged', fontSize: 16);
#override
void dispose() {
_triviaBloc.dispose();
super.dispose();
}
#override
void initState() {
super.initState();
}
#override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.orangeAccent,
body: SafeArea(
minimum: const EdgeInsets.all(16.0),
child:
Column(children: [
ImageBanner("assets/header_2.jpg"),
Expanded(
child:
StreamBuilder<TriviaQuestion>(
initialData: _triviaBloc.getInitialTriviaQuestion(),
//_initTriviaQuestion,
stream: _triviaBloc.triviaQuestionStream,
//initialData: _triviaBloc.getInitialTriviaQuestion(),
//builder: (BuildContext context, AsyncSnapshot<TriviaQuestion> snapshot)
builder: (BuildContext context, snapshot) {
//_triviaBloc.getTriviaQuestion(snapshot, context);
if (snapshot.data != null) {
return Column(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisSize: MainAxisSize.min,
children: <Widget>[
SizedBox(height: 15),
Padding(
padding: EdgeInsets.all(2.0),
child: TriviaAutoSizeText(context, snapshot.data.Question),
),
RadioButtonGroup(
labels: <String>[
snapshot.data.IncorrectAnswer1,
snapshot.data.IncorrectAnswer2,
snapshot.data.CorrectAnswer,
snapshot.data.IncorrectAnswer3
],
labelStyle: _answerStyle,
onSelected: (String selected) => _selectedAnswer = selected),
//),
SizedBox(height: 10),
new InkWell(
onTap: () => {_triviaBloc.checkAnswer(QuestionAnswered(_selectedAnswer))},
child: new Container(
width: MediaQuery.of(context).size.width * 0.5,
height: 50.0,
decoration: new BoxDecoration(
color: _butttonInteriorColour,
//Colors.blueAccent,
border:
new Border.all(color: Colors.white, width: 1.0),
borderRadius: new BorderRadius.circular(20.0),
),
child: new Center(
child: new Text(
'Check Answer',
style: new TextStyle(
fontSize: 18.0, color: Colors.white),
),
),
),
),
SizedBox(height: 10),
new InkWell(
onTap: () =>
{_triviaBloc.getTriviaQuestion(snapshot, context)},
child: new Container(
width: MediaQuery.of(context).size.width * 0.5,
height: 50.0,
decoration: new BoxDecoration(
color: _butttonInteriorColour,
//Colors.blueAccent,
border:
new Border.all(color: Colors.white, width: 1.0),
borderRadius: new BorderRadius.circular(20.0),
),
child: new Center(
child: new Text(
'Next Question',
style: new TextStyle(
fontSize: 18.0, color: Colors.white),
),
),
),
),
],
);
} else {
return Center(child: CircularProgressIndicator());
}
;
}),
)]),
),
bottomNavigationBar: buildBottomNav(context),
);
}
}
So you want the 4 button group and the 2 buttons at the bottom without changing position ?
Maybe a Spacer will help with that
if (snapshot.data != null) {
return Column(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisSize: MainAxisSize.max, //replace this to fill everything
children: <Widget>[
SizedBox(height: 15),
Padding(
padding: EdgeInsets.all(2.0),
child: TriviaAutoSizeText(context, snapshot.data.Question),
),
Spacer(), //add the spacer here
RadioButtonGroup(
labels: <String>[

Flutter: How to set the height of a SliverAppBar?

I am trying to use SliverAppBar. The layout was fine on the phone devices. However, when I use the iPad to test the layout, it seems that the height of the AppBar does not change, and it could not show the title text properly .
When I collasped the AppBar:
I tried too add the bottom attribute to the SliverAppBar, but it doesn't works too.
bottom: PreferredSize(
preferredSize: Size.fromHeight(60.0),
child: Text(''),
),
and the result looks like this:
the title text is still restricted with an invisible height.
my code:
new SliverAppBar(
expandedHeight: Adapt.px(220.0),
floating: false,
elevation: 0.0,
pinned: true,
primary: true,
centerTitle: true,
title: Text("TITLE",
style: TextStyle(
color: Colors.white,
fontSize: Adapt.px(45.0),
fontFamily: "pinfon",
fontWeight: FontWeight.w900,
letterSpacing: 1.0)),
backgroundColor: Colors.black45,
brightness: Brightness.dark,
bottom: PreferredSize(
preferredSize: Size.fromHeight(60.0),
child: Text(''),
),
flexibleSpace: new FlexibleSpaceBar(
background: Opacity(
opacity: 0.5,
child: new Stack(
fit: StackFit.expand,
children: <Widget>[
new Image.asset(
"assets/images/back.jpg",
fit: BoxFit.fitWidth,
),
],
),
),
),
),
I have tested the sample code you have provided and it seems that the layout is working fine when with an iPad Air - 4th generation - 14.0 emulator.
Though, I'm not able to see your complete code. I've just reused the minimal code you have provided and created a complete example to be able to see the output.
Here is my sample code:
import 'package:flutter/material.dart';
void main() => runApp(SilverAppBarExample());
class SilverAppBarExample extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: CustomScrollView(
slivers: <Widget>[
new SliverAppBar(
expandedHeight: 220.0,
floating: false,
elevation: 0.0,
pinned: true,
primary: true,
centerTitle: true,
title: Text("TITLE",
style: TextStyle(
color: Colors.white,
fontSize: 45.0,
fontFamily: "pinfon",
fontWeight: FontWeight.w900,
letterSpacing: 1.0)),
backgroundColor: Colors.black45,
brightness: Brightness.dark,
bottom: PreferredSize(
preferredSize: Size.fromHeight(60.0),
child: Text(''),
),
flexibleSpace: new FlexibleSpaceBar(
background: Opacity(
opacity: 0.5,
child: new Stack(
fit: StackFit.expand,
children: <Widget>[
new Image.asset(
"assets/background.jpg",
fit: BoxFit.fitWidth,
),
],
),
),
),
),
new SliverList(
delegate: new SliverChildListDelegate(_buildList(50))),
],
),
),
);
}
List _buildList(int count) {
List<Widget> listItems = List();
for (int i = 0; i < count; i++) {
listItems.add(new Padding(
padding: new EdgeInsets.all(20.0),
child: new Text('Item ${i.toString()}',
style: new TextStyle(fontSize: 25.0))));
}
return listItems;
}
}
Output using iPad Air - 4th generation - 14.0 emulator
Expanded:
Shrinked:
It seems that the SliverAppBar is working fine.
I've also tested it in an iPhone emulator for verification.
iPhone SE - 2nd generation - 14.0
Expanded:
Shrinked:
Here is a live output:
Maybe if you could provide the whole code you've created we will be able to check it on our end.

Show masked area on top of camera widget

I'm trying to implement a credit card reader (using some API). For the user to take a picture of the card, I'm presenting the camera preview widget full screen. I would like to mask out an area so the user centers the card there.
He're an illustration of what I have in mind
I was wondering how this masking could be accomplished in flutter? Currently I'm just drawing a BoxDecoration, but that's missing the semi transparent, greyed out area.
Container(
width: MediaQuery.of(context).size.width * 0.8,
height: MediaQuery.of(context).size.width * 0.8 / 1.55,
decoration: new BoxDecoration(
border: Border.all(
color: Colors.white,
width: 2.0,
style: BorderStyle.solid),
shape: BoxShape.rectangle,
borderRadius: BorderRadius.all(Radius.circular(4.0))),
)
You can try something like this for the time being but it is a very inefficient solution and I am sure there must be a better solution that I too would like to know.
#override
Widget build(BuildContext context) {
if (!controller.value.isInitialized) {
return Container();
}
return Container(
height: MediaQuery.of(context).size.height,
child: Stack(
children: <Widget>[
CustomPaint(
foregroundPainter: P(),
child: CameraPreview(controller),
),
ClipPath(
clipper: Clip(),
child: CameraPreview(controller)),
],
),
);
}
class P extends CustomPainter{
#override
void paint(Canvas canvas, Size size) {
canvas.drawColor(Colors.grey.withOpacity(0.8), BlendMode.dstOut);
}
#override
bool shouldRepaint(CustomPainter oldDelegate) {
// TODO: implement shouldRepaint
return true;
}
}
class Clip extends CustomClipper<Path>{
#override
getClip(Size size) {
print(size);
Path path = Path()
..addRRect(RRect.fromRectAndRadius(Rect.fromLTWH(10, size.height/2-120, size.width-20, 240), Radius.circular(26)));
return path;
}
#override
bool shouldReclip(oldClipper) {
// TODO: implement shouldReclip
return true;
}
}
I now just went with a custom BoxPainter and first draw the background, then the card area and finally blend the layers together to 'cut out' the center part.
import 'package:flutter/widgets.dart';
class CardDecoration extends Decoration {
#override
BoxPainter createBoxPainter([VoidCallback onChanged]) {
return _CardPainter();
}
}
class _CardPainter extends BoxPainter {
#override
void paint(Canvas canvas, Offset offset, ImageConfiguration configuration) {
final clearPaint = new Paint()
..color = Colors.black
..style = PaintingStyle.fill
..blendMode = BlendMode.dstOut;
final bgPaint = new Paint()
..color = Color.fromARGB(150, 0, 0, 0)
..style = PaintingStyle.fill;
final borderPaint = new Paint()
..color = Colors.white.withAlpha(120)
..style = PaintingStyle.stroke
..strokeWidth = 3.0;
final rect = offset & configuration.size;
final cardWidth = 0.8*rect.width;
final cardHeight = cardWidth/1.55;
canvas.saveLayer(Rect.fromLTRB(0, 0, rect.width, rect.height), clearPaint);
canvas.drawPaint(bgPaint);
canvas.saveLayer(Rect.fromLTRB(0, 0, rect.width, rect.height), clearPaint);
canvas.drawRRect(RRect.fromLTRBR(0.1*rect.width, (rect.height-cardHeight)/2, 0.9*rect.width, (rect.height+cardHeight)/2, Radius.circular(8)), bgPaint);
canvas.restore();
canvas.restore();
canvas.drawRRect(RRect.fromLTRBR(0.1*rect.width, (rect.height-cardHeight)/2, 0.9*rect.width, (rect.height+cardHeight)/2, Radius.circular(8)), borderPaint);
}
}
Probably, you can try with columns, rows, and containers :).
Widget getMaskCard(BuildContext context) {
Color _background = Colors.grey.withOpacity(0.2);
return Column(
children: <Widget>[
Row(
children: <Widget>[
Expanded(
child: Container(
height: MediaQuery.of(context).size.height,
width: 1,
color: _background,
),
),
Container(
height: MediaQuery.of(context).size.height,
width: MediaQuery.of(context).size.width * 0.8,
child: Column(
children: <Widget>[
Expanded(
child: Container(
color: _background,
),
),
Container(
height: 180,
width: MediaQuery.of(context).size.width * 0.8,
color: Colors.transparent,
),
Expanded(
child: Container(
color: _background,
),
),
],
),
),
Expanded(
child: Container(
height: MediaQuery.of(context).size.height,
width: 1,
color: _background,
),
),
],
)
],
);
}
Example

Resources