'Unique Challenge - My stepper widget in flutter is disabled and I'm not able to tap on anything
Here's the code
I tried adding a basic stepper widget to flutter. But, the screen is not tappable on mobile and web. However, if I run debug in Chrome and hot reload once. The screen is getting enabled.
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
'''
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';
import 'package:http/http.dart' as http;
import 'package:pull_to_refresh/pull_to_refresh.dart';
class CreateBuildingScreen extends StatefulWidget {
static const routeName = '/create-building';
@override
State<CreateBuildingScreen> createState() => _CreateBuildingScreenState();
}
class _CreateBuildingScreenState extends State<CreateBuildingScreen> {
int _activeCurrentStep = 0;
RefreshController _refreshController = RefreshController();
//step 1
TextEditingController buildingName = TextEditingController();
TextEditingController phoneNumber = TextEditingController();
TextEditingController noOfUnits = TextEditingController();
TextEditingController address = TextEditingController();
TextEditingController state = TextEditingController();
TextEditingController pincode = TextEditingController();
//step 2 - images
TextEditingController image = TextEditingController();
//step 3 - amenities
//image picker
PickedFile _imageFile;
final ImagePicker _picker = ImagePicker();
var uploadUrl = "xys";
void _pickImage() async {
try {
final pickedFile = await _picker.getImage(source: ImageSource.gallery);
setState(() {
_imageFile = pickedFile;
});
} catch (e) {
print("Image picker error " + e);
}
}
Future<String> uploadImage(filepath, url) async {
var request = http.MultipartRequest('POST', Uri.parse(url));
request.files.add(http.MultipartFile('image',
File(filepath).readAsBytes().asStream(), File(filepath).lengthSync(),
filename: filepath.split("/").last));
var res = await request.send();
}
Widget _previewImage() {
if (_imageFile != null) {
return Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Image.file(File(_imageFile.path)),
SizedBox(
height: 20,
),
RaisedButton(
onPressed: () async {
var res = await uploadImage(_imageFile.path, uploadUrl);
print(res);
},
child: const Text('Upload'),
)
],
),
);
} else {
return const Text(
'You have not yet picked an image.',
textAlign: TextAlign.center,
);
}
}
Future<void> retriveLostData() async {
final LostData response = await _picker.getLostData();
if (response.isEmpty) {
return;
}
if (response.file != null) {
setState(() {
_imageFile = response.file;
});
} else {
print('Retrieve error ' + response.exception.code);
}
}
// Here we have created list of steps
// that are required to complete the form
List<Step> stepList() => [
// This is step1 which is called Account.
// Here we will fill our personal details
Step(
state: StepState
.editing, //_activeCurrentStep <= 0 ? StepState.editing : StepState.complete,
isActive: _activeCurrentStep >= 0,
title: const Text('Building Details'),
content: Container(
child: Column(
children: [
TextField(
controller: buildingName,
decoration: const InputDecoration(
border: OutlineInputBorder(),
labelText: 'Building Name',
),
),
const SizedBox(
height: 8,
),
TextField(
controller: phoneNumber,
decoration: const InputDecoration(
border: OutlineInputBorder(),
labelText: 'Phone Number',
),
),
const SizedBox(
height: 8,
),
TextField(
controller: noOfUnits,
decoration: const InputDecoration(
border: OutlineInputBorder(),
labelText: 'No of Units',
),
),
const SizedBox(
height: 8,
),
TextField(
controller: noOfUnits,
decoration: const InputDecoration(
border: OutlineInputBorder(),
labelText: 'Address',
),
),
const SizedBox(
height: 8,
),
TextField(
controller: address,
decoration: const InputDecoration(
border: OutlineInputBorder(),
labelText: 'State',
),
),
const SizedBox(
height: 8,
),
TextField(
controller: state,
decoration: const InputDecoration(
border: OutlineInputBorder(),
labelText: 'City',
),
),
const SizedBox(
height: 8,
),
TextField(
controller: pincode,
decoration: const InputDecoration(
border: OutlineInputBorder(),
labelText: 'Zip Code',
),
),
],
),
),
),
// This is Step2 here we will enter our address
Step(
state: _activeCurrentStep <= 1
? StepState.editing
: StepState.complete,
isActive: _activeCurrentStep >= 1,
title: const Text('Building Image'),
content: Container(
child: Column(
children: [
const SizedBox(
height: 8,
),
Center(
child: Container(
decoration:
BoxDecoration(borderRadius: BorderRadius.circular(5)),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
OutlinedButton(
child: const Icon(Icons.photo_library),
onPressed: _pickImage),
_previewImage()
],
),
),
),
const SizedBox(
height: 8,
),
],
),
)),
// This is Step3 here we will display all the details
// that are entered by the user
Step(
state: StepState.complete,
isActive: _activeCurrentStep >= 2,
title: const Text('Confirm'),
content: Container(
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
mainAxisAlignment: MainAxisAlignment.start,
children: [
// Text('Name: ${name.text}'),
// Text('Email: ${email.text}'),
// Text('Password: ${pass.text}'),
// Text('Address : ${address.text}'),
// Text('PinCode : ${pincode.text}'),
],
)))
];
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text(
'Create Building',
style: TextStyle(color: Colors.white),
),
),
// Here we have initialized the stepper widget
body: SmartRefresher(
controller: _refreshController,
onRefresh: () async {
await Navigator.of(context).pushNamed(CreateBuildingScreen.routeName);
},
enablePullDown: true,
child: Container(
height: (MediaQuery.of(context).size.height -
76 -
MediaQuery.of(context).padding.top),
child: Stepper(
type: StepperType.horizontal,
currentStep: _activeCurrentStep,
steps: stepList(),
// onStepContinue takes us to the next step
onStepContinue: () {
if (_activeCurrentStep < (stepList().length - 1)) {
setState(() {
_activeCurrentStep += 1;
});
}
},
// onStepCancel takes us to the previous step
onStepCancel: () {
if (_activeCurrentStep == 0) {
return;
}
setState(() {
_activeCurrentStep -= 1;
});
},
// onStepTap allows to directly click on the particular step we want
onStepTapped: (int index) {
setState(() {
_activeCurrentStep = index;
});
},
),
),
),
);
}
}
'''
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
