How to make soft keyboard appearance animation smooth? - flutter

I have 2 examples with code and GIF.
Problem with first - it is not working with appropriate height.
Problem with second - when soft keyboard appears it is not animated.
What is the appropriate way to make soft keyboard appearance on the screen smooth and clean, when you have to move some objects, according to new screen height?
GIFS:
Code first:
import 'package:flutter/material.dart';
void main() {
runApp(MaterialApp(home: Example()));
}
class Example extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Scaffold(
body: Column(
children: [
Spacer(),
Container(
color: Colors.red,
child: TextField(),
),
],
),
);
}
}
Code second:
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
void main() {
runApp(MaterialApp(home: Example()));
}
class Example extends StatefulWidget {
#override
State<Example> createState() => _ExampleState();
}
class _ExampleState extends State<Example> {
double height = 0;
FocusNode node = FocusNode();
#override
void initState() {
super.initState();
node.addListener(() {
if (height == 0) {
height = 320;
} else {
height = 0;
}
setState(() {});
});
}
#override
Widget build(BuildContext context) {
return Scaffold(
resizeToAvoidBottomInset: false,
body: Column(
children: [
Spacer(),
Container(
color: Colors.red,
child: TextField(
focusNode: node,
),
),
AnimatedContainer(
duration: Duration(milliseconds: 370),
curve: Curves.ease,
height: height,
),
],
),
);
}
}

Related

flutter) How do I create a widgets in a tabbed position of screen?

I am developing a whiteboard app.
I would like to create text box widgets at the potision I want as shown in the image below.
May I know how to create a widget in the position(offset) where I've tapped?
As i comment before. I have an idea: using GestureDetector to detect Tap action and postion of Tap, then using Stack and Positioned to create an Editor like this.
* localPosition and globalPosition is differences, carefull.
import 'package:flutter/material.dart';
const Color darkBlue = Color.fromARGB(255, 18, 32, 47);
void main() {
runApp(MaterialApp(home: MyApp()));
}
class MyApp extends StatefulWidget {
#override
State<MyApp> createState() => MyAppState();
}
class MyAppState extends State<MyApp> {
var editorEnable = false;
var editorPosition = Offset.zero;
void closeEditor() {
print('editor closed');
setState(() {
editorEnable = false;
editorPosition = Offset.zero;
});
}
void newEditorAt(Offset position) {
print('editor created at $position');
setState(() {
editorEnable = true;
editorPosition = position;
});
}
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(),
body: GestureDetector(
onTapUp: (details) {
if (editorEnable) {
closeEditor();
} else {
newEditorAt(details.localPosition);
}
},
child: Stack(
fit: StackFit.expand,
children: [
Container(color: Colors.grey),
if (editorEnable)
Positioned(
left: editorPosition.dx,
top: editorPosition.dy,
child: TextEditor(),
),
],
),
),
);
}
}
class TextEditor extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Container(
color: Colors.white,
width: 200,
height: 50,
child: TextField(
autofocus: true,
),
);
}
}
And here is result

Animated Container not animating

I have an animated container, which starts at width and height 0, and then gets changed to 300 each, after a setState, which I call at initState. But It doesn't seem to animate. Here's the code-
import 'package:flutter/material.dart';
class ChooseCardWidget extends StatefulWidget {
const ChooseCardWidget({Key? key}) : super(key: key);
#override
State<ChooseCardWidget> createState() => ChooseCardWidgetState();
}
class ChooseCardWidgetState extends State<ChooseCardWidget> {
double height = 0;
double width = 0;
final double roundedValue = 20;
#override
void initState() {
super.initState();
startAnimation();
}
void startAnimation() {
setState(() {
height = 300;
width = 300;
});
}
#override
Widget build(BuildContext context) {
return AnimatedContainer(
duration: Duration(seconds: 10),
height: height,
width: width,
decoration: ShapeDecoration(
shadows: const [
BoxShadow(color: Colors.grey, blurRadius: 20, spreadRadius: 5)
],
color: Colors.purple,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(roundedValue),
),
),
);
}
}
It just starts off as being 300 in width and height. How do I fix this? Thanks!
You are calling startAnimation() directly inside initState, means it will get height=300 and width=300 on 1st build.
You can use addPostFrameCallback to animate this, it will set height and width to 300 after 1st frame is build.
#override
void initState() {
super.initState();
WidgetsBinding.instance?.addPostFrameCallback((timeStamp) {
startAnimation();
});
}
Also, you can use
Future.delayed(Duration.zero, () {
startAnimation();
});
Try this
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: YourClass(),
),
);
}
}
class YourClass extends StatefulWidget {
#override
_YourClassState createState() => _YourClassState();
}
class _YourClassState extends State<YourClass> {
double loginWidth = 40.0;
#override
Widget build(BuildContext context) {
return Center(
child: PageView(
children: <Widget>[
Column(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: <Widget>[
RaisedButton (
child: Text('Animate!'),
onPressed: () {
setState(() {
loginWidth = 250.0;
});
},
),
AnimatedContainer (
duration: Duration (seconds: 1),
width: loginWidth,
height: 40,
color: Colors.red,
),
],
)
],
),
);
}
}
This will perfectly work

Flutter Web Mouse Region Widget Triggering

I am trying to get this result with Flutter;
I am having this behaviour;
Code;
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'dart:html' as html;
class OverlayAnimatedGridElement extends StatefulWidget {
OverlayAnimatedGridElement(this.imagepath, this.postDetail, this.postTitle);
final String imagepath;
final String postTitle;
final String postDetail;
#override
_OverlayAnimatedGridElementState createState() =>
_OverlayAnimatedGridElementState();
}
class _OverlayAnimatedGridElementState extends State<OverlayAnimatedGridElement>
with TickerProviderStateMixin {
AnimationController _controller;
Animation<double> _opacityTween;
bool isHovered = false;
#override
void initState() {
_controller =
AnimationController(vsync: this, duration: Duration(milliseconds: 500));
_opacityTween = Tween<double>(begin: 0, end: 1).animate(
CurvedAnimation(parent: _controller, curve: Curves.easeInOutCirc));
super.initState();
}
hoverActivation(hoverState) {
bool hoverState;
isHovered = hoverState;
if (isHovered = true) {
_controller.forward();
} else if (isHovered = false) {
_controller.reset();
}
print("activated");
}
#override
Widget build(BuildContext context) {
_opacityTween.addListener(() {
setState(() {
print(_opacityTween.value);
});
});
return MouseRegion(
onHover: hoverActivation(true),
child: Container(
constraints: BoxConstraints(maxHeight: 360, maxWidth: 640),
child: Stack(
alignment: Alignment.bottomCenter,
children: [
Image(image: AssetImage("${widget.imagepath}")),
Opacity(
opacity: _opacityTween.value,
child: Container(
color: Color.fromRGBO(128, 128, 128, 0.5),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
mainAxisSize: MainAxisSize.max,
children: [
Column(
mainAxisSize: MainAxisSize.min,
children: [
Text("${widget.postDetail}"),
Text("${widget.postTitle}")
],
),
],
),
),
),
],
),
),
);
}
}
A standalone widget works okay but when I put it into gridview or pageview it automatically reads it mouse entered when I scroll into it.
I tried to use other widgets like Inkwell, Listener and others. No solution. Can someone guide me if there is better solution?
How Can I solve this?
edit:
Now having this problem. MouseRegion causes multiple controller.forwards
I am Created an Example ... please Try this,
import 'package:flutter/material.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
void main() => runApp(new MyApp());
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return new MaterialApp(
title: 'Todos',
theme: new ThemeData(primarySwatch: Colors.deepOrange),
home: MyHome(),
);
}
}
class MyHome extends StatelessWidget{
#override
Widget build(BuildContext context) {
return GridView.count(crossAxisCount: 3,
children: List.generate(10, (index) {
return OverlayAnimatedGridElement("https://picsum.photos/300/200");
}
));
}
}
class OverlayAnimatedGridElement extends StatefulWidget {
OverlayAnimatedGridElement(this.imagepath);
final String imagepath;
// final String postTitle;
// final String postDetail;
#override
_OverlayAnimatedGridElementState createState() =>
_OverlayAnimatedGridElementState();
}
class _OverlayAnimatedGridElementState extends State<OverlayAnimatedGridElement>
with TickerProviderStateMixin {
bool isHovered = false;
#override
void initState() {
super.initState();
}
hoverActivation(hoverState) {
setState(() {
isHovered = hoverState;
});
print("activated" + hoverState.toString());
}
#override
Widget build(BuildContext context) {
return Center(
child: Container(
color: Colors.red,
height: 200,
width: 300,
child: Stack(
children: [
Image.network(widget.imagepath),
MouseRegion(
onEnter: (event){
hoverActivation(true);
},
onExit: (event){
hoverActivation(false);
},
child: AnimatedContainer(
duration: Duration(milliseconds: 200),
color: Colors.black.withOpacity(isHovered ? 0.5 : 0),
),
)
],
),
),
);
}
}
I solved my second problem by just moving addListener method to initState. Previously it was inside widget itself which was causing adding listeners in every rebuild and causing multiple rebuilds for each listener, I guess.

Flutter: Where I can add Text()

I want to tap on the screen, and change background color to random color. It works fine, but I want to add Text on the center of screen, help me please.
Maybe someone have ideas, Thank you!
I try to add multiple child to AnimatedContainer.
Tried to add text in GestureDetector, but it doesn't work correctly
TopScreen.dart
import 'package:flutter/material.dart';
class TopScreen extends StatefulWidget {
final Widget child; //child widget
TopScreen({this.child});
#override
State createState() => _TopScreenState();
static _TopScreenState of (BuildContext context) {
assert(context != null);
final _TopScreenState result = context.findAncestorStateOfType();
return result;
}
}
class _TopScreenState extends State<TopScreen> {
Color _color = Colors.white;
#override
Widget build(BuildContext context) {
return Scaffold(
body: AnimatedContainer(
child: widget.child,
color: _color,
duration: Duration(milliseconds: 500),
),
);
}
void setColor(Color color) {
this._color = color;
}
}
main.dart
import 'dart:math';
import 'package:flutter/material.dart';
import 'package:solidsoftwaretest/TopScreen.dart';
void main() => runApp(MaterialApp(home: TopScreen(child: MainScreen())));
class MainScreen extends StatefulWidget {
#override
State createState() => _MainScreenState();
}
class _MainScreenState extends State<MainScreen> {
#override
Widget build(BuildContext context) {
return GestureDetector(
onTap: () {
TopScreen.of(context).setColor(Colors.primaries[Random().nextInt(Colors.primaries.length)]);
TopScreen.of(context).setState(() {});
},
);
} //build
}
UPD: if I add Child (Text) to GestureDetector - gestureDetector works only on Text.
class _MainScreenState extends State<MainScreen> {
#override
Widget build(BuildContext context) {
return GestureDetector(
child: Center(
child: Text('Hey there!')
),
onTap: () {
TopScreen.of(context).setColor(Colors.primaries[Random().nextInt(Colors.primaries.length)]);
TopScreen.of(context).setState(() {});
}
);
} //build
}
You can add Text widget inside a Container & wrap it further in GestureDetector. In order to make GestureDetector work on the whole area, give the Container a transparent color as follows:
return GestureDetector(
onTap: () {
TopScreen.of(context).setColor(Colors.primaries[Random().nextInt(Colors.primaries.length)]);
TopScreen.of(context).setState(() {});
},
child: Container(
color: Colors.transparent,
alignment: Alignment.center,
child: Text('Hey there'),
),
);
Hope, it will help
You should be able to put a Column inside your AnimatedContainer. The Column will hold multiple Widgets.
I've updated the code to show the full example.
ScaffoldColorCenterText60597839(
child: Text('Bananas'),
)
class ScaffoldColorCenterText60597839 extends StatefulWidget {
final Widget child;
ScaffoldColorCenterText60597839({
this.child
});
#override
_ScaffoldColorCenterText60597839State createState() => _ScaffoldColorCenterText60597839State();
}
class _ScaffoldColorCenterText60597839State extends State<ScaffoldColorCenterText60597839> {
Color _color = Colors.white;
#override
Widget build(BuildContext context) {
return Scaffold(
body: AnimatedContainer(
child: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
widget.child,
Text('A text widget')
],
)
),
color: _color,
duration: Duration(milliseconds: 500),
),
);
}
}

how to animate collapse elements in flutter

How can i expand and collapse widget when user taps on different widget ( sibling or parent ) with animation ?
new Column(
children: <Widget>[
new header.IngridientHeader(
new Icon(
Icons.fiber_manual_record,
color: AppColors.primaryColor
),
'Voice Track 1'
),
new Grid()
],
)
I want user to be able to tap on header.IngridientHeader and then Grid widget should toggle ( hide if visible and other way around )
edit:
im trying to do something that in bootstrap is called Collapse. getbootstrap.com/docs/4.0/components/collapse
edit 2:
header.IngridientHeader should stay in place all the time
Grid() is scrollable ( horizontal ) widget.
If you want to collapse a widget to zero height or zero width that has a child that overflow when collapsed, I would recommend SizeTransition or ScaleTransition.
Here is an example of the ScaleTransition widget being used to collapse the container for the four black buttons and status text. My ExpandedSection widget is used with a column to get the following structure.
An example of a Widget that use animation with the SizeTransition widget:
class ExpandedSection extends StatefulWidget {
final Widget child;
final bool expand;
ExpandedSection({this.expand = false, this.child});
#override
_ExpandedSectionState createState() => _ExpandedSectionState();
}
class _ExpandedSectionState extends State<ExpandedSection> with SingleTickerProviderStateMixin {
AnimationController expandController;
Animation<double> animation;
#override
void initState() {
super.initState();
prepareAnimations();
_runExpandCheck();
}
///Setting up the animation
void prepareAnimations() {
expandController = AnimationController(
vsync: this,
duration: Duration(milliseconds: 500)
);
animation = CurvedAnimation(
parent: expandController,
curve: Curves.fastOutSlowIn,
);
}
void _runExpandCheck() {
if(widget.expand) {
expandController.forward();
}
else {
expandController.reverse();
}
}
#override
void didUpdateWidget(ExpandedSection oldWidget) {
super.didUpdateWidget(oldWidget);
_runExpandCheck();
}
#override
void dispose() {
expandController.dispose();
super.dispose();
}
#override
Widget build(BuildContext context) {
return SizeTransition(
axisAlignment: 1.0,
sizeFactor: animation,
child: widget.child
);
}
}
AnimatedContainer also works but Flutter can complain about overflow if the child is not resizable to zero width or zero height.
Alternatively you can just use an AnimatedContainer to mimic this behavior.
class AnimateContentExample extends StatefulWidget {
#override
_AnimateContentExampleState createState() => new _AnimateContentExampleState();
}
class _AnimateContentExampleState extends State<AnimateContentExample> {
double _animatedHeight = 100.0;
#override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(title: new Text("Animate Content"),),
body: new Column(
children: <Widget>[
new Card(
child: new Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
new GestureDetector(
onTap: ()=>setState((){
_animatedHeight!=0.0?_animatedHeight=0.0:_animatedHeight=100.0;}),
child: new Container(
child: new Text("CLICK ME"),
color: Colors.blueAccent,
height: 25.0,
width: 100.0,
),),
new AnimatedContainer(duration: const Duration(milliseconds: 120),
child: new Text("Toggle Me"),
height: _animatedHeight,
color: Colors.tealAccent,
width: 100.0,
)
],
) ,
)
],
),
);
}
}
I think you are looking for ExpansionTile widget. This takes a title property which is equivalent to header and children property to which you can pass widgets to be shown or hidden on toggle.
You can find an example of how to use it here.
Simple Example Usage:
new ExpansionTile(title: new Text("Numbers"),
children: <Widget>[
new Text("Number: 1"),
new Text("Number: 2"),
new Text("Number: 3"),
new Text("Number: 4"),
new Text("Number: 5")
],
),
Hope that helps!
Output:
Code:
class FooPageState extends State<SoPage> {
static const _duration = Duration(seconds: 1);
int _flex1 = 1, _flex2 = 2, _flex3 = 1;
#override
Widget build(BuildContext context) {
final total = _flex1 + _flex2 + _flex3;
final height = MediaQuery.of(context).size.height;
final height1 = (height * _flex1) / total;
final height2 = (height * _flex2) / total;
final height3 = (height * _flex3) / total;
return Scaffold(
body: Column(
children: [
AnimatedContainer(
height: height1,
duration: _duration,
color: Colors.red,
),
AnimatedContainer(
height: height2,
duration: _duration,
color: Colors.green,
),
AnimatedContainer(
height: height3,
duration: _duration,
color: Colors.blue,
),
],
),
);
}
}
Thanks to #Adam Jonsson, his answer resolved my problem. And this is the demo about how to use ExpandedSection, hope to help you.
class ExpandedSection extends StatefulWidget {
final Widget child;
final bool expand;
ExpandedSection({this.expand = false, this.child});
#override
_ExpandedSectionState createState() => _ExpandedSectionState();
}
class _ExpandedSectionState extends State<ExpandedSection>
with SingleTickerProviderStateMixin {
AnimationController expandController;
Animation<double> animation;
#override
void initState() {
super.initState();
prepareAnimations();
_runExpandCheck();
}
///Setting up the animation
void prepareAnimations() {
expandController =
AnimationController(vsync: this, duration: Duration(milliseconds: 500));
animation = CurvedAnimation(
parent: expandController,
curve: Curves.fastOutSlowIn,
);
}
void _runExpandCheck() {
if (widget.expand) {
expandController.forward();
} else {
expandController.reverse();
}
}
#override
void didUpdateWidget(ExpandedSection oldWidget) {
super.didUpdateWidget(oldWidget);
_runExpandCheck();
}
#override
void dispose() {
expandController.dispose();
super.dispose();
}
#override
Widget build(BuildContext context) {
return SizeTransition(
axisAlignment: 1.0, sizeFactor: animation, child: widget.child);
}
}
class MyApp extends StatelessWidget {
// This widget is the root of your application.
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
home: Scaffold(
appBar: AppBar(
title: Text('Demo'),
),
body: Home(),
),
);
}
}
class Home extends StatefulWidget {
#override
_HomeState createState() => _HomeState();
}
class _HomeState extends State<Home> {
bool _expand = false;
#override
Widget build(BuildContext context) {
return Column(
children: [
Header(
onTap: () {
setState(() {
_expand = !_expand;
});
},
),
ExpandedSection(child: Content(), expand: _expand,)
],
);
}
}
class Header extends StatelessWidget {
final VoidCallback onTap;
Header({#required this.onTap});
#override
Widget build(BuildContext context) {
return GestureDetector(
onTap: onTap,
child: Container(
color: Colors.cyan,
height: 100,
width: double.infinity,
child: Center(
child: Text(
'Header -- Tap me to expand!',
style: TextStyle(color: Colors.white, fontSize: 20),
),
),
),
);
}
}
class Content extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Container(
color: Colors.lightGreen,
height: 400,
);
}
}
Another solution that doesn't require an animation controller is using AnimatedSwitcher widget with SizeTransition as a transition builder.
here is a simple example:
AnimatedSwitcher(
duration: Duration(milliseconds: 300),
transitionBuilder: (child, animation) {
return SizeTransition(sizeFactor: animation, child: child);
},
child: expanded ? YourWidget() : null,
)
Of course you can customize the curve and layout builder for the animation.