Flutter : How to make button like this [closed] - flutter

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 months ago.
Improve this question
i want to make button like this
The background is of different colors and shapes
Text and icon

Here is a complete code for making one of the buttons. you can play with bordet radius to build other buttons as well.
enter code here import 'package:flutter/material.dart';
void main() => runApp(const MyApp());
class MyApp extends StatelessWidget {
final Overlay overlay = const Overlay();
const MyApp({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: '3D Button',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const Home(),
);
}
}
class Home extends StatefulWidget {
const Home({Key? key}) : super(key: key);
#override
_HomeState createState() => _HomeState();
}
class _HomeState extends State<Home> {
#override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: colors[50],
appBar: AppBar(
title: const Center(
child: Text("3D Button"),
),
),
// body: const ClickyButtonPage(),
body: Center(child: const Custom3DButton()),
);
}
}
const MaterialColor colors = Colors.blue;
const MaterialColor colors2 = Colors.grey;
class Custom3DButton extends StatelessWidget {
const Custom3DButton({
Key? key,
}) : super(key: key);
#override
Widget build(BuildContext context) {
double _width = 220;
double _height = 65;
double _sideWidth = 180;
var _borderRadius = const Radius.circular(50);
return Container(
width: _width,
height: 70.0,
child: GestureDetector(
child: Stack(children: <Widget>[
Positioned(
bottom: 0,
child: Container(
width: _width,
height: _height,
decoration: BoxDecoration(
borderRadius: BorderRadius.all(_borderRadius),
color: colors[800],
),
),
),
Positioned(
left: 0,
bottom: 0,
child: Container(
width: _sideWidth * 5 / 6,
height: _height,
decoration: BoxDecoration(
borderRadius: BorderRadius.only(
bottomLeft: _borderRadius, topLeft: _borderRadius),
color: colors2[400],
),
),
),
Container(
width: _width,
height: _height,
decoration: BoxDecoration(
borderRadius: BorderRadius.all(_borderRadius),
color: Colors.blue,
),
),
Positioned(
left: 0,
child: Container(
width: _sideWidth,
height: _height,
decoration: BoxDecoration(
borderRadius: BorderRadius.all(_borderRadius),
color: Colors.white,
),
child: Center(
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const Icon(
Icons.menu_rounded,
size: 34,
color: Colors.blue,
),
const SizedBox(
width: 10,
),
Text(
'SETTING',
style: TextStyle(color: colors[600], fontSize: 24),
),
],
),
),
),
),
]),
),
);
}
}
Here is the screen shot for this example;

Related

How could I create a two color background for my app, I'm not trying to make a gradient, so far that's all the solutions I've been seeing

Screenshot of a mobile app ui design.
Play with this widget and follow flutter.dev/development/ui/layout
void main() => runApp(const MyApp());
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
static const String _title = 'Flutter Code Sample';
#override
Widget build(BuildContext context) {
return MaterialApp(title: _title, home: SampleW());
}
}
class SampleW extends StatelessWidget {
const SampleW({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.blueGrey,
body: LayoutBuilder(
builder: (context, constraints) => SingleChildScrollView(
child: Column(
children: [
Container(
height: constraints.maxHeight * .4,
alignment: Alignment.center,
child: CircleAvatar(
radius: constraints.maxWidth * .2,
backgroundColor: Colors.amber,
),
),
Container(
height: constraints.maxHeight, // will get by column
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.only(
topLeft: Radius.circular(24),
topRight: Radius.circular(24),
)),
)
],
),
),
),
);
}
}
This will give you
You can use like this:
Scaffold (
backgroundColor: Colors.red,
body: Align(
alignment: Alignment.bottomCenter,
child: Container(
height: MediaQuery.of(context).size.height * 0.6,
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.only(topLeft: Radius.circular(12),topRight: Radius.circular(12)),
),
),
)
)

flutter: image overlap card

i'm new to flutter and i wanted to create simple design for menu app as shown in image below ... i tried below code but it didn't give same design, is there any way to achieve it?
enter image description here
import 'package:flutter/material.dart';
main() => runApp(test());
class test extends StatefulWidget {
#override
_testState createState() => _testState();
}
class _testState extends State<test> {
#override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text("Card over stack"),
),
body: Stack(
children: <Widget>[
Align(
alignment: Alignment.topCenter,
child: Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.all(Radius.circular(10.0)),
color: Colors.lightBlueAccent),
height: 100,
),
),
Positioned(
top: 60,
right: 10,
left: 10,
child: Card(
child: ListTile(
leading: SizedBox(
height: 150.0,
width: 150.0, // fixed width and height
child: Image.asset("assets/images/test.png"))),
),
),
],
),
),
);
}
}
Stack is the rignt choice, check screenshot below:
Full working code:
import 'package:flutter/material.dart';
main() => runApp(const DemoApp());
class DemoApp extends StatefulWidget {
const DemoApp({Key? key}) : super(key: key);
#override
State<StatefulWidget> createState() {
return _DemoState();
}
}
class _DemoState extends State<DemoApp> {
#override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: Stack(
alignment: Alignment.bottomCenter,
children: [
Container(
color: Colors.white,
),
Positioned(
bottom: 150,
// replace with your Card here
child: Card(
child: Container(
width: 250,
height: 300,
color: Colors.blue,
),
),
),
Positioned(
bottom: 320,
// replace with your image here
child: Container(
width: 200,
height: 280,
color: Colors.pink,
),
),
],
),
),
);
}
}
Here is a solution for this. You can also refer to the following link to learn more.
https://www.flutterbeads.com/flutter-position-widget-in-stack/
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
// This widget is the root of your application.
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key}) : super(key: key);
#override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
#override
Widget build(BuildContext context) {
return Scaffold(
body: Container(
height: MediaQuery.of(context).size.height,
decoration: const BoxDecoration(
color: Colors.redAccent,
//add your gradiant here
),
child: SafeArea(
child: Stack(
alignment: AlignmentDirectional.topCenter,
children: [
Positioned(
top: 180,
child: Container(
width: MediaQuery.of(context).size.width * 0.9,
height: 500,
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(7),
),
),
),
Positioned(
top: 20,
child: Container(
width: MediaQuery.of(context).size.width * 0.7,
height: 200,
decoration: BoxDecoration(
color: Colors.blueAccent,
borderRadius: BorderRadius.circular(7),
),
//add your image here
//child: Center(child: Image.asset(name)),
),
),
],
),
),
),
);
}
}

Flutter: rounded corner container

i am new to flutter. How can I indent the corners like in the picture? Thanks for answers.
exapmle
You should be checking if the problem you faced asked before. There are tons of sources related to this.
1- There are containers with one rounded border in the example video you provided. To achieve that, check the following code:
BoxDecoration(
color: color,
borderRadius: const BorderRadius.only(
bottomLeft: Radius.circular(100.0),
),
);
2- To have multiple containers on top of each other, you need to use the Stack widget like the following:
Stack(
children: <Widget>[
Container(
height: MediaQuery.of(context).size.height * 0.90,
decoration: decoration(Colors.black),
),
Container(
height: MediaQuery.of(context).size.height * 0.70,
decoration: decoration(Colors.purple),
),
Container(
height: MediaQuery.of(context).size.height * 0.45,
decoration: decoration(Colors.pink),
),
Container(
height: MediaQuery.of(context).size.height * 0.20,
decoration: decoration(Colors.white),
),
],
),
Complete code main.dart:
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) => MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const MyHomePage(),
);
}
class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key}) : super(key: key);
#override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
#override
Widget build(BuildContext context) => Scaffold(
backgroundColor: Colors.grey,
body: Stack(
children: <Widget>[
Container(
height: MediaQuery.of(context).size.height * 0.90,
decoration: decoration(Colors.black),
),
Container(
height: MediaQuery.of(context).size.height * 0.70,
decoration: decoration(Colors.purple),
),
Container(
height: MediaQuery.of(context).size.height * 0.45,
decoration: decoration(Colors.pink),
),
Container(
height: MediaQuery.of(context).size.height * 0.20,
decoration: decoration(Colors.white),
),
],
),
floatingActionButton: FloatingActionButton(
onPressed: () {},
tooltip: 'Add',
backgroundColor: Colors.white,
child: const Icon(
Icons.add,
color: Colors.black,
),
),
);
BoxDecoration decoration(Color color) => BoxDecoration(
color: color,
borderRadius: const BorderRadius.only(
bottomLeft: Radius.circular(100.0),
),
);
}
You can also access the complete source code through GitHub.
If you have further questions, please don't hesitate to write in the comments.

How to add highlight overlay around the widget upon click?

I'm working on simple design system using Flutter. I want to highlight a widget upon selection (click), as you can see in the below images button get highlighted upon click. It gets handle and border.
Challenging part: I don't want layout getting changed as additional space taken by handle and border upon click. I want widget, handle and border are overlaid, so that it wouldn't shift the position of other neighbouring widgets.
And after selection
You could also use a Stack with the overlay bleeding out of the Stack thanks to a clipBehavior of Clip.none.
Full code
Just copy paste it in a DartPad to see it in action.
import 'package:flutter/material.dart';
const kcPrimary = Color(0xFF001989);
const kcSecondary = Color(0xFF239689);
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData.light(),
debugShowCheckedModeBanner: false,
home: const HomePage(),
);
}
}
class HomePage extends StatelessWidget {
const HomePage({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
child: Center(
child: Column(
mainAxisSize: MainAxisSize.min,
children: const [
Text('Blablablablabla'),
Text('Blablablablabla'),
Text('Blablablablabla'),
PlayButton(),
Text('Blablablablabla'),
Text('Blablablablabla'),
Text('Blablablablabla'),
],
),
),
),
);
}
}
class PlayButton extends StatefulWidget {
const PlayButton({Key? key}) : super(key: key);
#override
State<PlayButton> createState() => _PlayButtonState();
}
class _PlayButtonState extends State<PlayButton> {
bool clicked = false;
#override
Widget build(BuildContext context) {
return Stack(
clipBehavior: Clip.none,
children: [
InkWell(
onTap: () => setState(() => clicked = !clicked),
child: _mainButton,
),
if (clicked) ...[
Positioned.fill(
child: IgnorePointer(
child: _overlayBorder,
),
),
Positioned(
top: -20.0,
left: 0,
child: _overlayTitle,
),
Positioned(top: 0, right: 0, child: _corner),
Positioned(bottom: 0, right: 0, child: _corner),
Positioned(bottom: 0, left: 0, child: _corner),
Positioned(top: 0, left: 0, child: _corner),
],
],
);
}
Widget get _mainButton => Container(
width: 80.0,
height: 40.0,
decoration: BoxDecoration(
border: Border.all(
color: kcPrimary,
width: 3.0,
),
borderRadius: const BorderRadius.all(Radius.circular(12))),
child: Center(
child: Row(
mainAxisSize: MainAxisSize.min,
children: const [
Icon(Icons.play_arrow),
Text('Play'),
],
),
),
);
Widget get _overlayBorder => Container(
decoration: BoxDecoration(
border: Border.all(
color: kcSecondary,
width: 3.0,
),
),
);
Widget get _corner => Container(width: 10, height: 10, color: kcSecondary);
Widget get _overlayTitle => Container(
height: 20.0,
width: 48.0,
color: kcSecondary,
alignment: Alignment.center,
child: const Text(
'Button',
style: TextStyle(
color: Colors.white,
fontSize: 10,
fontWeight: FontWeight.bold,
),
),
);
}

How to stop positioned text objects from moving inside Stack as text changes across devices?

Objective: I want a TextButton to be bisected by a Container in a Stack.
Problem: The size of the TextButton changes across devices, causing an unwanted offset. See the first photo.
Below is a minimum reproducible example in which I use a static integer offset to position the TextButton. I also attempted to specify the TextButton's parent Container's width and height in an attempt to ensure my positional offset was the same each time, but overconstraining the TextButton caused odd reactions like this:
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
title: '):',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: '):'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key? key, required this.title}) : super(key: key);
final String title;
#override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
bool buttonToggled = false;
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Padding(
padding: EdgeInsets.fromLTRB(0, 30, 0, 0),
child: Stack(
clipBehavior: Clip.hardEdge,
alignment: AlignmentDirectional.center,
children: <Widget>[
Container(
//outline
height: 135,
margin: EdgeInsets.fromLTRB(20, 20, 20, 20),
decoration: BoxDecoration(
border: Border.all(color: Colors.purpleAccent, width: 1),
borderRadius: BorderRadius.circular(5),
shape: BoxShape.rectangle,
color: Colors.green,
),
),
Positioned(
left: 40,
top: 3,
child: Container(
margin: EdgeInsets.fromLTRB(10, 0, 0, 0),
padding: EdgeInsets.fromLTRB(7, 0, 7, 0),
height: 30,
color: Colors.blue,
child: Center(
child: TextButton(
child: Text(
buttonToggled ? '+ End Date and Time ' : '— End Date and Time',
textAlign: TextAlign.start, //aligment
style: TextStyle(color: Colors.white),
),
onPressed: () {
setState(() {
buttonToggled = !buttonToggled;
});
},
)))),
],
),
));
}
}
if you setup same size, inset, margin, it should not be different in different device.
have a try with TextStyle(color: Colors.white, inherit: false)