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');
},
Related
i'm a beginner in flutter and still trying to figure my way around. So i'm trying to build this app(i attached the image below), but I'm finding it difficult to get the gridview into a container and curve the edges of the container or include a border. I tried using Flexible instead to wrap the gridview, but i can't curve the edges with that or put a border.
import 'package:general_quizz/models/first_screen_data.dart';
import 'package:general_quizz/widgets/sectionBoxed.dart';
class Sections extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('quizz app'),
),
body: Column(
children: [
Container(
width: double.infinity,
height: 200,
alignment: Alignment.center,
decoration: BoxDecoration(
color: Colors.redAccent,
),
child: Text('Quizz app')),
Container(
decoration: BoxDecoration(
color:Colors.white,
borderRadius: BorderRadius.only(
topLeft: Radius.circular(50),
topRight: Radius.circular(50),
),
),
Flexible(
child: GridView(
children: DUMMY_CATEGORIES
.map(
(catData) => SectionBoxed(
catData.title,
catData.color,
catData.id,
),
)
.toList(),
gridDelegate: SliverGridDelegateWithMaxCrossAxisExtent(
maxCrossAxisExtent: 200,
childAspectRatio: 3 / 2,
crossAxisSpacing: 20,
mainAxisSpacing: 20,
),
),
),
],
),
);
}
}```
This is what I'm trying to recreate. [this][1]
[I'm trying to recreate this][2]
[1]: https://i.stack.imgur.com/GSl11.jpg
[2]: ![]https://i.stack.imgur.com/3fWCz.jpg
you have to use Container widget like this:
Container(
child:...,
height:200,
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.only(
topLeft: Radius.circular(20),
topRight: Radius.circular(20),
)),
);
I created an application in android studio to navigate from one screen to another.Here two stateless widgets are created as two screens and both contain a button to navigate pages each other.
However when i run the application a red screen is generated on my android phone I get an error saying
exception 'Column's children must not contain any null values, but a null value was found at index 0'.
I have provided my code below:
FIRST SCREEN
class FirstScreen extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("First Screen"),
),
body: Container(
child: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
center(
decoration: new BoxDecoration(
image: new DecorationImage(
image: new AssetImage('assets/new 7wonders.jpg'),
fit: BoxFit.cover,
),
),
),
Text('New 7 Wonders',
style: TextStyle(fontSize: 40, fontStyle: FontStyle.italic),
),
RaisedButton(
child: Text("Bang Here"),
onPressed: (){
Navigator.push(context, MaterialPageRoute(builder: (context) => SecondScreen()));
},
color: Colors.red,
textColor: Colors.yellow,
padding: EdgeInsets.fromLTRB(10, 10, 10, 10),
splashColor: Colors.grey,
)
],
),
),
),
);
}
center({BoxDecoration decoration}) {}
}
SECOND SCREEN
class SecondScreen extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Second Screen"),
),
body: RaisedButton(
child: Text("Go to First page"),
onPressed:() {
Navigator.pop(context);
},
),
);
}
}
Your center method should return a Widget, it is currently providing null to the Column.
Do this instead:
Widget center() {
// return a decorated box widget which gives you the decoration property
return Image(
image: AssetImage(
'assets/new 7wonders.jpg',),
fit: BoxFit.cover,
);
}
}
Then use in your Column like :
Container(
child: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
// call the center method which returns a Widget
center(),
Text(
'New 7 Wonders',
style: TextStyle(fontSize: 40, fontStyle: FontStyle.italic),
),
RaisedButton(
child: Text("Bang Here"),
onPressed: () {
Navigator.push(context,
MaterialPageRoute(builder: (context) => SecondScreen()));
},
color: Colors.red,
textColor: Colors.yellow,
padding: EdgeInsets.fromLTRB(10, 10, 10, 10),
splashColor: Colors.grey,
)
],
),
),
),
you have to return any widget in center
center({BoxDecoration decoration}) {
return Container();
}
You tryed write Center instead center in line 24?
And in Center must be will return for example Containter()
In 24th line, you returned null value. You can implement the center method like this;
return Container();
Remove center use this
Container(
height: 100, // height and width according to your ui
width:100,
child:Image.asset(('assets/new7wonders.jpg',fit: BoxFit.cover,), // use for local image from asset and please change image name in code as well as in asset folder.there should not be space between image name .
),
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>[
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.
I have a long text and I need to show the scrollbar by default when the user enters my page.
Currently, the bars not shown until the user click over the text and this, not good behavior because the user could leave the page without notice that there is some unread text.
My code:
return Scaffold(
body: Padding(
padding: const EdgeInsets.all(15.0),
child: Center(
child: Column(
children: <Widget>[
Image.asset(
"assets/images/logo.png",
height: 200.0,
),
SizedBox(
height: 40,
),
Expanded(
child: Scrollbar(
child: SingleChildScrollView(
child: Text("Long Text Here ...",
textDirection: TextDirection.rtl,
style: TextStyle(fontSize: 17.2),
),
),
),
),
SizedBox(
height: 50,
),
Row(
children: <Widget>[
Expanded(
child: RaisedButton(
child: Text("Continue"),
onPressed: () {
MaterialPageRoute route = MaterialPageRoute(
builder: (BuildContext context) => MainPage());
Navigator.of(context).push(route);
},
),
),
SizedBox(
width: 20.0,
),
RaisedButton(
child: Text("Close"),
onPressed: () {
exit(0);
},
),
],
)
],
),
),
),
);
}```
As of Flutter version 1.17, on Scrollbar you can set isAlwaysShown to true, but you must set the same controller for your Scrollbar and your SingleChildScrollView (and that applies to any other scrollable Widget as well).
Have in mind that, for the Scrollbar to be visible, there must be enough items to scroll. If there are not, the Scrollbar won't be shown.
Full working example:
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: MyWidget(),
),
);
}
}
class MyWidget extends StatefulWidget {
#override
_MyWidgetState createState() => _MyWidgetState();
}
class _MyWidgetState extends State<MyWidget> {
final _scrollController = ScrollController();
#override
Widget build(BuildContext context) {
return Scaffold(
body: Padding(
padding: const EdgeInsets.all(15.0),
child: Center(
child: Column(
children: <Widget>[
// ...
Expanded(
child: Scrollbar(
controller: _scrollController, // <---- Here, the controller
isAlwaysShown: true, // <---- Required
child: SingleChildScrollView(
controller: _scrollController, // <---- Same as the Scrollbar controller
child: Text(
"Long Text Here ...",
textDirection: TextDirection.rtl,
style: TextStyle(fontSize: 17.2),
),
),
),
),
// ...
],
),
),
),
);
}
}
As of v2.9.0-1.0, thumbVisiblity is the proper field to set.
Note you can set this globally for your app (or a certain subtree) using ScrollbarTheme:
return MaterialApp(
...
theme: ThemeData(
...
scrollbarTheme: ScrollbarThemeData(
thumbVisibility: MaterialStateProperty.all<bool>(true),
)
)
)
It's good to prefer themes for styling like this, so avoid doing more than once.
You'll still need to add a Scrollbar and Controller as described in other answers though.
Use draggable_scrollbar package. It provides a dragable scrollbar with option to make the scrollbar always visible. For example, you can use the following code
DraggableScrollbar.arrows(
alwaysVisibleScrollThumb: true, //use this to make scroll thumb always visible
labelTextBuilder: (double offset) => Text("${offset ~/ 100}"),
controller: myScrollController,
child: ListView.builder(
controller: myScrollController,
itemCount: 1000,
itemExtent: 100.0,
itemBuilder: (context, index) {
return Container(
padding: EdgeInsets.all(8.0),
child: Material(
elevation: 4.0,
borderRadius: BorderRadius.circular(4.0),
color: Colors.purple[index % 9 * 100],
child: Center(
child: Text(index.toString()),
),
),
);
},
),
);
'isAlwaysShown' is deprecated and shouldn't be used. Use thumbVisibility instead.
Example:
Scrollbar(
controller: ScrollController(),
thumbVisibility: true,
child: SingleChildScrollView(
child: Column(
thumbVisibility make true for show always scroll bar for list in scrollbar widget
Scrollbar(thumbVisibility: true,)
You can change the scrollbartheme to set flag isAlwaysShown true
scrollbarTheme: const ScrollbarThemeData(isAlwaysShown: true)