Flutter: How to show and hide the Lottie animation - flutter

I already success to show the Lottie like below:
But the question is, how to trigger to show and hide the Lottie with a button? For example, the Lottie is not showing, but when I clicking Show Lottie button, the Lottie will show, and when I clicking Hide Lottie button, the Lottie will hide.
This is my full code:
import 'package:flutter/material.dart';
import 'package:lottie/lottie.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('Lottie Flutter'),
),
body: Container(
width: double.infinity,
child: Column(
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
SizedBox(
width: 50,
height: 50,
child: Lottie.asset(
'assets/10219-notification-dot.json',
),
),
Row(
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
RaisedButton(
onPressed: () {},
child: Text('Show Lottie'),
),
RaisedButton(
onPressed: () {},
child: Text('Hide Lottie'),
),
],
),
],
),
),
),
);
}
}

use a showDialogue method.
child -> GestureDetector, which when tapped do a pop.
Code is below:
showDialog(
context: context,
builder: (context) {
return GestureDetector(
onTap: () {
Navigator.pop(context);
},
child: Lottie.asset('assets/animations/success.json',
repeat: false, animate: true),
);
});
});

I solved this with using setState and Visibility widget, this is my code:
import 'package:flutter/material.dart';
import 'package:lottie/lottie.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatefulWidget {
#override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
var _isShow = false;
#override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('Lottie Flutter'),
),
body: Container(
width: double.infinity,
child: Column(
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
SizedBox(
width: 50,
height: 50,
child: Visibility(
visible: _isShow,
child: Lottie.asset(
'assets/10219-notification-dot.json',
),
),
),
Row(
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
RaisedButton(
onPressed: () {
setState(() {
_isShow = true;
});
},
child: Text('Show Lottie'),
),
RaisedButton(
onPressed: () {
setState(() {
_isShow = false;
});
},
child: Text('Hide Lottie'),
),
],
),
],
),
),
),
);
}
}

You can use a bool and if else condition to show it. Below code can give an idea perhaphs.
var show = true;
...
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
....
show ?? Lottie.asset(
'assets/10219-notification-dot.json',
),
....
RaisedButton(
onPressed: () {
setState(){show = true}
},
child: Text('Show Lottie'),
),
RaisedButton(
onPressed: () {
setState(){show = false}
},
child: Text('Hide Lottie'),
),

Related

ChoiceChip not expanding with IntrinsicWidth

I'm wondering why ChoiceChip is not expanding like ElevatedButton does.
Here is an example: (look at dartpad)
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 StatelessWidget {
#override
Widget build(BuildContext context) {
return Column(children: [
IntrinsicWidth(
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
mainAxisSize: MainAxisSize.min,
children: [
Expanded(
child: ElevatedButton(
child: const Text('Larger Text'),
onPressed: () {},
)),
Expanded(
child: ElevatedButton(
child: const Text('Text'),
onPressed: () {},
)),
],
)),
IntrinsicWidth(
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
mainAxisSize: MainAxisSize.min,
children: const [
Expanded(
child: ChoiceChip(
label: Text('Larger Text'),
selected: false,
)),
Expanded(
child: ChoiceChip(
label: Text('Text'),
selected: false,
)),
],
))
]);
}
}
To solve this, let's start by enabling Flutter Inspector, we can see the second IntrinsicWidth width and its children don't fill their parent as the first one (two problems)
Solve 1st problem: IntrinsicWidth children don't fill their parent width
So, the problem is the size of the 2nd IntrinsicWidth's children is not wide/big enough so that it can be full of parent width. Try increasing its width manually by wrapping it in a Container with double.infinity width, like this:
ChoiceChip(
label: Container(width: double.infinity, child: Text('Larger Text')),
selected: false,
)
New result:
Solve 2nd problem: the 2nd IntrinsicWidth don't fill their parent width
Let's leave Column children can have the maximum width as it is (removing all IntrinsicWidth inside its children) and then wrap the Column by an IntrinsicWidth. Complete sample code:
Sample code
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
#override
Widget build(BuildContext context) {
return const MaterialApp(
home: Scaffold(
body: MyWidget(),
),
);
}
}
class MyWidget extends StatelessWidget {
const MyWidget({super.key});
#override
Widget build(BuildContext context) {
return IntrinsicWidth(
child: Column(
children: [
Row(
mainAxisAlignment: MainAxisAlignment.center,
mainAxisSize: MainAxisSize.min,
children: [
Expanded(
child: ElevatedButton(
child: const Text('Larger Text'),
onPressed: () {},
),
),
Expanded(
child: ElevatedButton(
child: const Text('Text'),
onPressed: () {},
),
),
],
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
mainAxisSize: MainAxisSize.min,
children: [
Expanded(
child: ChoiceChip(
label: Container(width: double.infinity, child: Text('Larger Text')),
selected: false,
),
),
Expanded(
child: ChoiceChip(
label: Container(width: double.infinity, child: Text('Text')),
selected: false,
),
),
],
),
],
),
);
}
}
Final result:
Because under the hood the widgets are designed differently (that's why the names).
I haven't checked the code in detail (you can do that by ctrl + click on Widget Name).
But best guess, visual density is lower in chips for being compact and higher in buttons.
More on technical difference to understand which to use : chips-vs-button

How do i make a custom button in flutter?

i need help with my app. iam new to flutter and wanted to try to make a homepage with an intresting button. i have design my homepage in figma but i dont really know how to make the button to be the same, so here's my figma UI design that i want to implement
i use an SVG icon for the button.
and so far in my code, my HomePage only looks like this
and btw this is my HomePage code
import 'package:flutter/material.dart';
import 'package:get/get_navigation/get_navigation.dart';
import 'package:medreminder/NewsArticle/news_home.dart';
import 'Reminder/ui/home_reminder.dart';
import 'Reminder/ui/widgets/button.dart';
import 'package:medreminder/main_reminder.dart';
import 'package:medreminder/home_page.dart';
void main() {
// debugPaintSizeEnabled = true;
runApp(const HomePage());
}
class HomePage extends StatelessWidget {
const HomePage({super.key});
#override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: Scaffold(
appBar: AppBar(
title: const Text('Medicine Reminder App'),
),
body: Column(
children: [
Stack(
children: [
Image.asset(
'images/MenuImg.jpg',
width: 600,
height: 200,
fit: BoxFit.cover,
),
],
),
const SizedBox(height: 10.0),
Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
ElevatedButton(
child: const Text('Reminder'),
onPressed: () {
Navigator.of(context, rootNavigator: true).push(
MaterialPageRoute(builder: (context) => const ReminderHomePage()),
);
},
),
ElevatedButton(
child: const Text('News & Article'),
onPressed: () {
Navigator.of(context, rootNavigator: true).push(
MaterialPageRoute(builder: (context) => const NewsHomePage()),
);
},
),
ElevatedButton(
child: const Text('Healty Food Recipe'),
onPressed: () {},
),
],
),
],
),
),
);
}
}
thankyou guys for the attention, any help would mean so much to me. thankyou
I assume you have those images in your asset folder, so you can use this widget to build your custom button:
Widget buildCustomButton(String imagePath, String title, Function()? onTap) {
return InkWell(
onTap: onTap,
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Container(
clipBehavior: Clip.antiAlias,
height: 50,
width: 50,
decoration: BoxDecoration(
shape: BoxShape.circle,
),
child: Image.asset(imagePath, fit: BoxFit.none),
),
SizedBox(
height: 8,
),
Text(
title,
textAlign: TextAlign.center,
)
],
),
);
}
and use it like this:
buildCustomButton('assets/images/reminder.png', 'Reminder', () {
print("Reminder click");
}),
wrap the widget you want to be a button with inkwell, you can make buttons as you want.
InkWell(
onTap: () {},
child: Container(
child: FlutterLogo(
size: 80,
),
))
Make design as you want then wrap that image with GestureDetector like this.
GestureDetector (onTap:()
{Your Method('Where you want to naviagate')},
child:Your Design})

Pull down in a flutter app to refresh the app state

I want to update/refresh the balanceAvailable value inside the Text Widget by pulling down the mobile screen in a flutter app.
I have attached a sample code that I am working on for your reference.
I does not seem to work as intended. I would be grateful if someone can provide a solution to this issue.
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatefulWidget {
#override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
final GlobalKey<RefreshIndicatorState> _refreshIndicatorKey =
GlobalKey<RefreshIndicatorState>();
double balanceAvailable = 0.0;
Future<void> _onRefreshing() async {
setState(() async {
balanceAvailable = 100;
print('newbalance : $balanceAvailable');
});
}
#override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: RefreshIndicator(
key: _refreshIndicatorKey,
onRefresh: _onRefreshing,
child: Container(
width: double.infinity,
color: Colors.lightBlue,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
RaisedButton(
onPressed: () async {},
child: Text("Just a Button 1"),
),
SizedBox(
height: 100,
),
Text('$balanceAvailable'),
],
),
)),
),
);
}
}
In order for RefreshIndicator to work, it needs a vertically scrollable descendant like a ListView:
RefreshIndicator(
key: _refreshIndicatorKey,
onRefresh: _onRefreshing,
child: ListView(
physics: const AlwaysScrollableScrollPhysics(),
children: [
Container(
width: double.infinity,
color: Colors.lightBlue,
child: Column(
mainAxisSize: MainAxisSize.min,
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
RaisedButton(
onPressed: () async {},
child: Text("Just a Button 1"),
),
SizedBox(
height: 100,
),
Text('$balanceAvailable'),
],
),
),
],
),
)

Flutter: How to reduce size of CupertinoPicker in ModalBottomSheet?

Whenever I try to set up a CupertinoPicker in a ModalBottomSheet in a Column, Row, or in a Container it seems to occupy the maximum possible place.
Is there anyway to limit the size/heigt of the ModalBottomSheet to the actual size of the CupertinoPicker, which is obviously much smaller in height than the ModalBottomSheet (as becomes apparent through its grey background color).
Here is my code:
import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
home: MyHomePage(),
);
}
}
class MyHomePage extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Scaffold(body:
Center(child: RaisedButton(onPressed: () => showPicker(context),
child: Text('Show Bottom Sheet')),)
);
}
Widget showPicker(BuildContext context) {
showModalBottomSheet(
context: context,
builder: (context) {
return Container(
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Text('This is a picker'),
Expanded(
child: Row(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Expanded(
child: Container(
height: 100,
child: CupertinoPicker(
itemExtent: 30,
onSelectedItemChanged: (int index) {
print(index);
},
children: <Widget>[
Center(child: Text("Item 1")),
Center(child: Text("Item 2")),
Center(child: Text("Item 3")),
],
),
),
),
],
),
),
],
),
);
});
}
}
This is what I get:
Thank you for any tips, hints and advice!
Widget showPicker(BuildContext context) {
showModalBottomSheet(
context: context,
builder: (context) {
return Container(
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Text('This is a picker'),
Row(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Expanded(
child: Container(
height: 100,
child: CupertinoPicker(
itemExtent: 30,
onSelectedItemChanged: (int index) {
print(index);
},
children: <Widget>[
Center(child: Text("Item 1")),
Center(child: Text("Item 2")),
Center(child: Text("Item 3")),
],
),
),
),
],
),
],
),
);
});
}
This can solve the issue paste the method, you will get your desired output

Navigation.of(context) not going to another route

I'm trying to go to a new route using the following code but it doesn't work. There are no errors showing up in my IDE and my code seems pretty similar to all the resources I've checked on how to go to a new page.
I've compared my code to others online. I'm guessing its how I've implemented it. I have also tried using:
MaterialPageRoute(builder: (context) => SecondRoute())
But that was not the issue. Here is most of my app:
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
Widget setTimer = Container(
decoration: BoxDecoration(
border: Border.all(color: Colors.purpleAccent,width: 3)
),
padding: EdgeInsets.all(32),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
RaisedButton(
child: Text('Set Timer'),
onPressed: (){
//Go to the second route
Navigator.of(context).pushNamed('/page2');
},
)
],
),
);
return MaterialApp(
title: 'Hands Off',
routes: <String, WidgetBuilder>{
'/page2': (BuildContext context) => SecondRoute(),
},
home: Scaffold(
appBar: AppBar(
title: Text('Timer config'),
),
body: Column(
children: <Widget>[
setTimer,
],
),
),
);
}
}
class SecondRoute extends StatelessWidget{
#override
Widget build(BuildContext context) {
Widget firstRouteButton = Container(
child: Row(
children: <Widget>[
RaisedButton(
child: Text('Go back'),
onPressed: (){
//Go back to main route (first route)
Navigator.of(context).pop(true);
},
)
],
),
);
return Scaffold(
appBar: AppBar(
title: Text('hel'),
),
);
}
}
It should go to a new page but whenever I press the button nothing happens. Can you guys explain why it doesn't work as well please?
Pretty simple.
Use a Builder to pass the context down.
Builder(builder: (BuildContext context) {
return Whatever_Widget;
},)
so the code will be
body: Builder(builder: (BuildContext context) {
return Column(
children: <Widget>[
Container(
decoration: BoxDecoration(border: Border.all(color: Colors.purpleAccent, width: 3)),
padding: EdgeInsets.all(32),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
RaisedButton(
child: Text('Set Timer'),
onPressed: () {
//Go to the second route
// Navigator.of(context).push(MaterialPageRoute(builder: (context) => SecondRoute()));
Navigator.of(context).pushNamed('/page2');
},
)
],
),
),
],
);
},)