Flutter Layout Row / Column – share width, expand height

Have a look at IntrinsicHeight; wrapping the root Row should provide the effect you’re looking for: import ‘package:flutter/material.dart’; void main() => runApp(MyApp()); class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( title: ‘Flutter Demo’, theme: ThemeData( primarySwatch: Colors.blue, ), home: Scaffold( appBar: AppBar(title: Text(‘Rows & Columns’)), body: RowsAndColumns(), ), ); } } … Read more

How to use conditional statement within child attribute of a Flutter Widget (Center Widget)

Actually you can use if/else and switch and any other statement inline in dart / flutter. Use an immediate anonymous function class StatmentExample extends StatelessWidget { Widget build(BuildContext context) { return Text((() { if(true){ return “tis true”;} return “anything but true”; })()); } } ie wrap your statements in a function (() { // your … Read more

Importance of Calling SetState inside initState

The setState() method notifies the framework that the internal state of the Stateful widget has changed. Calling this method is what triggers the widget to rebuild with the latest state values, so it is not necessary to call it inside the initState() lifecycle method since it is only called once when the widget is inserted … Read more

How to test Flutter widgets on different screen sizes?

You can specify custom surface size by using WidgetTester The following code will run a test with a screen size of 42×42 import ‘package:flutter/widgets.dart’; import ‘package:flutter_test/flutter_test.dart’; void main() { testWidgets(“foo”, (tester) async { tester.binding.window.physicalSizeTestValue = Size(42, 42); // resets the screen to its original size after the test end addTearDown(tester.binding.window.clearPhysicalSizeTestValue); // TODO: do something }); … Read more

Flutter: How to hide or show more text within certain length

you can do that this way import ‘package:flutter/material.dart’; import ‘package:meta/meta.dart’; void main() => runApp(new MyApp()); class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return new MaterialApp( title: ‘Flutter Demo’, home: new HomeScreen(), ); } } class HomeScreen extends StatelessWidget { final String description = “Flutter is Google’s mobile UI framework for crafting high-quality … Read more

How to update state of a ModalBottomSheet in Flutter?

You can use Flutter’s StatefulBuilder to wrap your ModalBottomSheet as follows: showModalBottomSheet( context: context, builder: (context) { return StatefulBuilder( builder: (BuildContext context, StateSetter setState /*You can rename this!*/) { return Container( height: heightOfModalBottomSheet, child: RaisedButton(onPressed: () { setState(() { heightOfModalBottomSheet += 10; }); }), ); }); }); Please note that the new setState will override … Read more

How to debounce Textfield onChange in Dart?

Implementation Import dependencies: import ‘dart:async’; In your widget state declare a timer: Timer? _debounce; Add a listener method: _onSearchChanged(String query) { if (_debounce?.isActive ?? false) _debounce.cancel(); _debounce = Timer(const Duration(milliseconds: 500), () { // do something with query }); } Don’t forget to clean up: @override void dispose() { _debounce?.cancel(); super.dispose(); } Usage In your … Read more

How to use shared preferences to keep user logged in flutter?

You can navigate to the Login page if the user details are saved in the storage else to the Home page with the below code Future<void> main() async { WidgetsFlutterBinding.ensureInitialized(); SharedPreferences prefs = await SharedPreferences.getInstance(); var email = prefs.getString(’email’); print(email); runApp(MaterialApp(home: email == null ? Login() : Home())); } Save the required user details after … Read more